|
|
coherent
calling conventions Technical Information calling conventions
The following presents the calling conventions for COHERENT.
The design of the calling conventions had to take into account
the fact that C does not require that the number of arguments
passed to a function be the same as the number of arguments
specified in the function's declaration. Routines with a vari-
able number of arguments are not uncommon; for example, printf
and scanf can take a variable number of arguments. Another con-
sideration was the availability of register variables.
Therefore, COHERENT uses the following calling sequence. The
function arguments are pushed onto the stack from the first, or
rightmost, through the last, or leftmost. lloonnggs are pushed high-
half first; this makes the word order compatible with the dddd
instruction. The function is then called with a near call. An
add instruction after the call removes the arguments from the
stack.
For example, the function call
int a;
long b;
char c;
foo()
{
example(a, b, c);
}
generates the code
movb al,c
cbw
push ax
push b+2
push b
push a
call example_
add sp,8
Note that an underbar character `_' has been appended to the
function name. This serves two purposes. First, it makes it
harder to accidentally call routines written in other languages.
Second, it means that two routines with the same name can be
called from C and another language in identical fashions.
The parameters and local variables in the called function are
referenced as offsets from the bp register. The arguments begin
at offset 8 and continue toward higher addresses, whereas the
local variables begin at offset -2 and continue toward lower ad-
dresses.
COHERENT Lexicon Page 1
calling conventions Technical Information calling conventions
The sp register points the local variable with the lowest ad-
dress. Thus, when eexxaammppllee_ is reached in the above model, the
stack frame resembles the following:
High ZDDDDDDDDDDDDDDDDDDDDDDD?
3 c (widened to a word) 3
CDDDDDDDDDDDDDDDDDDDDDDD4
3 high half of b 3
CDDDDDDDDDDDDDDDDDDDDDDD4
3 low half of b 3
CDDDDDDDDDDDDDDDDDDDDDDD4
3 a 3
Low @DDDDDDDDDDDDDDDDDDDDDDDY
Functions return iinntts in the ax register, lloonnggs in the dx:ax
register pair, pointers in the ax register and ddoouubbllees in ffppaacc_.
The following program
example(a, b, c)
int a, b, c;
{
return (a * b - c);
}
when compiled with the -VVAASSMM option, produces the following as-
sembly-language code:
.shri
.globl example_
example_:
push si
push di
push bp
mov bp, sp
mov ax, 10(bp)
imul 8(bp)
sub ax, 12(bp)
pop bp
pop di
pop si
ret
The runtime startup initializes the registers cs, ds, es, and ss,
and the segment registers remain unchanged. Other registers may
be overwritten.
COHERENT Lexicon Page 2
calling conventions Technical Information calling conventions
COHERENT pushes function arguments as follows.
cchhaarr Widened to iinntt, then pushed
iinntt Pushed in machine word order
lloonngg Pushed high order word, then low-order word
ffllooaatt Widened to ddoouubbllee, then pushed
ddoouubbllee Pushed high order, then low order
ssttrruucctt Pushed in memory order
uunniioonn Pushed in memory order
Functions return values as follows:
cchhaarr In al
iinntt In ax
lloonngg In dx:ax
ffllooaatt Same as ddoouubbllee
ddoouubbllee In ffppaacc_
ssttrruucctt Pointer in ax
uunniioonn Pointer in ax
pointerIn ax
A function that returns a struct or union actually returns a
pointer; the code generated for the function call block-moves the
result to its destination Functions that return a float or double
return it in the global double ffppaacc_.
For example, consider the call
example(i, l, c, cp);
where i is an int, l is a long, c is a char, cp is a pointer to a
char, and example declares two automatic iinntts. After execution
of the call and the prologue of example, the stack contains the
following 11 words:
High ZDDDDDDDDDDDDDDDDDD?
3 cp 3
CDDDDDDDDDDDDDDDDDD4
3 c 3
CDDDDDDDDDDDDDDDDDD4
3 high word of l 3
CDDDDDDDDDDDDDDDDDD4
3 low word of l 3
CDDDDDDDDDDDDDDDDDD4
3 i 3
CDDDDDDDDDDDDDDDDDD4
3 return address 3
COHERENT Lexicon Page 3
calling conventions Technical Information calling conventions
CDDDDDDDDDDDDDDDDDD4
3 saved SI 3
CDDDDDDDDDDDDDDDDDD4
3 saved DI 3
CDDDDDDDDDDDDDDDDDD4
3 saved BP 3
CDDDDDDDDDDDDDDDDDD4
3 space for auto 1 3
CDDDDDDDDDDDDDDDDDD4
3 space for auto 2 3
Low @DDDDDDDDDDDDDDDDDDY
The following example performs a simple function call:
main()
{
example(1, 2);/* call sample routine */
}
example(p1, p2)
{
int a, b;
a = 3;
b = 4;
}
When the function eexxaammppllee is about to return, the stack appears
as follows:
High ZDDDDDDDDDDDDDDDDD?
3 2 3^ ppaarrmm 22 1100(bbpp)
CCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD44
33 11 33^ parm 1 8(bp)
CDDDDDDDDDDDDDDDDD4
3 Return Address: 3
3 2 words in 3
3 LARGE model, 3
3 1 in SMALL model3 6(bp)
CDDDDDDDDDDDDDDDDD4
3 main's SI 3 4(bp)
CDDDDDDDDDDDDDDDDD4
3 main's DI 3 2(bp)
CDDDDDDDDDDDDDDDDD4
3 main's BP 3 (bp)
CDDDDDDDDDDDDDDDDD4
3 3 3^ aa -22(bbpp)
CCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD44
LLooww 33 44 33^ SP b -4(bp)
@DDDDDDDDDDDDDDDDDY
COHERENT Lexicon Page 4
calling conventions Technical Information calling conventions
***** See Also *****
C language, technical information
COHERENT Lexicon Page 5
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.