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 registers DX and AX with the most
    significant word in register DX.

    Example:

         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret4_
         Ret4_   proc    near   ; long int Ret4()
                 mov     AX,word ptr CS:Val4+0
                 mov     DX,word ptr CS:Val4+2
                 ret
         Val4    dd      7777777
         Ret4_   endp
         _TEXT   ends
                 end

 4. 8-byte values, except structures, are to be returned in registers AX,
    BX, CX and DX with the most significant word in register AX.

    Example:

                 .8087
         _TEXT   segment byte public 'CODE'
                 assume  CS:_TEXT
                 public  Ret8_
         Ret8_   proc    near   ; double Ret8()
                 mov     DX,word ptr CS:Val8+0
                 mov     CX,word ptr CS:Val8+2
                 mov     BX,word ptr CS:Val8+4
                 mov     AX,word ptr CS:Val8+6
                 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.  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.

 5. Otherwise, the caller allocates space on the stack for the return value
    and sets register SI to point to this area.  In a big data model,
    register SI 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     word ptr SS:0[SI],71
                 mov     word ptr SS:4[SI],72
                 mov     word ptr SS:8[SI],73
                 mov     word ptr SS:12[SI],74
                 mov     word ptr SS:16[SI],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 "ms"
or "mc" compiler option).

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