I am trying to follow along this tutorial on the stack in x86 assembly. It seemed to me that esp is a register containing a pointer to the top of the stack - and to test this out I dereferenced esp and tried to store its value in eax. This gave me a segmentation fault, and I cannot figure out why. With GDB, I was able to confirm that this dereference caused the error:
(gdb) disassemble
Dump of assembler code for function main:
0x0000000100000fa2 <+0>: pushq $0x32
=> 0x0000000100000fa4 <+2>: mov (%esp),%eax
0x0000000100000fa8 <+6>: mov $0x0,%rdi
0x0000000100000faf <+13>: mov $0x2000001,%rax
0x0000000100000fb6 <+20>: syscall
End of assembler dump.
(gdb)
But I cannot figure out why. Does anyone acquainted with the stack in x86 know what I am doing wrong?
How I am assembling: gcc -masm=intel access_stack_via_pointer.asm
It's also important to note that I am on MacOS.
.global _main
.text
_main:
push 50
# why the segmentation fault?
# eax should have 50 in it
mov eax, [esp]
mov rdi, 0
mov rax, 0x2000001
syscall