Advertisement
UNIXnerdV

Assembly Assignment

Jan 6th, 2022
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Assembly work for Asssignment
  2. ;
  3. ; Course: Assembly Programming for all Platforms
  4. ;
  5. ; Copyright (C) 2022 UNIXn3rdV
  6.  
  7.  
  8. ; Macros takes 2 arguments and will be required to use
  9. ;
  10. ;
  11. %macro print_line 2
  12.  
  13.     mov eax, 4      ; sys_write call
  14.     mov ebx, 1      ; use a file descriptor (fd) - Screen
  15.     mov ecx, %1     ; Data in to buffer
  16.     mov edx, %2     ; send no of bytes to buffer
  17.    
  18.     int 0x80        ; call kernel interupt
  19.    
  20. ;  Sys_write system call (EAX is 32 Bit while RAX is 64 Bit)
  21. ;
  22. ; EAX   Arg1 (EBX)  Arg2 (ECX)    Arg3 (EDX)
  23. ;  4      fd         String     Bytes of string
  24. ;
  25. %endmacro
  26.  
  27. %macro print_Linux_lines 2
  28.  
  29.     mov eax, 4      ; sys_write call
  30.     mov ebx, 1      ; use a file descriptor (fd) - Screen
  31.     mov ecx, %1     ; Data in to buffer
  32.     mov edx, %2     ; send no of bytes to buffer
  33.     int 0x80        ; call kernel interupt
  34.    
  35.     ; Add a newline after print
  36.     mov eax, 4
  37.     mov ebx, 1
  38.     mov ecx, newline
  39.     mov edx, nllen
  40.     int 0x80
  41.  
  42.  
  43. %endmacro
  44.  
  45. ; similar to the C putchar
  46. %macro putchar 2
  47.     mov eax, 4
  48.     mov ebx, 1
  49.     mov ecx, %1
  50.     mov edx, %2
  51.     int 0x80
  52.    
  53.     ; Add a space after the charactor
  54.     mov eax, 4
  55.     mov ebx, 1
  56.     mov ecx, space
  57.     mov edx, splen
  58.     int 0x80
  59.    
  60. %endmacro
  61.  
  62. %macro read_line 2
  63.  
  64.     mov eax, 3      ; sys_read call
  65.     mov ebx, 1      ; use file descriptor (fd) - Keyboard
  66.     mov ecx, %1     ; output buffer
  67.     mov edx, %2     ; size
  68.    
  69.     int 0x80        ; call kernel interrupt
  70.    
  71.  
  72. ;  Sys_read system call
  73. ;
  74. ; EAX   Arg1 (EBX)  Arg2 (ECX)    Arg3 (EDX)
  75. ;  3      fd         Variable   Bytes of variable
  76. ;
  77.  
  78. %endmacro
  79.  
  80.  
  81.  
  82. ; Data Section will contain values to be addressed in memory before
  83. ; compilation of the program
  84. ;
  85.  
  86. section .data
  87.    
  88.     newline: db ' ', 0xA        ; New line
  89.     nllen: equ $-newline   
  90.    
  91.     space: db ' ', 0 ; Space
  92.     splen: equ $-space 
  93.    
  94.     progn: db 'Assembly assignment program',0xA     ; Program name (db = define bytes)
  95.     pglen: equ $-progn                              ; Program name length in bytes calculation
  96.    
  97.     copyr: db 'Copyright (C) 2022 UNIXnerd',0xA,0xA ; copyright message
  98.     crlen: equ $-copyr                              ; copyright length in bytes calculation
  99.    
  100.     prompt1: db 'Please Enter your name: ', 0   ; Prompt a user
  101.     prmptlen: equ $-prompt1
  102.    
  103.     hello: db 'Hello, ', 0  ; Hello
  104.     helen: equ $-hello
  105.    
  106.     ASCIIShow: db 'Now we are going to show all non-extended ASCII Charactors', 0xA
  107.     ASClen: equ $-ASCIIShow
  108.    
  109.     asm: db 'Made in Assembly (Linux)', 0
  110.     asmlen: equ $-asm
  111.    
  112.  
  113. ; Section for variables reserving memory in bytes for future
  114. ; use in the program
  115. ;
  116. section .bss
  117.         name:  resb 20  ; reserve 20 bytes for a name  - for input
  118.         char:  resb 5   ; this is for ascii charactor related
  119.         no:    resb 2       ; Number to use for edx
  120.        
  121.  
  122. ; This is where most of code will be
  123. section .text
  124.     global _start
  125.  
  126.  
  127. _start:
  128.  
  129.     print_line progn, pglen         ; Display Program name
  130.     print_line copyr, crlen         ; Display Copyright
  131.     print_line prompt1, prmptlen    ; Show Prompt
  132.     read_line name, 20              ; Take input
  133.     print_line newline, nllen       ; Line Spacing
  134.     print_line hello, helen         ; Display Hello
  135.     print_line name, 20             ; display name
  136.     print_line newline, nllen       ; Line Spacing
  137.     print_line ASCIIShow, ASClen    ; Display message on ASCII Charactor display
  138.     print_line newline, nllen       ; Line Spacing
  139.    
  140.     ; Display ASCII Charactors
  141.     mov esi, 48                     ; Start ASCII at 48
  142.     call print_ascii
  143.     print_line newline, nllen       ; Line Spacing
  144.     print_line newline, nllen       ; Line Spacing
  145.    
  146.     mov edi, 1                      ; for printing linux line
  147.     mov [no], edi
  148.     call print_Linux
  149.     print_line newline, nllen       ; Line Spacing
  150.    
  151.    
  152.    
  153.     call sys_exit                   ; exit the program
  154.    
  155.    
  156. print_ascii:
  157.    
  158.     mov [char], esi                 ; put value in to variable
  159.     putchar char, 1             ;
  160.    
  161.     inc esi
  162.     ;add esi, 1
  163.     cmp esi, 126                    ; 126 is final charactor
  164.     jle print_ascii                 ; jump back to start of block until 126 is reached
  165.     ret
  166.    
  167. print_Linux:
  168.     mov [no], edi
  169.     print_Linux_lines asm, [no]     ; no must be in brackets (Variable) or it will over run.
  170.     inc edi
  171.     cmp edi, asmlen             ; compare value with total length of linux line
  172.     jle print_Linux
  173.     ret
  174.    
  175. sys_exit:
  176.     mov eax, 1          ; sys_exit call
  177.     mov ebx, 0          ; show exit code 0
  178.     int 0x80            ; interrupt the kernel
  179.     ret
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement