|
|
1.1 root 1:
2:
3: calling conventions Technical Information calling conventions
4:
5:
6:
7: The following presents the calling conventions for COHERENT.
8:
9: The design of the calling conventions had to take into account
10: the fact that C does not require that the number of arguments
11: passed to a function be the same as the number of arguments
12: specified in the function's declaration. Routines with a vari-
13: able number of arguments are not uncommon; for example, printf
14: and scanf can take a variable number of arguments. Another con-
15: sideration was the availability of register variables.
16:
17: Therefore, COHERENT uses the following calling sequence. The
18: function arguments are pushed onto the stack from the first, or
19: rightmost, through the last, or leftmost. lloonnggs are pushed high-
20: half first; this makes the word order compatible with the dddd
21: instruction. The function is then called with a near call. An
22: add instruction after the call removes the arguments from the
23: stack.
24:
25: For example, the function call
26:
27:
28: int a;
29: long b;
30: char c;
31:
32: foo()
33: {
34: example(a, b, c);
35: }
36:
37:
38: generates the code
39:
40:
41: movb al,c
42: cbw
43: push ax
44: push b+2
45: push b
46: push a
47: call example_
48: add sp,8
49:
50:
51: Note that an underbar character `_' has been appended to the
52: function name. This serves two purposes. First, it makes it
53: harder to accidentally call routines written in other languages.
54: Second, it means that two routines with the same name can be
55: called from C and another language in identical fashions.
56:
57: The parameters and local variables in the called function are
58: referenced as offsets from the bp register. The arguments begin
59: at offset 8 and continue toward higher addresses, whereas the
60: local variables begin at offset -2 and continue toward lower ad-
61: dresses.
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: calling conventions Technical Information calling conventions
70:
71:
72:
73:
74: The sp register points the local variable with the lowest ad-
75: dress. Thus, when eexxaammppllee_ is reached in the above model, the
76: stack frame resembles the following:
77:
78:
79: High ZDDDDDDDDDDDDDDDDDDDDDDD?
80: 3 c (widened to a word) 3
81: CDDDDDDDDDDDDDDDDDDDDDDD4
82: 3 high half of b 3
83: CDDDDDDDDDDDDDDDDDDDDDDD4
84: 3 low half of b 3
85: CDDDDDDDDDDDDDDDDDDDDDDD4
86: 3 a 3
87: Low @DDDDDDDDDDDDDDDDDDDDDDDY
88:
89:
90: Functions return iinntts in the ax register, lloonnggs in the dx:ax
91: register pair, pointers in the ax register and ddoouubbllees in ffppaacc_.
92: The following program
93:
94:
95: example(a, b, c)
96: int a, b, c;
97: {
98: return (a * b - c);
99: }
100:
101:
102: when compiled with the -VVAASSMM option, produces the following as-
103: sembly-language code:
104:
105:
106: .shri
107: .globl example_
108:
109:
110:
111: example_:
112: push si
113: push di
114: push bp
115: mov bp, sp
116: mov ax, 10(bp)
117: imul 8(bp)
118: sub ax, 12(bp)
119: pop bp
120: pop di
121: pop si
122: ret
123:
124:
125: The runtime startup initializes the registers cs, ds, es, and ss,
126: and the segment registers remain unchanged. Other registers may
127: be overwritten.
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: calling conventions Technical Information calling conventions
136:
137:
138:
139:
140: COHERENT pushes function arguments as follows.
141:
142:
143: cchhaarr Widened to iinntt, then pushed
144: iinntt Pushed in machine word order
145: lloonngg Pushed high order word, then low-order word
146: ffllooaatt Widened to ddoouubbllee, then pushed
147: ddoouubbllee Pushed high order, then low order
148: ssttrruucctt Pushed in memory order
149: uunniioonn Pushed in memory order
150:
151:
152: Functions return values as follows:
153:
154:
155: cchhaarr In al
156: iinntt In ax
157: lloonngg In dx:ax
158: ffllooaatt Same as ddoouubbllee
159: ddoouubbllee In ffppaacc_
160: ssttrruucctt Pointer in ax
161: uunniioonn Pointer in ax
162: pointerIn ax
163:
164:
165: A function that returns a struct or union actually returns a
166: pointer; the code generated for the function call block-moves the
167: result to its destination Functions that return a float or double
168: return it in the global double ffppaacc_.
169:
170: For example, consider the call
171:
172:
173: example(i, l, c, cp);
174:
175:
176: where i is an int, l is a long, c is a char, cp is a pointer to a
177: char, and example declares two automatic iinntts. After execution
178: of the call and the prologue of example, the stack contains the
179: following 11 words:
180:
181:
182: High ZDDDDDDDDDDDDDDDDDD?
183: 3 cp 3
184: CDDDDDDDDDDDDDDDDDD4
185: 3 c 3
186: CDDDDDDDDDDDDDDDDDD4
187: 3 high word of l 3
188: CDDDDDDDDDDDDDDDDDD4
189: 3 low word of l 3
190: CDDDDDDDDDDDDDDDDDD4
191: 3 i 3
192: CDDDDDDDDDDDDDDDDDD4
193: 3 return address 3
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
199:
200:
201: calling conventions Technical Information calling conventions
202:
203:
204:
205: CDDDDDDDDDDDDDDDDDD4
206: 3 saved SI 3
207: CDDDDDDDDDDDDDDDDDD4
208: 3 saved DI 3
209: CDDDDDDDDDDDDDDDDDD4
210: 3 saved BP 3
211: CDDDDDDDDDDDDDDDDDD4
212: 3 space for auto 1 3
213: CDDDDDDDDDDDDDDDDDD4
214: 3 space for auto 2 3
215: Low @DDDDDDDDDDDDDDDDDDY
216:
217:
218: The following example performs a simple function call:
219:
220:
221: main()
222: {
223: example(1, 2);/* call sample routine */
224: }
225:
226: example(p1, p2)
227: {
228: int a, b;
229:
230: a = 3;
231: b = 4;
232: }
233:
234:
235: When the function eexxaammppllee is about to return, the stack appears
236: as follows:
237:
238:
239: High ZDDDDDDDDDDDDDDDDD?
240: 3 2 3^ ppaarrmm 22 1100(bbpp)
241: CCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD44
242: 33 11 33^ parm 1 8(bp)
243: CDDDDDDDDDDDDDDDDD4
244: 3 Return Address: 3
245: 3 2 words in 3
246: 3 LARGE model, 3
247: 3 1 in SMALL model3 6(bp)
248: CDDDDDDDDDDDDDDDDD4
249: 3 main's SI 3 4(bp)
250: CDDDDDDDDDDDDDDDDD4
251: 3 main's DI 3 2(bp)
252: CDDDDDDDDDDDDDDDDD4
253: 3 main's BP 3 (bp)
254: CDDDDDDDDDDDDDDDDD4
255: 3 3 3^ aa -22(bbpp)
256: CCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD44
257: LLooww 33 44 33^ SP b -4(bp)
258: @DDDDDDDDDDDDDDDDDY
259:
260:
261:
262: COHERENT Lexicon Page 4
263:
264:
265:
266:
267: calling conventions Technical Information calling conventions
268:
269:
270:
271:
272: ***** See Also *****
273:
274: C language, technical information
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328: COHERENT Lexicon Page 5
329:
330:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.