This code assembles fine using GCC in terminal:
.globl _sub
_sub:
push %rbp
mov %rbp,%rsp
mov $0xBEEF,%eax
pop %rbp
ret
.globl _main
_main:
push %rbp
mov %rbp,%rsp
call _sub
mov $0,%eax
pop %rbp
ret
But when I run the a.out file, I get this error:
Segmentation fault: 11
If I run it in GDB this is the error I get:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000001
0x0000000000000001 in ?? ()
Any ideas as to why this might be happening?
mov %rbp, %rspcorrupts the stack pointer. You probably wantmov %rsp, %rbpinstead.Due to historical reasons, some assemblers consider operands to be left-to-right while others take them right-to-left.