Who know the root cause of print order error? How to resolve it? I compiled the following code to assembly:
{
int a, b;
sprint("input number\n");
scan (a);
iprint(a);
}
This generated the following assembly code:
IO:
.string "%lld\n"
.text
.globl main
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
.data
L1: .string "input number"
.text
leaq L1(%rip), %rcx
movq $0, %rax
subq $32, %rsp
callq printf <-------printf
addq $32, %rsp
movq %rbp, %rax
leaq -8(%rax), %rax
movq %rax, %rdx
leaq IO(%rip), %rcx
subq $32, %rsp
callq scanf <---------scanf
addq $32, %rsp
movq %rbp, %rax
leaq -8(%rax), %rax
movq (%rax), %rax
pushq %rax
popq %rdx
leaq IO(%rip), %rcx
subq $32, %rsp
callq printf <--------printf
addq $32, %rsp
leaveq
retq
I installed Embarcadero-dev-cpp on my windows 7 and used gcc tmp.s
to get a execute file a.exe, and then ran a.exe
$ ./a.exe
987 <-----my input, should be the second display
input number <-------should be print firstly
987 <-------the last.
jason@jason-PC ~/samplecygwin
$
The cation order of printf, scanf is wrong.
My high language code is translate to assembly code. Should I insert flush(stdout) after printf in my compiler? Thank you. it's a common problem, but only meet seldom, refer to the following please.