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 - how arguments are passed to a function with register-based calling http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
How arguments are passed to a function with register-based calling
conventions is determined by the size (in bytes) of the argument and where
in the argument list the argument appears.  Depending on the size, arguments
are either passed in registers or on the stack.  Arguments such as
structures are almost always passed on the stack since they are generally
too large to fit in registers.  Since arguments are processed from left to
right, the first few arguments are likely to be passed in registers (if they
can fit) and, if the argument list contains many arguments, the last few
arguments are likely to be passed on the stack.

The registers used to pass arguments to a function are AX, BX, CX and DX.
 The following algorithm describes how arguments are passed to functions.

Initially, we have the following registers available for passing arguments:
AX, DX, BX and CX.  Note that registers are selected from this list in the
order they appear.  That is, the first register selected is AX and the last
is CX.  For each argument Ai, starting with the left most argument, perform
the following steps.

 1. If the size of Ai is 1 byte, convert it to 2 bytes and proceed to the
    next step.  If Ai is of type "unsigned char", it is converted to an
    "unsigned int".  If Ai is of type "signed char", it is converted to a
    "signed int".  If Ai is a 1-byte structure, the padding is determined by
    the compiler.

 2. If an argument has already been assigned a position on the stack, Ai
    will also be assigned a position on the stack.  Otherwise, proceed to
    the next step.

 3. If the size of Ai is 2 bytes, select a register from the list of
    available registers.  If a register is available, Ai is assigned that
    register.  The register is then removed from the list of available
    registers.  If no registers are available, Ai will be assigned a
    position on the stack.

 4. If the size of Ai is 4 bytes, select a register pair from the following
    list of combinations:  [DX AX] or [CX BX].  The first available register
    pair is assigned to Ai and removed from the list of available pairs.
     The high-order 16 bits of the argument are assigned to the first
    register in the pair; the low-order 16 bits are assigned to the second
    register in the pair.  If none of the above register pairs is available,
    Ai will be assigned a position on the stack.

 5. If the type of Ai is "double" or "float" (in the absence of a function
    prototype), select [AX BX CX DX] from the list of available registers.
     All four registers are removed from the list of available registers.
     The high-order 16 bits of the argument are assigned to the first
    register and the low-order 16 bits are assigned to the fourth register.
    If any of the four registers is not available, Ai will be assigned a
    position on the stack.

 6. All other arguments will be assigned a position on the stack.

Notes:

 1. Arguments that are assigned a position on the stack are padded to a
    multiple of 2 bytes.  That is, if a 3-byte structure is assigned a
    position on the stack, 4 bytes will be pushed on the stack.

 2. Arguments that are assigned a position on the stack are pushed onto the
    stack starting with the rightmost argument.

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