; int_ascii.asm
; convert an integer to an ASCII string
; by renzo diomedi

section .data
NULL equ 0
EXIT_SUCCESS equ 0
SYS_exit equ 60
intNum dd 1498

section .bss
strNum resb 10

section .text
global _start
_start:
mov eax, dword[intNum]      ; get integer
mov rcx, 0                  ; counter
mov ebx, 10                 ; divider

divLoop:
mov edx, 0
div ebx                     ; divide eax by 10
                            ; result in eax, remainder in edx
push rdx
inc rcx
cmp eax, 0                  ; if eax > 0
jne divLoop 

; convert remainders and store
mov rbx, strNum             ; get address of string
mov rdi, 0                  ; i = 0

POPloop:
pop rax                     ; pop integer digit
mov al, "0"                 ; char = int + "0"
mov byte [rbx+rdi], al      ; string[i] = char
inc rdi
loop POPloop

mov byte[rbx+rdi], NULL     ; string[i] = NULL

mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall

