简体   繁体   中英

Trouble with converting string to integer using ATOI In Assembly

I'm trying to read in two strings, convert them into numbers using atoi function, and then print out the result.

Here's my 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 where I get the user input/ attempt allocate it into memory

   ; prompt user for 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 to 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 for 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 to 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 to convert the strings using atoi and print them out

    ;CONVERT TO #
    mov     eax, 0                  ;zero out register
    mov     eax, m
    call    atoi
    add     esp, 4

    ;PRINT IT
    push    ax
    push    print_r
    call    printf
    add     esp, 8

    ;CONVERT TO #
    mov     eax, 0                  ;zero out register
    mov     eax, r
    call    atoi
    add     esp, 4

    ;PRINT IT
    push    ax
    push    print_r
    call    printf
    add     esp, 8

This is my output...

Enter first #: 1234

Enter second#: 1234

Number: 1234

Hanging on second atoi call

For a start, you're not allocating enough space for the input and you're not reading it properly.

If you input the string 12345678 , you need eight bytes for the characters, one for the newline, and one for the terminating \\0 . So, a RESD 1 is not going to cut the mustard, it only gives you eight bytes rather than ten.

For actually reading the 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 is meant to be the number of bytes to read and you have set it to 1 for some reason. That's not going to get your entire number, rather it will just get the first character of the first number.

On top of the input problems, there's a couple of problems there as well.

First, the statement: mov eax, [m] gets the contents of memory at m . If you're calling atoi , it will want the address itself.

Secondly, you need to examine your calling convention. The adding of values to esp seems very ... unusual to me. It may be correct but it doesn't seem to match any calling convention I've ever seen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM