c - Trouble with converting string to integer using ATOI In Assembly -
i'm trying read in 2 strings, convert them numbers using atoi function, , print out result.
here's uninitialized variables. (%define buflen 20)
section .bss ; uninitialized data section m: resb buflen ;string 1 mlen: resb 4 r: resb buflen ;string 2 rlen: resb 4
here's user input/ attempt allocate memory
; prompt user first number mov eax, syscall_write ; write function mov ebx, stdout ; arg1: file descriptor mov ecx, msg1 ; arg2: addr of message mov edx, len1 ; arg3: length of message int 080h ; ask kernel write ; read in user input ; mov eax, syscall_read ; read function mov ebx, stdin ; arg 1: file descriptor mov ecx, m ; arg 2: address of buffer mov edx, buflen ; arg 3: buffer length int 080h mov [rlen], eax ; save length of string read ; prompt user second number mov eax, syscall_write ; write function mov ebx, stdout ; arg1: file descriptor mov ecx, msg2 ; arg2: addr of message mov edx, len2 ; arg3: length of message int 080h ; ask kernel write ; read in user input mov eax, syscall_read ; read function mov ebx, stdin ; source mov ecx, r ; destination mov edx, buflen ; length of destination int 080h mov [mlen], eax ; save length of string read
now i'm trying convert strings using atoi , print them out
;convert # mov eax, 0 ;zero out register mov eax, m call atoi add esp, 4 ;print push ax push print_r call printf add esp, 8 ;convert # mov eax, 0 ;zero out register mov eax, r call atoi add esp, 4 ;print push ax push print_r call printf add esp, 8
this output...
enter first #: 1234
enter second#: 1234
number: 1234
hanging on second atoi call
for start, you're not allocating enough space input and you're not reading properly.
if input string 12345678
, need 8 bytes characters, 1 newline, , 1 terminating \0
. so, resd 1
not going cut mustard, gives 8 bytes rather ten.
for reading information:
mov eax, syscall_read ; read function mov ebx, stdin ; arg 1: file descriptor mov ecx, m ; arg 2: address of buffer mov edx, 1 ; arg 3: buffer length int 080h
edx
meant number of bytes read , have set 1
reason. that's not going entire number, rather first character of first number.
on top of input problems, there's couple of problems there well.
first, statement: mov eax, [m] gets contents of memory @ m
. if you're calling atoi
, want address itself.
secondly, need examine calling convention. adding of values esp
seems ... unusual me. may correct doesn't seem match calling convention i've ever seen.
Comments
Post a Comment