The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Ksbunker on May 27, 2009, 06:16:25 AM

Title: C-calling convention PLUS balancing stack
Post by: Ksbunker on May 27, 2009, 06:16:25 AM
I have a function "FormatString()" with in theory an unlimited number of parameters, however it is called using the c-calling convention so the stack has to be manually balanced after call.

In the example below, the function takes 3 parameters (therefore 3h*4h=0Ch) and the stack requires "add esp, 0Ch"

Is there any means of automating the stack balancing after the Call. Using macro's comes to mind, but im not sure of their exact usage in this situation.

I did read that the INVOKE macro did this, but im not 100% sure.

Any help appreciated.

.data
  szFormat BYTE "%s %s", 0
  sz1 BYTE "String1", 0
  sz2 BYTE "String2", 0
.code
  push OFFSET sz2
  push OFFSET sz1
  push OFFSET szFormat
  call FormatString
  add esp, 0Ch
Title: Re: C-calling convention PLUS balancing stack
Post by: sinsi on May 27, 2009, 06:22:05 AM
Just prototype it before you use it -
FormatString PROTO C :VARARG
Then using invoke will clean the stack up for you.
Title: Re: C-calling convention PLUS balancing stack
Post by: Ksbunker on May 27, 2009, 06:24:01 AM
Many thanks and fast reply!
Title: Re: C-calling convention PLUS balancing stack
Post by: MichaelW on May 27, 2009, 06:34:19 AM
I see that sinsi provided the answer, but since I bothered to code this I'll post it anyway.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
varargs proc C p:DWORD, v:VARARG
    ret
varargs endp
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke varargs, 1, 2
    print uhex$(esp),13,10
    invoke varargs, 1, 2, 3
    print uhex$(esp),13,10
    invoke varargs, 1, 2, 3, 4
    print uhex$(esp),13,10
    invoke crt_printf, chr$("%d %d %c"), 1, 2, 10
    print uhex$(esp),13,10,13,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


00401005 6A02                   push    2
00401007 6A01                   push    1
00401009 E8F2FFFFFF             call    fn_00401000
0040100E 83C408                 add     esp,8
. . .
00401030 6A03                   push    3
00401032 6A02                   push    2
00401034 6A01                   push    1
00401036 E8C5FFFFFF             call    fn_00401000
0040103B 83C40C                 add     esp,0Ch
. . .

Title: Re: C-calling convention PLUS balancing stack
Post by: sinsi on May 27, 2009, 06:59:19 AM
I wasn't sure whether to do "proto c :dword, :vararg" so I cheated and looked at wsprintf in user.inc  :bdg
Title: Re: C-calling convention PLUS balancing stack
Post by: mitchi on May 27, 2009, 02:57:59 PM
I think that this is the best calling convention for 32 bit Windows  :U
Title: Re: C-calling convention PLUS balancing stack
Post by: Jimg on May 27, 2009, 03:03:59 PM
I like syscall better.  I avoid C as much as possible :bg
Title: Re: C-calling convention PLUS balancing stack
Post by: mitchi on May 27, 2009, 03:18:18 PM
syscall? That's on Linux no?
Title: Re: C-calling convention PLUS balancing stack
Post by: Jimg on May 27, 2009, 03:37:04 PM
Maybe, but it's also in masm.  It doesn't prepend a worthless underline character.
Title: Re: C-calling convention PLUS balancing stack
Post by: mitchi on May 27, 2009, 04:24:39 PM
Ah, sorry I was confused by another meaning of syscall I know.
Yes, syscall convention is good! ;)
Title: Re: C-calling convention PLUS balancing stack
Post by: Vortex on May 27, 2009, 04:50:59 PM
The syscall calling convention is useful to define symbols with no decoration.