I'm having trouble understanding why my code causes segmentation fault.
.section .rdata
format:
.asciz "Hello world\n"
.text
.globl main
main:
pushq %rbp
leaq format(%rip), %rcx
call printf
popq %rbp
ret
This is a simple assembly code that I wrote, and I compiiled using the following command using GCC in 64-bit windows.
gcc hello.s
After printing Hello world followed by a newline, the program crashes with Segmentation fault due to STATUS_ACCESS_VIOLATION. Is there anything wrong with my code?
sub $32, %rspjust before thecall printfand thenadd $32, %rspright after the call?mainwas misaligned by the return address on the stack and thepush %rbpaligned back to 16-bytes. So subtracting 32 from that is of course still aligned. At function call it is aligned. But after control is transferred the call misaligns by 8. Push of one 64-bit register realigns to 16. Had the PUSH not been there then I would have complained about alignment.C, so please remove thectag