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 EAX, EBX, ECX and
EDX.  The following algorithm describes how arguments are passed to
functions.

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

 1. If the size of Ai is 1 byte or 2 bytes, convert it to 4 bytes and
    proceed to the next step.  If Ai is of type "unsigned char" or "unsigned
    short int", it is converted to an "unsigned int".  If Ai is of type
    "signed char" or "signed short int", it is converted to a "signed int".
    If Ai is a 1-byte or 2-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 4 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 type of Ai is "far pointer", select a register pair from the
    following list of combinations:  [EDX EAX] or [ECX EBX].  The first
    available register pair is assigned to Ai and removed from the list of
    available pairs.  The segment value will actually be passed in register
    DX or CX and the offset in register EAX or EBX.  If none of the above
    register pairs is available, Ai will be assigned a position on the
    stack.  Note that 8 bytes will be pushed on the stack even though the
    size of an item of type "far pointer" is 6 bytes.

 5. If the type of Ai is "double" or "float" (in the absence of a function
    prototype), select a register pair from the following list of
    combinations:  [EDX EAX] or [ECX EBX].  The first available register
    pair is assigned to Ai and removed from the list of available pairs.
     The high-order 32 bits of the argument are assigned to the first
    register in the pair; the low-order 32 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.

 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 4 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