According to the "ART OF ASSEMBLY...." you should always put "ret" before endproc so that procedures that are written in a contigiouse manner don't run into one another. I have also seen many codings with "ret 0" before endproc. Three questions.

1. What is the difference?

2. Is the value in eax always returned if you just ret.

3. Does the processor do an automatic xor eax,eax if you "ret 0"

thanx
Posted on 2002-06-01 19:13:36 by titan
1. the value after the ret is the count added to the stack, once the return address has been popped into the EIP register.

2. the interpretation of the registers is based on the calling convention specified - you could return values in EBP if you wanted, but the Win32 API puts values in EAX. there is no instruction which specifically sets a return value.

3. { answered above }

the 'Calling Convention' is an agreement between programmers - part of the low-level interface between routines usually involving the stack (at least a return address is stored there).
Posted on 2002-06-01 21:37:58 by bitRAKE
if you dont use ret at the end of your function/procedure it wont return back to the orginal address you called your function from.

when you do a ret, the value in eax is preserved. i usually store a handle or something in eax before i return from my function/procedure.
Posted on 2002-06-01 22:13:52 by smurf
ret and ret n generate different opcodes
Posted on 2002-06-02 02:10:12 by eet_1024
CALL and RET are generally used in pairs.

CALL = push return address and jump to address.
RET = jump to address on stack and pop stack.

Bitrake has explained what happens when you specify a number after RET.

Neither CALL or RET change the content of EAX. EAX is used by convention as the container of the return value that is 32 bit in size.

If you want to specify the content of EAX in a procedure, you must do it BEFORE you call RET.

Regards,

hutch@movsd.com
Posted on 2002-06-02 06:19:42 by hutch--
Lately I often use proc without ret :)
Using proc statement just to make included lbls local ;)
Posted on 2002-06-02 06:50:11 by The Svin
Well that was interesting.

So i guess the "ret" before endproc is not the same as ending a macro

EXITM <eax> ; here return the value in eax
ENDM
Posted on 2002-06-02 09:41:43 by titan

Lately I often use proc without ret :)
Using proc statement just to make included lbls local ;)
Same here Svin - very handy feature.
You can use PROCs in PROCs, too. :)
Posted on 2002-06-02 10:42:30 by bitRAKE
Originally posted by The Svin
Lately I often use proc without ret


Curious... every time I forget to put a ret at the end of my procs, I get a GPF.
Posted on 2002-06-02 11:07:28 by Ernie
Ernie,
It's mostly relaited not to a procedure in general meaning (that is part of code you jump to using call opcode and jmp out (return) using retn) but to
proc \ endp macro.
You see, you can use it anywere you want including part of code between
name proc\name endp statements.
One of possible advantages I meant above is that lables names is seen inside the to proc\endp statements that makes possible to make label name local for any piece of code.
Posted on 2002-06-02 11:32:31 by The Svin

Curious... every time I forget to put a ret at the end of my procs, I get a GPF.
	OPTION PROLOGUE:NONE

OPTION EPILOGUE:NONE

mpn_com_n PROC PUBLIC FORCENOFRAME, x1:DWORD, x2:DWORD
; this PROC doesn't generate any code, but we
; still get the assemble-time features of MASM:
; - labels within the PROC are scoped
; - invoke still works with proc name
mpn_com_n ENDP

OPTION PROLOGUE:DEFAULTOPTION
OPTION EPILOGUE:DEFAULTOPTION
Posted on 2002-06-02 11:35:23 by bitRAKE
ret = C3
ret 0 = C2 00 00
Posted on 2002-06-02 12:52:57 by f0dder
Try that with a proc with parameters. Then the alone 'ret' will become 'ret VALUE-TO-ADD-TO-STACK'.
Posted on 2002-06-03 01:14:36 by gliptic
that's because ret is a macro in masm - use "retn" if you want to
force the C3 opcode.
Posted on 2002-06-03 05:18:37 by f0dder

Same here Svin - very handy feature.
You can use PROCs in PROCs, too. :)


Lately I use macro redifinition in a funny way to
place local lbls where I want.
Like this:


LocLbl macro

LOCAL again,lbl
mov ecx,2

.data
lbl db 'LocLbl1',0
.code
again: push ecx
invoke MessageBox,0,offset lbl,0,0
pop ecx
loop again
endm
LocLbl
LocLbl macro
LOCAL again,lbl
mov ecx,3
.data
lbl db 'LockLbl2',0
.code
again: push ecx
invoke MessageBox,0,offset lbl,0,0
pop ecx
loop again
endm
LocLbl


So I right in code write new macro with the same name
place different code inside with local labels I want, and
call the macro right after deffenition.
When I want place some local lbls for a new code block
I redefine macro again, write block of code inside the macro
and call it , and so on.
This way all code might in macros instead of section source.
And local lbls inside the current declaration of macro.
Name always the same :)
Posted on 2003-08-11 12:49:57 by The Svin