News:

Download Pelles C here: http://www.pellesc.se

Main Menu

Variadic functions

Started by Vortex, Today at 12:46:11 PM

Previous topic - Next topic

Vortex

Here is a variadic function example, the VaFunc function calculates the sum of DWORDs.

.386
.model flat,stdcall
option casemap:none

ExitProcess PROTO :DWORD
printf PROTO C :DWORD,:VARARG
VaFunc PROTO C :DWORD,:VARARG

.data

f db 'Sum = %u',0

.data?

retaddr dd ?
i       dd ?

.code

start:

    push    7
    push    6
    push    5
    push    3
    call    VaFunc

    invoke  printf,ADDR f,eax

    invoke  ExitProcess,0

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

VaFunc PROC C x:DWORD,y:VARARG

    pop     retaddr
    pop     edx
    xor     eax,eax
@@:
    pop     ecx
    add     eax,ecx
    inc     i
    cmp     edx,i
    jne     @b
   
    push    retaddr
    retn

VaFunc ENDP
   
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

END start
Code it... That's all...