The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: msqweasm on May 28, 2011, 01:10:18 PM

Title: How to Specify a Procedure Return Type
Post by: msqweasm on May 28, 2011, 01:10:18 PM
A newbie question.  The MASM PROC directive allows us to specify parameters.  But what about return values?  How is return value implemented in assembly. 

Sorry I know this may sound trivial but I am still learning MASM assembly.
Title: Re: How to Specify a Procedure Return Type
Post by: RuiLoureiro on May 28, 2011, 01:26:42 PM
Quote from: msqweasm on May 28, 2011, 01:10:18 PM
A newbie question.  The MASM PROC directive allows us to specify parameters.  But what about return values?  How is return value implemented in assembly. 

Sorry I know this may sound trivial but I am still learning MASM assembly.

Before the ret instruction return the value in the register you want ( eax is used sometimes )
or return the pointer to the values in the memory

Sum     proc    A:DWORD, B:DWORD

        mov     eax, A
        mov     edx, B
        add     eax, edx        ; the result is in EAX
       
        ret
Sum     endp
Title: Re: How to Specify a Procedure Return Type
Post by: donkey on May 28, 2011, 01:50:52 PM
Hi,

The ABI specification for Win32 has any values returned in EDX:EAX. For 32 bit values regardless of type they are returned in EAX (EDX is not necessary) for 64 bit values the high DWORD is returned in EDX and the low in EAX. In normal circumstances strings and REALs are returned by a pointer in EAX though the convention calls for REAL numbers to be returned in ST(0) this is rarely done in practice. Since no flat Win32 API I know of returns a REAL the discrepancy between the convention and practice for their return doesn't have any effect so you can use whatever method you like internally. Since assembler is only concerned with data width (ie # of bytes) there is no necessity to cast the return value to a specific type, they all resolve to BYTE, WORD, DWORD or LARGE_INTEGER.
Title: Re: How to Specify a Procedure Return Type
Post by: dedndave on May 28, 2011, 02:31:55 PM
ECX may also be used   :P
Title: Re: How to Specify a Procedure Return Type
Post by: msqweasm on May 28, 2011, 03:11:14 PM
So in assembly there is no "return type checking"?  I mean at least something like those provided by PROC Vs INVOKE
Title: Re: How to Specify a Procedure Return Type
Post by: dedndave on May 28, 2011, 03:22:49 PM
none at all
the 3 available registers are dword size
in many cases, data is returned in a structure whose address was specified as an input parameter
if that's the case, a size override would be required to violate the size of a structure member
with assembly language, it is often the responsibilty of the programmer to get things right
in other words, there is an assumption that you know what you are doing
which is ok - it keeps feebs from writing assembler - lol
Title: Re: How to Specify a Procedure Return Type
Post by: hutch-- on May 29, 2011, 08:49:08 AM
msqweasm,

The convention in win32 for a return value is specifically in the EAX register. In assembler you can also return extra values in ECX and EDX and as mentioned before you return a 64 bit value in EAX:EDX. If you need more data than a single return in EAX and don't want to use ECX and EDX then you pass the address of a variable, array or structure and in your procedure you write the results you require to that address.

Most high level languages are specific to EAX as the return value so if you want to be able to interface with a high level language like C then you place your return value in EAX. It was mentioned that you can return floating point values in ST(0) but as it was already mentioned this is rarely ever used.
Title: Re: How to Specify a Procedure Return Type
Post by: jj2007 on May 29, 2011, 09:11:27 AM
Quote from: hutch-- on May 29, 2011, 08:49:08 AM
you can return floating point values in ST(0) but as it was already mentioned this is rarely ever used.

The GNU Scientific Library returns all its doubles in ST(0), see the gsl thread (http://www.masm32.com/board/index.php?topic=16725.0).