This is gcc assembler code produced by my compiler. Comments describe what should be happenning (in my opinion), and i'm entering zero. Yet the output of the program is 4 (or 4 + whatever I put in the prompt).
.globl main
main:
pushq %rbp
movq %rsp, %rbp
subq $0, %rsp
movq $1, %rbx ; rbx = 1
movq $46, %rdx ; rbx = 1 rdx = 46
movq %rbx, %rcx ; rbx = 1 rdx = 46 rcx = 1
addq $7, %rcx ; rbx = 1 rdx = 46 rcx = 8
movq $4, %rbx ; rbx = 4 rdx = 46 rcx = 8
addq %rcx, %rbx ; rbx = 12 rdx = 46 rcx = 8
addq %rdx, %rcx ; rbx = 12 rdx = 46 rcx = 54
callq read_int ; rbx = 12 rdx = 46 rcx = 54
movq %rax, %rdx ; rbx = 12 rdx = R rcx = 54
negq %rbx ; rbx = -12 rdx = R rcx = 54
addq %rbx, %rcx ; rbx = -12 rdx = R rcx = 42
movq %rdx, %rbx ; rbx = R rdx = R rcx = 42
addq %rcx, %rbx ; rbx = R rdx = R rcx = 42 + R
movq %rbx, %rax ; rax = 42 + R
movq %rax, %rdi
callq print_int
addq $0, %rsp
movq $0, %rax
popq %rbp
retq
Don't really understand why this is the case. If I try compiling it without the read instruction it works fine. The only difference in code being
movq $0, %rdx
instead of
callq read_int
movq %rax, %rdx
But %rax register is not used before. And no register is holding 4. Code for read_int is
int64_t read_int() {
int64_t i;
scanf("%" SCNd64, &i);
return i;
And it works fine by itself e.g. code that only only calls read int and moves rax into rdi, then prints it works fine.
Is this function call somehow messing with other registers?