flat assembler
Message board for the users of flat assembler.

Index > Windows > 18 byte dynamic retn

Author
Thread Post new topic Reply to topic
r22



Joined: 27 Dec 2004
Posts: 805
r22 28 Jan 2005, 05:43
Did you ever want to make a function that can take a variable amount of arguments? Example wsprintf - but it doesn't clean the stack for you. This code puts ESP back where it belongs.

Code:
Start:
        push 1
        push 1
        push 1
        push 1
        push 1
        push 1
        push 7 ;# of arguments
        call dynamicreturn
        push 0
        call [ExitProcess]

dynamicreturn:
        push ebp
        mov ebp,esp
        mov esp,ebp
        pop ebp ;dynamic return starts
        shl dword[esp+4],2 ;#of args to #of dwords
        mov ecx,[esp]      ;save return addr
        mov edx,[esp+4]    ;adjust esp to
        lea esp,[esp+edx+4];preCall position
        jmp ecx            ;jmp return addr      


You don't even need an arg for the number of arguments IF one of your args is say a format string you can have your function store how many args in a local from the stack frame.

I hope you all enjoy this as much as I enjoyed coding it.
Post 28 Jan 2005, 05:43
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 28 Jan 2005, 09:46
You shouldn't use this approach for stuff where speed matters, as P4 architecture (perhaps other as well) has a CALL/RET optimization...
Post 28 Jan 2005, 09:46
View user's profile Send private message Visit poster's website Reply with quote
Frank



Joined: 17 Jun 2003
Posts: 100
Frank 28 Jan 2005, 13:03
Simpler:

Code:
Start:
        push 1
        push 1
        push 1
        push 1
        push 1
        push 1
        push 6 ;# of arguments, excluding this one
        call dynamicreturn
        push 0
        call [ExitProcess]

dynamicreturn:
        pop   edx
        pop   ecx
        ; code goes here, ESP points to parameters, ECX and EDX must not be changed
        lea   esp, [esp + ecx*4]
        push  edx
        ret
    
Post 28 Jan 2005, 13:03
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 28 Jan 2005, 20:53
its just an amusing peice of code, that's pretty much it
Post 28 Jan 2005, 20:53
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.