Reputation: 55
Let me explain it briefly :).
I approach the argument 10, by using EBP register with ebx register because of the stack structure of which is containing EBP(base register), return address, # of parameter, parameter 1, parameter 2... since I don't use a local register. I could see the parameter was appropriately input as I could print it using call print_string. But, since < while: > code line, it seems like the string 10 doesn't seem to be read for the commend line does nothing when it comes to it. I would gently ask where to start with the code. thanks for reading.
Input : ./atoi 10
Result : 10
%include "asm_io.inc"
segment .data
segment .bss
input resw 1
segment .text
global main
main:
enter 0,0
pusha
mov ebx, [ebp+12]
mov eax, [ebx+4]
; call print_string
dump_stack 1,2,4
mov ebx, 0
mov ecx, 10
while:
cmp al, 0x0a
je print
sub eax, 0x30
mov [input], eax
mov eax, ebx
mul ecx
add eax, [input]
mov ebx, eax
jmp while
print:
mov eax, ebx
call print_int
call print_nl
popa
mov eax, 0
leave
ret
Upvotes: 3
Views: 501
Reputation: 39691
Your while loop doesn't read any characters! You retrieve these using mov dl,[eax]
.
As you can see from the code below, there's no need to use a temporary input variable.
xor ebx, ebx ;Result
while:
movzx edx, byte ptr [eax] ;Read 1 character
test dl, dl ;Test for end of string
jz print ;End found
sub dl, 0x30 ;Go from character to value [0,9]
imul ebx, 10 ;Result x10
add ebx, edx ;Add new digit
inc eax ;To next character
jmp while
Upvotes: 2