Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SECTION .text
- global _start
- _start:
- mov ecx, 0
- loop:
- inc ecx
- mov eax, ecx
- call iprint
- push eax ; push eax onto the stack to preserve it while we use the eax register in this function
- mov eax, 0Ah ; move 0Ah into eax - linebreak
- push eax ; push the linefeed onto the stack so we can get the address
- mov eax, esp ; move the address of the current stack pointer into eax for sprint
- call stringPrint
- pop eax ; remove our linefeed character from the stack
- pop eax ; restore the original value of eax before our function was called
- cmp ecx, 12 ; 12 is the hard-coded end of the loop
- jne loop
- ;quit
- mov ebx, 0
- mov eax, 1
- int 80h
- ret
- ; -------SUBROUTINES-----------
- iprint:
- push eax ; counter of how many bytes we need to print in the endpush eax ; preserve eax on the stack to be restored after function runs
- push ecx ; preserve ecx on the stack to be restored after function runs
- push edx ; preserve edx on the stack to be restored after function runs
- push esi ; preserve esi on the stack to be restored after function runs
- mov ecx, 0
- divideLoop:
- inc ecx ; count each byte to print - number of characters
- mov edx, 0 ; empty edx
- mov esi, 10 ; mov 10 into esi
- idiv esi ; divide eax by esi
- add edx, 48 ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction
- push edx ; push edx (string representation of an intger) onto the stack
- cmp eax, 0 ; can the integer be divided anymore?
- jnz divideLoop ; jump if not zero to the label divideLoop
- printLoop:
- dec ecx ; count down each byte that we put on the stack
- mov eax, esp ; mov the stack pointer into eax for printing
- call stringPrint ; call our string print function
- pop eax ; remove last character from the stack to move esp forward
- cmp ecx, 0 ; have we printed all bytes we pushed onto the stack?
- jnz printLoop ; jump is not zero to the label printLoop
- pop esi ; restore esi from the value we pushed onto the stack at the start
- pop edx ; restore edx from the value we pushed onto the stack at the start
- pop ecx ; restore ecx from the value we pushed onto the stack at the start
- pop eax ; restore eax from the value we pushed onto the stack at the start
- ret
- slen:
- push ebx
- mov ebx, eax
- nextchar:
- cmp byte [eax], 0
- jz finished
- inc eax
- jmp nextchar
- finished:
- sub eax, ebx
- pop ebx
- ret
- stringPrint:
- push edx
- push ecx
- push ebx
- push eax
- call slen
- mov edx, eax
- pop eax
- mov ecx, eax
- mov ebx, 1
- mov eax, 4
- int 80h
- pop ebx
- pop ecx
- pop edx
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement