Pelles C forum

Assembly language => Assembly discussions => Topic started by: Vortex on May 25, 2026, 12:46:11 PM

Title: Variadic functions
Post by: Vortex on May 25, 2026, 12:46:11 PM
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