Having some problems where my string is in the wrong order.
I have written a function in assembly to convert an integer into a string:
.type itos, @function
itos:
pushl %ebp
movl %esp, %ebp
movl 8(%esp), %eax # number
movl $12, %edi
leal (%esp, %edi, 1), %ebx # buffer
movl $0, %edi # counter
pushl $0x0
jmp itos_loop
itos_loop:
movl $0, %edx
movl $10, %ecx
div %ecx
addl $48, %edx
pushl %edx
movb (%esp), %cl
movb %cl, (%ebx, %edi, 1)
test %eax, %eax
je itos_end
inc %edi
jmp itos_loop
itos_end:
movl %ebx, %eax
movl %ebp, %esp
popl %ebp
ret
I call my function like this (converting the number 256 as an example):
subl $8, %esp # making space for a buffer
pushl (%esp) # pushing buffer as 2 argument to function
pushl $256 # pushing an integer as 1 argument to function
call itos
pushl %eax
call print
When I run this, I get the following output:
652
I understand why it is in reverse and I have some ideas on how to make it not in reverse.
For example, instead of pushing (%esp)
as the buffer, I could push 8(%esp)
and then change my itos
function to decrement instead of increment.
However I am not too keen on that solution.
What would be another efficient way of doing this?
And btw, my print
that I am using in the code above looks like this:
print:
pushl %ebp
movl %esp, %ebp
pushl 8(%esp)
call strlen
addl $4, %esp
movl 8(%esp), %ecx
movl %eax, %edx
movl $4, %eax
movl $1, %ebx
movl %ebp, %esp
popl %ebp
int $0x80
ret
Thank you!