I am trying to write a program in x86 that takes in a long long integer input by calling scanf. However, on compiling this with GCC and running it, I get a segmentation fault on line 12 (call scanf
). I am not very experienced with assembly programming and would like to know what exactly is going on in other parts of my code too. Here’s my code:
.format:
.string "%lld"
.global main
main:
pushq %rbp
movq %rsp, %rbp
subq $8, %rsp # allcocating space for a long long integer
andq $16, %rsp # aligning stack pointer
movq %rsp, %rsi
leaq .format(%rip), %rdi # what exactly is this line doing?
call scanf
xorq %rax, %rax
movq %rbp, %rsp
popq %rbp
ret
I would like to know what happens if we do not align the stack pointer to a multiple of 16 bytes, as well as what the leaq
line is doing. Any help would be much appreciated.
I tried looking it up online and some sources suggested that I use a separate section to allocate space for the variable like so:
.data:
.long 0
Again, what exactly is this doing?
Gautam Bhetanabhotla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.