0

I'm writing a x86 (32-bit) assembly program to read two characters from the user and then print them. I'm using syscalls for reading and writing, but my program only waits for the first input and then exits without waiting for the second input:

section .data
    input_buffer1 db 0 ;will store ASCII for the first number from 0 to 9
    input_buffer2 db 0

section .text
    global _start

_start:
    mov ecx, input_buffer1
    call input
    call write

    mov ecx, input_buffer2
    call input
    call write

    call exit

sys_call:
    xor ebx, ebx
    mov edx, 1
    int 0x80
    ret

input:
    mov eax, 3
    call sys_call
    ret

write:
    mov eax, 4
    call sys_call
    ret

exit:
    mov eax, 1
    xor ebx, ebx
    int 0x80
    ret

When I run this program, it waits for the first input, prints it, and then exits immediately without waiting for the second input. What is the issue with my code, and how can I fix it so that my program waits for the second input as intended? I tried asking chat gpt, but none of its suggestions, such as changing the terminal's settings, work.

Note: I'm using NASM as my assembler and Ubuntu 22.04, which I don't know if it's relevant.

  • 3
    Use `strace ./a.out` to see that each 1-byte `read` gets 1 character, the second one being the newline from pressing enter. One simplistic way is to do a larger read and only take the first character each time, discarding the rest of the input. But making assumptions about lines and `read` calls only works in simple cases when reading from the terminal, not `./a.out < file` – Peter Cordes May 04 '23 at 12:19
  • 2
    Also, your program writes to stdin, which only happens to work on a terminal because stdin, stdout, and stderr are all duplicates of each other, a read/write open of the TTY. – Peter Cordes May 04 '23 at 12:22

0 Answers0