Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ User's Guide - the way in which function values are returned depends on the size of the http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
The way in which function values are returned depends on the size of the
return value.  The following examples describe how function values are to be
returned.  They are coded for a small code model.

 1. 1-byte values are to be returned in register AL.

    Example:

         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret1_
         Ret1_   proc    near   ; char Ret1()
                 mov     AL,'G'
                 ret
         Ret1_   endp
         _TEXT   ends
                 end

 2. 2-byte values are to be returned in register AX.

    Example:

         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret2_
         Ret2_   proc    near   ; short int Ret2()
                 mov     AX,77
                 ret
         Ret2_   endp
         _TEXT   ends
                 end

 3. 4-byte values are to be returned in register EAX.

    Example:

         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret4_
         Ret4_   proc    near   ; int Ret4()
                 mov     EAX,7777777
                 ret
         Ret4_   endp
         _TEXT   ends
                 end

 4. 8-byte values, except structures, are to be returned in registers EDX
    and EAX.  When using the "fpc" (floating-point calls) option, "float"
    and "double" are returned in registers.  See section "Returning Values
    in 80x87-based Applications" when using the "fpi" or "fpi87" options.

    Example:

                 .8087
         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret8_
         Ret8_   proc    near   ; double Ret8()
                 mov     EDX,dword ptr CS:Val8+4
                 mov     EAX,dword ptr CS:Val8
                 ret
         Val8:   dq      7.7
         Ret8_   endp
         _TEXT   ends
                 end

    The ".8087" pseudo-op must be specified so that all floating-point
    constants are generated in 8087 format.

 5. Otherwise, the caller allocates space on the stack for the return value
    and sets register ESI to point to this area.  In a big data model,
    register ESI contains an offset relative to the segment value in segment
    register SS.

    Example:

         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  RetX_
         ;
         ; struct int_values {
         ;     int value1, value2, value3, value4, value5;
         ;                   };
         ;
         RetX_   proc    near ; struct int_values RetX()
                 mov     dword ptr SS:0[ESI],71
                 mov     dword ptr SS:4[ESI],72
                 mov     dword ptr SS:8[ESI],73
                 mov     dword ptr SS:12[ESI],74
                 mov     dword ptr SS:16[ESI],75
                 ret
         RetX_   endp
         _TEXT   ends
                 end

    When returning values on the stack, remember to use a segment override
    to the stack segment (SS).

The following is an example of a Watcom C/C++ program calling the above
assembly language subprograms.


     #include <stdio.h>

     struct int_values {
         int value1;
         int value2;
         int value3;
         int value4;
         int value5;
     };


     extern  char              Ret1(void);
     extern  short int         Ret2(void);
     extern  long int          Ret4(void);
     extern  double            Ret8(void);
     extern  struct int_values RetX(void);


     void main()
     {
         struct int_values x;

         printf( "Ret1 = %c\n", Ret1() );
         printf( "Ret2 = %d\n", Ret2() );
         printf( "Ret4 = %ld\n", Ret4() );
         printf( "Ret8 = %f\n", Ret8() );
         x = RetX();
         printf( "RetX1 = %d\n", x.value1 );
         printf( "RetX2 = %d\n", x.value2 );
         printf( "RetX3 = %d\n", x.value3 );
         printf( "RetX4 = %d\n", x.value4 );
         printf( "RetX5 = %d\n", x.value5 );
     }

The above function should be compiled for a small code model (use the "mf",
"ms" or "mc" compiler option).

+--------------------------------------------------------------------------+
|   Note:  Returning values from functions in the stack-based calling      |
| convention is the same as returning values from functions in the         |
| register-based calling convention when using the "fpc" option.           |
+--------------------------------------------------------------------------+

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson