News:

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

Invoke

Started by 2-Bit Chip, November 24, 2009, 10:26:31 PM

Previous topic - Next topic

2-Bit Chip

I am trying to write a procedure that returns a value by pushing it onto the stack. But, it does not give me the correct value. Do I push the return value or move it onto eax for it work with invoke?

Thanks!


EDIT: Figured it out. Thanks anyways. :red

dedndave

#1
you could do it something like this

Procedure PROTO :DWORD
.
.
.
        INVOKE Procedure,DummyParameter
        pop     eax                     ;recover the parm and balance the stack
.
.
.
        OPTION  PROLOGUE:NONE
        OPTION  EPILOGUE:NONE

Procedure PROC DummyParameter:DWORD

        push    ebp
        mov     ebp,esp
        mov     eax,ReturnValue
        mov     [ebp+8],eax
.
.
.
        leave
        ret                       ;return, but do not pop the parm(s)

Procedure ENDP

        OPTION  PROLOGUE:PrologueDef
        OPTION  EPILOGUE:EpilogueDef

usually, the EAX, ECX, and EDX registers are used to return values

jj2007

Usually, just move a value into eax, followed by ret.
Dave's idea works, too:

include \masm32\include\masm32rt.inc

.code
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
MyTest proc OneArg
pop edx
pop eax ; OneArg
imul eax, eax, 100
push eax ; the value to be returned
jmp edx
MyTest endp

start:
invoke MyTest, 123
pop eax
print str$(eax), " returned"
getkey
exit
end start

2-Bit Chip

All invoke does is just make procedure-calling simpler; nothing more, nothing less?

jj2007

invoke is a life saver. Of course, you can push your arguments manually, and define LOCAL variables manually. But unless you have an awful lot of experience, and a need for an incredibly fast way to call some proc from an inner loop, the manual way is just a recipe for disaster. I only use manual pushpushcall if I want to have some fun :green

2-Bit Chip

Your code springs up another question of mine: what's up with the OPTION EPILOGUE:...?

hutch--

 :bg

> OPTION EPILOGUE

This is when you want to have some real fun, put aside soft easy options like stack frames and write your own stack frame free procedures. Not for the faint of heart.  :P
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Slugsnack

Quote from: jj2007 on November 24, 2009, 10:59:04 PM
invoke is a life saver. Of course, you can push your arguments manually, and define LOCAL variables manually. But unless you have an awful lot of experience, and a need for an incredibly fast way to call some proc from an inner loop, the manual way is just a recipe for disaster. I only use manual pushpushcall if I want to have some fun :green
REAL MEN extend the stack by the space for the arguments then mov [esp+X] !!

dedndave

QuoteREAL MEN extend the stack by the space for the arguments then mov [esp+X] !!

i am trying to see the advantage in that, Mike - lol
although, i played around with something like that using REP MOVSD to set the stack parameters from a list
i was just playing - lol

oex

We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

Slugsnack

Quote from: dedndave on November 24, 2009, 11:49:19 PM
QuoteREAL MEN extend the stack by the space for the arguments then mov [esp+X] !!

i am trying to see the advantage in that, Mike - lol
although, i played around with something like that using REP MOVSD to set the stack parameters from a list
i was just playing - lol
so a push usually extends the stack then moves something on right. well you can just do one of those extensions so you save clock cycles that way ? i don't know. i know that's how some compilers will generate their code though. i'm not actually sure if it's faster but i guess it makes sense, right ?

jj2007

Quote from: Slugsnack on November 25, 2009, 03:25:08 AM
but i guess it makes sense, right ?

Keep guessing :toothy
Real Men(TM) do timings and check code sizes. e.g. for mov xxx, [esp+x] vs. mov xxx, [ebp+x]
:wink

sinsi

This bit of code pop eax
pop ecx
...
jmp ecx
opened my eyes a bit...agner said call/ret are optimised.

Real Men (TM) don't have to worry about any register starting with 'e', we've got 64-bit danglies. :U
Oh, and no invoke, no stack frames, etc. Heaven on a (usb) stick...
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on November 25, 2009, 07:23:55 AM
This bit of code pop eax
pop ecx
...
jmp ecx
opened my eyes a bit...agner said call/ret are optimised.
jj & Lingo say the sequence is some cycles faster

> Real Masos (TM) don't have to worry about any register starting with 'e', we've got 64-bit danglies. :U

sinsi

I like ml64 because it is stripped down - I control everything (sorry goasm) and that's why I use asm.
I've said before that I don't even like using 'invoke' - heretic! burn him! - more abstraction.

'Masos' - is that 'masochists'? Umm, yeah, hit me. 'masos' sounds vaguely to do with sex...hang on...
Light travels faster than sound, that's why some people seem bright until you hear them.