Hello all. This is a quick question. I have learned quite a bit of assembly so far. One thing confused me though. Why would one push a register with nothing in it .

for example

push eax

I have see this happen in code even when nothing has been moved into it like. How would pushing an empty register result in anything. Any help please and thanks.

Hello all. This is a quick question. I have learned quite a bit of assembly so far. One thing confused me though. Why would one push a register with nothing in it .

for example

push eax

I have see this happen in code even when nothing has been moved into it like. How would pushing an empty register result in anything. Any help please and thanks.

One reason you might do this is to save the value of the register so you can restore it later. In this case, you'd see a matching pop eax later in the code. This usually happens when you're about to do something that will change the value in the register, but you don't want to confuse later code that might assume you haven't changed it. This is common when writing procedures.

gusano79> In this case, you'd see a matching pop eax later in the code.

Yes, it all depends on where you find the POP.
It could simply be a fake value for a parameter in a function call or it could send the program a different route depending on what it's popped into.

Assembly programmers use push and pop for many reasons. The most common reasons would be, as mentioned above, to save values on the stack for later use, safety from functions disrupting those values (when you use a call instruction, one should assume that all registers have changed afterwards), and when using the C Calling Convention (push the arguments for the function in reverse order, and then call the function). Pushing a register that has no value (null) can simply be used for creating room on the stack for local variables as well (although just subtracting 4 from the stack pointer would do just fine), which could later be accessed through different addressing modes using the base pointer.

If you don't understand what push and pop do, well then you should first know that push inserts something on the top of the stack, and pop takes the value on the top of the stack off of the stack. To understand this better, you should learn more about the stack first.

I hope that helped.

Edited 6 Years Ago by theifyppl: n/a

This article has been dead for over six months. Start a new discussion instead.