# Converting number to string

hi everybody
how do I convert in assembly a number ( 0 - 4321078) to a string ?
Merci
Bodo

• : hi everybody
: how do I convert in assembly a number ( 0 - 4321078) to a string ?
: Merci
: Bodo
:
:

You can divide it by 10 repeatedly and store a remainder as a digit char starting from the end of the buffer:

1024/10 = 102 and 4 -> make '4' and store it
102/10 = 10 and 2 -> make '2' and store it
10/10 = 1 and 0 -> make '0' and store it
1/10 = 0 and 1 -> make '1' and store it -> done...

• ok, my code looks now like that :

mov eax,total_bytes
mov ecx,10
l1: mov dx,0
div ecx
push edx
cmp eax,0
jne l1

now when I pop it again, where do I know that my stack is
empty, and not holding back still numbers

Cheers
Bodo

• : ok, my code looks now like that :
:
: mov eax,total_bytes
: mov ecx,10
: l1: mov dx,0
: div ecx
: push edx
: cmp eax,0
: jne l1
:
: now when I pop it again, where do I know that my stack is
: empty, and not holding back still numbers
:
: Cheers
: Bodo
:
:
:

.DATA
strOutput db 16 dup (0)

.CODE
mov eax, total
mov ecx, 10
xor bx, bx ; Count the PUSHes

divide:
xor edx, edx
div ecx
push dx ; DL is a digit in range [0..9]
inc bx ; Count it

test eax, eax
jnz divide ; EAX is not zero - so, continue...

; Now POP them all using BX as a counter
mov cx, bx
lea si, strOutput ; DS:SI points to your buffer

get_digit:
pop ax
add al, '0' ; Make it ASCII

; Save it in the buffer
mov [si], al
inc si

loop get_digit

; Store '\$' to use with Func. 9h Int 21h
mov al, '\$'
mov [si], al

; To screen...
lea dx, strOutput
mov ah, 9
int 21h

; DONE!!!!!!!!