0

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

1
  • Surely you do not want to push ax! Commented May 12, 2015 at 7:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It's printing out the first converted string correctly, but hanging on the second atoi call now. Hopefully I can figure it out from here, thanks.
Also it only correctly prints if 4 or less digits are used as input, I need it so I can type 8 digits.
@tdwig, that's actually a different question. The mechanism of SO is Q&A (with very specific answers to questions) rather than discussion board (chatting about various aspects tangentially related but not core to the original question), so you'll get a wider response of answers if you just post a new question (referring back to this one if you wish).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.