-2

I've got the following code:

.align 32

.data
input1: .string "Give short: \n"
input2: .string "Give char: \n"
arg1: .string "%hu"
arg2: .string "%c"
output1: .string "%hu\n"
output2: .string "%c\n"
short: .short 1
char: .byte 1

.global main

main:
pushl %ebp
movl %esp, %ebp

pushl $input1
call printf
addl $4, %esp

pushl $short
pushl $arg1
call scanf
addl $8, %esp

pushl (short)
pushl $output1
call printf
addl $8, %esp

pushl $input2
call printf
addl $4, %esp

pushl $char
pushl $arg2
call scanf
addl $8, %esp

pushl (char)
pushl $output2
call printf
addl $8, %esp

movl %ebp, %esp
popl %ebp
ret

I want to input short, print it, input char and print it but instead, after printing 'Give char' program ends leaving two blank lines. Any ideas why? It's working when I get the arguments in one function call.

rkhb
  • 14,159
  • 7
  • 32
  • 60
trnks_
  • 45
  • 1
  • 7

1 Answers1

0

This is a straight-forward C question, it has nothing to do with your assembly code. You should probably read the scanf documentation again. It only processes whatever initial part can be converted to a number, the rest of the input is left in the buffer. For well-behaved input that's just the terminating newline, but can be anything. Then, contrary to all the other conversions, the %c does not skip whitespace automatically, so it will happily accept the newline from the buffer. Solution: explicitly adding a space before the format specifier will make scanf skip whitespace. Note you can still provide the input on a single line. If you don't want that you will need to discard the rest of the line manually, or read lines.

Jester
  • 56,577
  • 4
  • 81
  • 125