News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Confusion on push/pop

Started by unktehi, February 26, 2009, 03:49:55 AM

Previous topic - Next topic

unktehi

In my class I have the following example that represents the equation:
eax = (ecx * edx) / eax

These are the lines that they wrote to show how this is done:

push edx
(save edx for later)

push eax
(save eax for later)

mov eax, ecx
(save ecx in eax: eax = ecx)

imul edx
(multiply eax (now ecx) x edx)

pop ebx
(where did ebx come from? The last value that was pushed was eax, so if the instruction uses a different register does it assume the last thing that was pushed? Is ebx popping the value of what was formerly pushed as eax?)

idiv ebx
(This is dividing edx: eax (from 2 steps above) with ebx (which was the former value of eax before it was pushed?)

pop edx
Restore the edx value to the original 'pushed' value

Do I understand this correctly according to the comments under each instruction?

Farabi

Yes ebx is the value from the last push and it is eax.
Try to see it on debugger to understand it better.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

cmpxchg

Quote from: unktehi on February 26, 2009, 03:49:55 AM
imul edx
(multiply eax (now ecx) x edx)

imul edx
multiply eax by edx (eax x edx), ecx is not even touched and not changed

push edx
push eax
pop ebx       ebx = eax
pop edx       

the only requirement is to have  equal # of push & pop commands, 2 push & 2 pop (because push/pop modify esp register)
push/pop can be used to transfer value from one register to another, this is exactly what you code does

push eax - saves contet of eax in memory then any instruction can be used to read memory(no more dealing with eax)
pop  ebx - reads memory and transfers content to ebx
reason how pop knows where to read is esp register - your pointer to memory, actively used but the use is hidden

in you case here an alternative to 'pop ebx' would be:
mov  ebx, dword [esp]
add  esp, 4

unktehi

Thanks for confirming my understanding.  I appreciate it!