2

I am learning NASM Assembly and I am writing a program to receive input and printout the input.

global main
extern read
extern printf

section .data
    request: db "Enter a string (len <= 32): ", 0
    response: db "You have typed: '%s'", 0x0a, 0
    format: db "%s", 0

section .bss
    val: resb 32

section .text
main:
    push ebp
    mov ebp, esp

    push request 
    call printf
    add esp, 4 

    push 32
    push val
    push 1
    call read
    add esp, 12

    mov eax, val

    push eax
    push response
    call printf
    add esp, 8

    mov esp, ebp
    pop ebp
    mov eax, 0
    ret

A weird thing is when I execute the code, the read() function seems to be executed before the first printf(). I am not quite sure why this happens.

screenshot

Thank you very much for your support.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 7
    The C `printf` function write to the C `stdout` stream. That stream, when the program is running from an interactive terminal (like you seem to be doing) will be *line buffered*. Output won't be flushed (actually written) unless you explicitly flush the stream with either `fflush(stdout)` or have a newline in the string. – Some programmer dude Sep 08 '22 at 11:43
  • So if I want to have the first message still on the first line, I have to add a newline character in the message "Enter a string..." or using fflush right? – Anh Nguyễn Tuấn Sep 08 '22 at 11:45
  • Yes that's correct. – Some programmer dude Sep 08 '22 at 11:48

0 Answers0