I am struggling to understand why this x86 assembly code, compiles fine with gcc 4.2.1 (llvm) on OSX, but gives a segmentation fault when the executable is run:
.globl _main
_main:
push %rbp
mov %rsp, %rbp
mov $1, %rbx
push %rbx
lea L_.str0(%rip), %rdi
mov %rbx, %rsi
call _printf
pop %rbx
pop %rbp
ret
.section __TEXT,__cstring,cstring_literals
L_.str0:
.asciz "%d \000"
I observed that if the pop %rbx
line is moved before call _printf
, then the program works correctly. But why should it fail at all in its original form?