oorosco
oorosco

Reputation: 246

Need help understanding Assembly interaction with stack

Okay, so here's the deal. To my understanding you can do something like such with the stack in assembly:

push 5
push 6

Okay so now we have on the stack: 6 5

so

pop eax

would put 6 into eax correct? However, what if we wished to grab 5 or some value pushed onto the stack before 6 or 5. How would we grab it? (without popping top values off)

I looked at this : How does the stack work in assembly language? And it seems that you cannot access things based on address in the stack in assembly. However that doesn't seem to make much sense to me. Or is there a way to iterate through the stack without "popping" things off the top. Thanks, sorry for the "silly" question!

Upvotes: 2

Views: 150

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 992747

The stack is a memory location just like any other. The ESP register points to the current top of the stack (the stack grows downward in memory, so a push instruction will decrease the ESP register).

You can access specific values on the stack by indexing from a register:

mov ebp,esp
mov eax,[ebp+4]

(If I remember correctly, you can't use offset addressing modes with ESP directly, but you can with EBP.)

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

The stack pointer (which is manipulated by push and pop) is just a register; esp. You can offset that register to access things relative to the current top of the stack.

Upvotes: 0

Related Questions