Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .text
- global _start
- %include "includes/print.asm" ; syscall to print to screen function
- %include "includes/leave.asm" ; syscall to exit program
- _start:
- mov ebx, 0 ; i = 0 in loop
- itoa_start:
- xor edx, edx ; clear edx
- mov eax, [num] ; move value of num into eax
- mov ecx, 10 ; move 10 into ecx (divisor)
- div ecx ; div ecx (ex. 1234 / 10 = 1234.5)
- cmp edx, 0 ; compare remainder (edx ex. .5) to 0
- je itoa_end ; if remainder == 0 jump to end label
- mov [num], eax ; move quotient (eax ex. 1234) into num (12345 -> 1234)
- add edx, '0' ; add '0' (48) to remainder to convert into ASCII (5 -> '5')
- mov [res + ebx], edx ; move remainder into res value + ebx (ex. res + 0 -> '5')
- add ebx, 1 ; increase ebx value by 1
- jmp itoa_start
- itoa_end:
- add ebx, 1 ; increase ebx value by 1 for terminating string
- mov word [res + ebx], 0 ; move 0 (terminator) into res + ebx
- mov ecx, res
- mov edx, 5
- call print ; print res string
- call leave
- section .data
- ; msg db "Enter num: " ; Uncommenting this and running works as expected
- ; msg_len equ $ - msg ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- ; msg db "HELLO" ; Uncommenting this and running gives another value (2863)
- ; msg_len equ $ - msg ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- ; Strangely, adding ": " to the end of "HELLO" makes it work fine
- num dw 12345 ; Defining word num with value 12345
- section .bss
- res resw 6 ; Reserving word of 6 for res variable
Advertisement
Add Comment
Please, Sign In to add comment