Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 where stack pointer points?

For example if I pushed ax is [SP] points to my value of ax or the word after ax? Also is it differs from real mode to protected mode? I ask this because the Art of assembly book illustrates and explains as the sp points to last pushed data, and on this page OSDev Wiki - Stack it illustrated as it points to empty word after last pushed data.

like image 468
user1180619 Avatar asked Dec 27 '12 19:12

user1180619


People also ask

Where are stack pointer points?

The stack pointer always points to the item that is currently at the top of the stack. A push operation pre-decrements the stack pointer before storing an item on the stack. Hence the program initializes the stack pointer to point one item beyond the highest numbered element in the array that makes up the stack.

Where are the stack and stack pointer located?

Regardless of the approach, the stack pointer is always located at the top of the stack, providing a reliable starting point for accessing stack memory.

Where the top of the stack is on x86?

Intel's x86 architecture places its stack "head down". It starts at some address and grows down to a lower address. Here's how it looks: So when we say "top of the stack" on x86, we actually mean the lowest address in the memory area occupied by the stack.

Which register is the stack pointer x86?

Stack registers in x86 In 8086, the main stack register is called stack pointer - SP. The stack segment register (SS) is usually used to store information about the memory segment that stores the call stack of currently executed program.


3 Answers

Wikipedia says here:

The stack is implemented with an implicitly decrementing (push) and incrementing (pop) stack pointer. In 16-bit mode, this implicit stack pointer is addressed as SS:[SP], in 32-bit mode it is SS:[ESP], and in 64-bit mode it is [RSP]. The stack pointer actually points to the last value that was stored, under the assumption that its size will match the operating mode of the processor (i.e., 16, 32, or 64 bits) to match the default width of the push/pop/call/ret instructions.

This is the way my way-back memory says it works, too.

like image 106
Lee Meador Avatar answered Nov 01 '22 21:11

Lee Meador


push eax

Is equivalent to:

sub esp, 4
mov [esp], eax

So after a push, esp will hold the address of the pushed value.

like image 36
Cory Nelson Avatar answered Nov 01 '22 19:11

Cory Nelson


As per Lee Meador's and Cory Nelson's answers, the stack pointer points on the last value that was pushed.

From the Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2 (2A, 2B & 2C): Instruction Set Reference, A-Z, the first line from the description of the PUSH instruction reads as follow:

Decrements the stack pointer and then stores the source operand on the top of the stack.

like image 22
Philippe Aubertin Avatar answered Nov 01 '22 20:11

Philippe Aubertin