Hello!
I have a problem in trying to convert a string into an integer. I'm not supposed to use the routine atoi, ie I have to do it on my own. Here is the code;
.data
String: .asciz "1234\n"
Show_integer: .asciz "The integer = %d\n"
Intg: .long 0
.text
.global main
main:
movl $1, %edi
movl $0, %ebx
movl $String, %ecx
character_push_loop:
movb (%ecx), %dl /*Take one byte from the string and put in %dl*/
cmpb $0, %dl
je conversion_loop
movb %dl, (%eax)
pushl %eax /*Push the byte on the stack*/
incl %ecx
incl %ebx
jmp character_push_loop
conversion_loop:
popl %eax
subl $48, %eax /*convert to ASCII number*/
imul %edi /* eax = eax*edi */
addl %eax, Intg
movl $10, %eax
movl %edi, %eax
imul %ecx /* eax = eax*ecx */
movl %eax,%edi /* edi = eax */
decl %ebx
cmpl $0, %ebx
je end /*When done jump to end*/
jmp conversion_loop
end:
pushl Intg
pushl $Show_integer
call printf /*Print out integer*/
addl $8, %esp
call exit
The result from this is:
The integer = 1463561460
But this is obviously wrong, the answer should be 1234 but now I guess I'm pointing to a memoryaddress. Can anyone see what is wrong here?
Anders