News:

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

Using PUSH and POP with CALL

Started by georgek01, July 07, 2005, 08:52:07 AM

Previous topic - Next topic

georgek01

Hi there,

An elementary question...

When using PUSH, will the called function automatically POP the variables from the stack and fill the parameters, or do I have to match each PUSH with a corresponding POP inside the function?

e.g.



MyProc PROTO :DOWRD,:DWORD,:DWORD

...

PUSH 0CH
PUSH 0BH
PUSH 0AH

CALL MyProc

...

MyProc proc var1:DWORD, var2:DWORD, var3:DWORD

    ; var1 = 0AH
    ; var2 = 0BH
    ; var3 = 0CH

MyProc EndP



What we have to learn to do, we learn by doing.

- ARISTOTLE (384-322 BC)

sluggy

No, you need to "balance" the stack yourself, by adding to esp the number of bytes occupied by the parameters you passed. An easier alternative is to use the invoke statement (macro), when you do this MASM will insert the PUSHes, POPs and do the stack balancing for you. And also don't forget the inbuilt ret macro, ret 12 will adjust the stack pointer by 12 bytes (3 DWORDs) before returning from the function.

Tedd

Assuming the defualt calling convention in your program is stdcall (.model stdcall), then when you get to a 'ret' within a proc, masm will secretly replace that with "ret 12" (or whatever value is required to clean up the stack properely for that proc.)
So in your example, the call to MyProc is fine, and assuming MyProc will RET at some point (why wouldn't it?) then the stack will cleaned up 'for you' at that point.
Procs that are of C type require the caller to clean it up instead, so then you would have to pop your arguments back off the stack after the call. Though repeated popping is the long way to do it, so you can just "add esp,12" (or whatever value) to do it all in one go :wink
No snowflake in an avalanche feels responsible.

Jeff

i think he was referring to using the arguments passed into the procedure.  :)

it isnt necessary to "fill in the parameters", just use the names like you would for any variable.

MyProc proc var1:DWORD, var2:DWORD, var3:DWORD
    MOV eax,var1
    MUL var2
    etc, etc, etc.
    RET
MyProc EndP