The MASM Forum

General => The Campus => Topic started by: xponential on March 09, 2014, 11:07:35 AM

Title: masm32 procedures
Post by: xponential on March 09, 2014, 11:07:35 AM
please can someone link me to a resource where i can learn to create
routines in masm32.
i'm sorry for such dumb request but i've searched the .chm files
in masm32/help but there isn't anything that explains it in details for a
newbie to grasp.
also, i'm asking out of curiousity, when calling a routine in masm32, im i gonna need to
first of all push all parameters onto the stack as explained in the c calling convention
seeing that routines are called using INVOKE(which has parameters embedded just like HLL's) rather than CALL?
Title: Re: masm32 procedures
Post by: dedndave on March 09, 2014, 11:23:54 AM
we generally use the StdCall convention, which is what windows API functions use
you can also use the C convention, if desired

if a function is typed the same way it is prototpyed, the assembler will balance the stack with either convention

Func1 PROTO C :DWORD,:DWORD

    .CODE

Start:
    INVOKE  Func1,Arg1,Arg2
    INVOKE  ExitProcess,0

Func1 PROC C Parm1:DWORD,Parm2:DWORD

    ret

Func1 ENDP

    END     Start


if "C" is omitted on the PROTO and PROC lines, the StdCall convention is used (default)
Func2 PROTO :DWORD,:DWORD

    .CODE

Start:
    INVOKE  Func2,Arg1,Arg2
    INVOKE  ExitProcess,0

Func2 PROC Parm1:DWORD,Parm2:DWORD

    ret

Func2 ENDP

    END     Start
Title: Re: masm32 procedures
Post by: dedndave on March 09, 2014, 11:31:29 AM
you can use the forum search tool, searching for "stack frame" and find several more in-depth discussions
Title: Re: masm32 procedures
Post by: xponential on March 09, 2014, 11:35:19 AM
thank you dendave, much appreciated
Title: Re: masm32 procedures
Post by: raymond on March 09, 2014, 02:03:11 PM
Remember that there is no absolute need to use the INVOKE instruction (which is essentially a macro and not a true assembly instruction) in masm, neither is the use of creating an official procedures for every routine (as with the C/C++ language) as compared to the simple CALL of the routine when there is no need to create a stack frame.

One of the reasons for writing a "procedure" would be if you intend to reuse that code with other programs and don't want to rewrite that same code everytime, specially if it is a complex one. Another reason may be if you need to call that code within the same program with a variety of parameters which you can then easily pass on the stack.