Advertisement
UNIXnerdV

Number Counter

Jan 9th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Number Counter on Assembly
  2.  
  3. ; Registers can be used
  4. ; ESI, EBP,
  5.  
  6. %macro PrintUnits 0
  7.    
  8.     mov eax, 4      ; sys_write
  9.     mov ebx, 1      ; file descriptor (stdout)
  10.     mov ecx, Units  ; Count register for string values
  11.     mov edx, 1      ; 1 byte for 1 charactor
  12.     int 0x80        ; call kernel
  13. %endmacro
  14.  
  15. %macro PrintTens 0
  16.    
  17.     mov eax, 4      ; sys_write
  18.     mov ebx, 1      ; file descriptor (stdout)
  19.     mov ecx, Tens   ; Count register for string values
  20.     mov edx, 1      ; 1 byte for 1 charactor
  21.     int 0x80        ; call kernel
  22. %endmacro
  23.  
  24. %macro PrintHundreds 0
  25.    
  26.     mov eax, 4          ; sys_write
  27.     mov ebx, 1          ; file descriptor (stdout)
  28.     mov ecx, Hundreds   ; Count register for string values
  29.     mov edx, 1          ; 1 byte for 1 charactor
  30.     int 0x80            ; call kernel
  31. %endmacro
  32.  
  33. %macro Printnewline 0   ; Same as above prints
  34.     mov eax, 4
  35.     mov ebx, 1
  36.     mov ecx, Newline
  37.     mov edx, Nllen
  38.     int 0x80
  39. %endmacro
  40.  
  41. section .data
  42.     Message: db 'Number Counter by UNIXnerdV 2022', 0xA, 0xA    ; String
  43.     msglen: equ $-Message       ; length or size of string
  44.     Newline: db '',0xA          ; Newline "\n"
  45.     Nllen: equ $-Newline        ; Newline length
  46.  
  47. section .bss
  48.  
  49.     Units:   resb 1     ; 1 byte reserve
  50.     Tens:    resb 1     ; 1 byte reserve
  51.     Hundreds resb 1     ; same     
  52.    
  53.    
  54. section .text
  55.     global _start
  56.  
  57.  
  58. _start:
  59.         mov esi, 48         ; Set to ASCII Value of 0 using code
  60.         mov ebp, 48         ; same as above
  61.         mov edi, 48         ; same
  62.        
  63.         mov eax, 4          ; sys_write
  64.         mov ebx, 1          ; fd
  65.         mov ecx, Message    ; String
  66.         mov edx, msglen     ; Length
  67.         int 0x80
  68.        
  69.         jmp print_units     ; start to display numbers jump to function
  70.  
  71. print_units:                    ; Print  Numbers
  72.        
  73.         mov [Units], esi    ; move values Units variable from ESI (Source Index)
  74.         mov [Tens], ebp     ; move values Tens variable from ESI (Base)
  75.         mov [Hundreds],edi  ; move values Hundreds variable from EDI (Destination Index)
  76.        
  77.         ; Print Numbers
  78.         PrintHundreds       ; Print Hundreds column
  79.         PrintTens           ; Print Tens column
  80.         PrintUnits          ; Print Units column
  81.         Printnewline        ; Newline "\n"
  82.        
  83.        
  84.         inc esi             ; Increment Unit's register
  85.         cmp esi, 57         ; Compare if matches ASCII value of 57 (Number 9)
  86.         jle print_units     ; jump less equal to start of function
  87.         cmp esi, 57         ; compare again
  88.         je _inc_tens        ; if equals jump to increment tens
  89.  
  90. _inc_tens:                          ; Increments the tens
  91.     mov esi, 48                     ; Reset previous register to 0
  92.     inc ebp                         ; increment ten's register
  93.     mov [Tens], ebp                 ; save value to variabe
  94.    
  95.     cmp ebp, 57                     ; compare for number 9 (57)
  96.     jle print_units                 ; start at beginning
  97.    
  98.     cmp ebp, 57                     ; compare again
  99.     je _inc_hundreds                ; if match jump to hundreds
  100.    
  101. _inc_hundreds:                      ; Increment Hundreads
  102.     mov ebp, 48                     ; Reset Previous register to 0
  103.     inc edi                         ; increment hundreds register
  104.     mov [Hundreds], edi             ; save
  105.    
  106.     cmp edi, 57                     ; compare
  107.     jle print_units                 ; start again if does not reach 9
  108.    
  109.     cmp edi, 57                     ; compare again
  110.     je _sysexit                     ; quits if reaches 9 (57)
  111.        
  112.  
  113. _sysexit:
  114.     mov eax, 1          ; System exit (exit(0) in C)
  115.     mov ebx, 0          ; Exit code 0
  116.     int 0x80            ; omitting this will cause a seg fault
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement