0

I am trying to read a character in a loop from keyboard until I pass '&'. The code seems to work if I try to read numbers and with format "%d", but not with "%c".

.386
.model flat,stdcall

includelib msvcrt.lib
extern exit:proc
extern printf:proc
extern scanf:proc

public start

.data
    string db 100 dup(?)
    msg db "Read until &",13,10,0
    format db "%c",0
    caracter db " "

.code
start:
    mov ecx,3
    mov esi,0
    reader:
        push ecx

        push offset msg
        call printf 
        add esp,4

        pop edx
        push edx

        push offset caracter
        push offset format
        call scanf 
        add esp,8


        pop ecx
    loop reader

    exit_from:



push 0
call exit
end start

The result is something like this:

Read until &
a
Read until &
Read until &
b
exit

What am I missing, and how do I fix it?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
sylar12
  • 107
  • 1
  • 8
  • What exactly do you think is a problem? Looks like everything is working as expected. `%c` doesn't skip whitespace (including newline), so `ab` scans 3 characters. You can use `" %c"` to skip leading whitespace. – Peter Cordes Feb 10 '18 at 09:10
  • " %c" works. Thanks a lot! – sylar12 Feb 10 '18 at 09:13

0 Answers0