News:

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

How to Specify a Procedure Return Type

Started by msqweasm, May 28, 2011, 01:10:18 PM

Previous topic - Next topic

msqweasm

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.

RuiLoureiro

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

donkey

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.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

dedndave


msqweasm

So in assembly there is no "return type checking"?  I mean at least something like those provided by PROC Vs INVOKE

dedndave

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

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jj2007

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.