What exactly is the base pointer and stack pointer? To what do they point?

esp is as you say it is, the top of the stack. ebp is usually set to esp at the start of the function. Function parameters and local variables are accessed by adding and subtracting, respectively, a constant offset from ebp. All x86 calling conventions define ebp as being preserved across function calls. ebp itself … Read more

Print out value of stack pointer

One trick, which is not portable or really even guaranteed to work, is to simple print out the address of a local as a pointer. void print_stack_pointer() { void* p = NULL; printf(“%p”, (void*)&p); } This will essentially print out the address of p which is a good approximation of the current stack pointer

movq (%rsp), %rsp assembly stack pointer load?

movq (assuming you’re talking about x86) is a move of a quadword (64-bit value). This particular instruction: movq (%rsp), %rsp looks very much like code that will walk up through stack frames. This particular instruction grabs the quadword pointed to by the current stack pointer, and loads it into the stack pointer, overwriting it. By … Read more

Base pointer and stack pointer

When the function is called, the stack looks like: +————-+ | Parameter 2 | +————-+ | Parameter 1 | +————-+ | Return Addr | <– esp +————-+ then after the “stack frame” is set up: +————-+ | Parameter 2 | <– [ebp + 12] +————-+ | Parameter 1 | <– [ebp + 8] +————-+ | … Read more

What is the function of the push / pop instructions used on registers in x86 assembly?

pushing a value (not necessarily stored in a register) means writing it to the stack. popping means restoring whatever is on top of the stack into a register. Those are basic instructions: push 0xdeadbeef ; push a value to the stack pop eax ; eax is now 0xdeadbeef ; swap contents of registers push eax … Read more