Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Number Counter on Assembly
- ; Registers can be used
- ; ESI, EBP,
- %macro PrintUnits 0
- mov eax, 4 ; sys_write
- mov ebx, 1 ; file descriptor (stdout)
- mov ecx, Units ; Count register for string values
- mov edx, 1 ; 1 byte for 1 charactor
- int 0x80 ; call kernel
- %endmacro
- %macro PrintTens 0
- mov eax, 4 ; sys_write
- mov ebx, 1 ; file descriptor (stdout)
- mov ecx, Tens ; Count register for string values
- mov edx, 1 ; 1 byte for 1 charactor
- int 0x80 ; call kernel
- %endmacro
- %macro PrintHundreds 0
- mov eax, 4 ; sys_write
- mov ebx, 1 ; file descriptor (stdout)
- mov ecx, Hundreds ; Count register for string values
- mov edx, 1 ; 1 byte for 1 charactor
- int 0x80 ; call kernel
- %endmacro
- %macro Printnewline 0 ; Same as above prints
- mov eax, 4
- mov ebx, 1
- mov ecx, Newline
- mov edx, Nllen
- int 0x80
- %endmacro
- section .data
- Message: db 'Number Counter by UNIXnerdV 2022', 0xA, 0xA ; String
- msglen: equ $-Message ; length or size of string
- Newline: db '',0xA ; Newline "\n"
- Nllen: equ $-Newline ; Newline length
- section .bss
- Units: resb 1 ; 1 byte reserve
- Tens: resb 1 ; 1 byte reserve
- Hundreds resb 1 ; same
- section .text
- global _start
- _start:
- mov esi, 48 ; Set to ASCII Value of 0 using code
- mov ebp, 48 ; same as above
- mov edi, 48 ; same
- mov eax, 4 ; sys_write
- mov ebx, 1 ; fd
- mov ecx, Message ; String
- mov edx, msglen ; Length
- int 0x80
- jmp print_units ; start to display numbers jump to function
- print_units: ; Print Numbers
- mov [Units], esi ; move values Units variable from ESI (Source Index)
- mov [Tens], ebp ; move values Tens variable from ESI (Base)
- mov [Hundreds],edi ; move values Hundreds variable from EDI (Destination Index)
- ; Print Numbers
- PrintHundreds ; Print Hundreds column
- PrintTens ; Print Tens column
- PrintUnits ; Print Units column
- Printnewline ; Newline "\n"
- inc esi ; Increment Unit's register
- cmp esi, 57 ; Compare if matches ASCII value of 57 (Number 9)
- jle print_units ; jump less equal to start of function
- cmp esi, 57 ; compare again
- je _inc_tens ; if equals jump to increment tens
- _inc_tens: ; Increments the tens
- mov esi, 48 ; Reset previous register to 0
- inc ebp ; increment ten's register
- mov [Tens], ebp ; save value to variabe
- cmp ebp, 57 ; compare for number 9 (57)
- jle print_units ; start at beginning
- cmp ebp, 57 ; compare again
- je _inc_hundreds ; if match jump to hundreds
- _inc_hundreds: ; Increment Hundreads
- mov ebp, 48 ; Reset Previous register to 0
- inc edi ; increment hundreds register
- mov [Hundreds], edi ; save
- cmp edi, 57 ; compare
- jle print_units ; start again if does not reach 9
- cmp edi, 57 ; compare again
- je _sysexit ; quits if reaches 9 (57)
- _sysexit:
- mov eax, 1 ; System exit (exit(0) in C)
- mov ebx, 0 ; Exit code 0
- int 0x80 ; omitting this will cause a seg fault
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement