News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

C-calling convention PLUS balancing stack

Started by Ksbunker, May 27, 2009, 06:16:25 AM

Previous topic - Next topic

Ksbunker

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

sinsi

Just prototype it before you use it -
FormatString PROTO C :VARARG
Then using invoke will clean the stack up for you.
Light travels faster than sound, that's why some people seem bright until you hear them.

Ksbunker


MichaelW

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
. . .

eschew obfuscation

sinsi

I wasn't sure whether to do "proto c :dword, :vararg" so I cheated and looked at wsprintf in user.inc  :bdg
Light travels faster than sound, that's why some people seem bright until you hear them.

mitchi

I think that this is the best calling convention for 32 bit Windows  :U

Jimg

I like syscall better.  I avoid C as much as possible :bg

mitchi


Jimg

Maybe, but it's also in masm.  It doesn't prepend a worthless underline character.

mitchi

Ah, sorry I was confused by another meaning of syscall I know.
Yes, syscall convention is good! ;)

Vortex

The syscall calling convention is useful to define symbols with no decoration.