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

section .note.GNU-stack

section .data
NULL equ 0

section .bss
strNum resb 10
arr resd 1

section .text
global intascii
intascii:
push rbp
mov eax, dword[arr]      ; 
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 r8, 0                  ; i = 0

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

mov byte[rbx+r8], NULL     ; string[i] = NULL
                          
pop rbp
ret

