News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Proc

Started by robaz, June 08, 2012, 04:44:00 PM

Previous topic - Next topic

robaz

How can i call a proc with parameters?

BogdanOntanu

invoke proc_name, parameter1, parameter2,offset parameter3, addr parameter4, [parameter5], etc
Ambition is a lame excuse for the ones not brave enough to be lazy, www.oby.ro

dedndave

near the beginning of the file, you want a PROTOtype
MyFunc  PROTO   :DWORD,:DWORD,:DWORD
that tells the assembler how many and what size the parameters are
for 32-bit programs, it is best if they are all DWORD sized parms

in the program, you call the function as boggie suggested...
        INVOKE  MyFunc,Parameter3,Parameter2,Parameter1
i numbered the parameters that way because that is the order they are pushed onto the stack

then, to write the function...
MyFunc  PROC    Parm3:DWORD,Parm2:DWORD,Parm1:DWORD

        mov     eax,Parm1
        mov     ecx,Parm2
        mov     edx,Parm3
;
;
;
        mov     eax,ReturnValue
        ret

MyFunc  ENDP


if your routine uses EBX, ESI, or EDI, it should be preserved
the assembler will do it for you
let's say we were going to use all 3...
MyFunc  PROC    USES EBX ESI EDI Parm3:DWORD,Parm2:DWORD,Parm1:DWORD
in most cases, you shouldn't use EBP because the assembler uses that register as the stack frame base pointer and needs it to reference the parms

Ghandi

Quotein most cases, you shouldn't use EBP because the assembler uses that register as the stack frame base pointer and needs it to reference the parms

If you have a need for them, you can use any/all of the 8 general purpose registers AS general purpose registers, as long as you're not using direct named referencing of parameters or locals once EBP has been changed. An MMX register will store ESP after pushing all other registers you wish to preserve, then move it back and pop any values pushed.

1. Preserve any registers required by pushing them to stack, EBP last.
2. Access procedure parameters by loading to GP registers.
3. Save ESP to MMX register with MOVD MMn, ESP.
4. Perform operations on data given to procedure.
5. Restore ESP from MMX.
6. Pop EBP, save results to temp locals now accessible once again.
7. Restore other registers from stack, set return value(s) and exit.

HR,
Ghandi

dedndave

...or you can balance the stack and just PUSH/POP EBP   :P

Vortex

Hi robaz,

Here is a quick example  :

include     \masm32\include\masm32rt.inc

calculate   PROTO :DWORD,:DWORD

.data

msg         db '2x + y = %d',0

.code

start:

    invoke  calculate,10,80
    invoke  crt_printf,ADDR msg,eax
    invoke  ExitProcess,0


calculate PROC x:DWORD,y:DWORD

    mov     eax,x
    shl     eax,1
    add     eax,y
    ret                 ; eax = 2 * x + y

calculate ENDP

END start

Ghandi

Quote...or you can balance the stack and just PUSH/POP EBP   

This will only work if you don't use ESP for general purpose as well, meaning you go from 8 usable registers to 7.

I'm well aware that there will not be a great need for such procedures and there are many twists or variations on the same theme, i was just pointing out it can be done without a problem.

HR,
Ghandi