comparison doc/manual/callconvs/callconv_x86.tex @ 328:276eb8c87aa0

- review and fixes, cleanup, amendments to calling convention appendix of manual
author Tassilo Philipp
date Fri, 22 Nov 2019 23:11:56 +0100
parents 63892f03e493
children 6e33db95e724
comparison
equal deleted inserted replaced
327:c0390dc85a07 328:276eb8c87aa0
1 %////////////////////////////////////////////////////////////////////////////// 1 %//////////////////////////////////////////////////////////////////////////////
2 % 2 %
3 % Copyright (c) 2007,2009 Daniel Adler <dadler@uni-goettingen.de>, 3 % Copyright (c) 2007-2019 Daniel Adler <dadler@uni-goettingen.de>,
4 % Tassilo Philipp <tphilipp@potion-studios.com> 4 % Tassilo Philipp <tphilipp@potion-studios.com>
5 % 5 %
6 % Permission to use, copy, modify, and distribute this software for any 6 % Permission to use, copy, modify, and distribute this software for any
7 % purpose with or without fee is hereby granted, provided that the above 7 % purpose with or without fee is hereby granted, provided that the above
8 % copyright notice and this permission notice appear in all copies. 8 % copyright notice and this permission notice appear in all copies.
23 \subsection{x86 Calling Conventions} 23 \subsection{x86 Calling Conventions}
24 24
25 25
26 \paragraph{Overview} 26 \paragraph{Overview}
27 27
28 On this processor, a word is defined to be 16 bits in size, a dword 32 bits
29 and a qword 64 bits.\\
30
28 There are numerous different calling conventions on the x86 processor 31 There are numerous different calling conventions on the x86 processor
29 architecture, like cdecl \cite{x86cdecl}, MS fastcall \cite{x86Winfastcall}, GNU 32 architecture, like cdecl \cite{x86cdecl}, MS fastcall \cite{x86Winfastcall}, GNU
30 fastcall \cite{x86GNUfastcall}, Borland fastcall \cite{x86Borlandfastcall}, Watcom 33 fastcall \cite{x86GNUfastcall}, Borland fastcall \cite{x86Borlandfastcall}, Watcom
31 fastcall \cite{x86Watcomfastcall}, Win32 stdcall \cite{x86Winstdcall}, MS thiscall 34 fastcall \cite{x86Watcomfastcall}, Win32 stdcall \cite{x86Winstdcall}, MS thiscall
32 \cite{x86Winthiscall}, GNU thiscall \cite{x86GNUthiscall}, the pascal calling 35 \cite{x86Winthiscall}, GNU thiscall \cite{x86GNUthiscall}, the pascal calling
33 convention \cite{x86Pascal} and a cdecl-like version for Plan9 \cite{x86Plan9} 36 convention \cite{x86Pascal} and a cdecl-like version for Plan9 \cite{x86Plan9}
34 (dubbed plan9call by us), etc.\\ 37 (dubbed plan9call by us), etc.\\
35 38
39 \begin{table}[h]
40 \begin{tabular*}{0.95\textwidth}{rccccc}
41 & \# of regs & \# regs to & & cleanup & 64bit args \\
42 Name & for params & \# preserve & push order & by & via regs? \\
43 \hline
44 cdecl & 0 & 4 & $\leftarrow$ & caller & - \\
45 MS fastcall & 2 & 4 & $\leftarrow$ & callee & Y \\
46 GNU fastcall & 2 & 4 & $\leftarrow$ & callee & N \\
47 Borland fastcall & 3 & 4 & $\rightarrow$ & callee & N \\
48 Watcom fastcall & 4 & 2-6 & $\leftarrow$ & callee & N \\
49 win32 stdcall & 0 & 4 & $\leftarrow$ & callee & - \\
50 MS thiscall & 1 & 4 & $\leftarrow$ & callee & N \\
51 GNU thiscall & 0 & 4 & $\leftarrow$ & caller & - \\
52 pascal & 0 & 4 & $\rightarrow$ & callee & - \\
53 plan9call & 0 & 0 & $\leftarrow$ & caller & - \\
54 \end{tabular*}
55 \caption{short x86 calling convention comparison}
56 \end{table}
57
36 58
37 \paragraph{\product{dyncall} support} 59 \paragraph{\product{dyncall} support}
38 60
39 Currently cdecl, stdcall, fastcall (MS and GNU), thiscall (MS and GNU) and 61 Currently cdecl, stdcall, fastcall (MS and GNU), thiscall (MS and GNU) and
40 plan9call are supported.\\ 62 plan9call are supported.\\
41 \\ 63 \\
64
65
66 \newpage
42 67
43 68
44 \subsubsection{cdecl} 69 \subsubsection{cdecl}
45 70
46 \paragraph{Registers and register usage} 71 \paragraph{Registers and register usage}
48 \begin{table}[h] 73 \begin{table}[h]
49 \begin{tabular*}{0.95\textwidth}{3 B} 74 \begin{tabular*}{0.95\textwidth}{3 B}
50 Name & Brief description\\ 75 Name & Brief description\\
51 \hline 76 \hline
52 {\bf eax} & scratch, return value\\ 77 {\bf eax} & scratch, return value\\
53 {\bf ebx} & permanent\\ 78 {\bf ebx} & preserve\\
54 {\bf ecx} & scratch\\ 79 {\bf ecx} & scratch\\
55 {\bf edx} & scratch, return value\\ 80 {\bf edx} & scratch, return value\\
56 {\bf esi} & permanent\\ 81 {\bf esi} & preserve\\
57 {\bf edi} & permanent\\ 82 {\bf edi} & preserve\\
58 {\bf ebp} & permanent\\ 83 {\bf ebp} & preserve\\
59 {\bf esp} & stack pointer\\ 84 {\bf esp} & stack pointer\\
60 {\bf st0} & scratch, floating point return value\\ 85 {\bf st0} & scratch, floating point return value\\
61 {\bf st1-st7} & scratch\\ 86 {\bf st1-st7} & scratch\\
62 \end{tabular*} 87 \end{tabular*}
63 \caption{Register usage on x86 cdecl calling convention} 88 \caption{Register usage on x86 cdecl calling convention}
64 \end{table} 89 \end{table}
65 90
66 91
67 \pagebreak
68
69 \paragraph{Parameter passing} 92 \paragraph{Parameter passing}
70 93
71 \begin{itemize} 94 \begin{itemize}
72 \item stack parameter order: right-to-left 95 \item stack parameter order: right-to-left
73 \item caller cleans up the stack 96 \item caller cleans up the stack
77 \paragraph{Return values} 100 \paragraph{Return values}
78 101
79 \begin{itemize} 102 \begin{itemize}
80 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register 103 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
81 \item integers \textgreater\ 32 bits are returned via the eax and edx registers 104 \item integers \textgreater\ 32 bits are returned via the eax and edx registers
105 \item return values \textgreater\ 64 bits (e.g. structures) are returned by the caller allocating the space and
106 passing a pointer to the callee as a new, implicit first parameter (this means, on the stack)
82 \item floating point types are returned via the st0 register (except on Minix, where they are returned as integers are) 107 \item floating point types are returned via the st0 register (except on Minix, where they are returned as integers are)
83 \end{itemize} 108 \end{itemize}
84 109
85 110
86 \paragraph{Stack layout} 111 \paragraph{Stack layout}
87 112
113 % verified/amended: TP nov 2019 (see also doc/disas_examples/x86.cdecl.disas)
88 Stack directly after function prolog:\\ 114 Stack directly after function prolog:\\
89 115
90 \begin{figure}[h] 116 \begin{figure}[h]
91 \begin{tabular}{5|3|1 1} 117 \begin{tabular}{5|3|1 1}
92 \hhline{~-~~} 118 & \vdots & & \\
93 & \vdots & & \\ 119 \hhline{~=~~}
94 \hhline{~=~~} 120 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
95 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 121 \hhline{~-~~}
96 \hhline{~-~~} 122 local data & & & \\
97 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 123 \hhline{~-~~}
124 \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
98 & \ldots & & \\ 125 & \ldots & & \\
99 & \ldots & & \\ 126 & arg 0 & & \\
100 \hhline{~-~~} 127 \hhline{~-~~}
101 & return address & & \\ 128 & return address & & \\
102 \hhline{~=~~} 129 \hhline{~=~~}
103 local data & & & \mrrbrace{3}{current frame} \\ 130 register save area & & & \mrrbrace{4}{current frame} \\
131 \hhline{~-~~}
132 local data & & & \\
104 \hhline{~-~~} 133 \hhline{~-~~}
105 parameter area & & & \\ 134 parameter area & & & \\
106 \hhline{~-~~} 135 \hhline{~-~~}
107 & \vdots & & \\ 136 & \vdots & & \\
108 \hhline{~-~~}
109 \end{tabular} 137 \end{tabular}
110 \caption{Stack layout on x86 cdecl calling convention} 138 \caption{Stack layout on x86 cdecl calling convention}
111 \end{figure} 139 \end{figure}
112 140
113 141
114 \pagebreak 142 \newpage
143
115 144
116 \subsubsection{MS fastcall} 145 \subsubsection{MS fastcall}
117 146
118 \paragraph{Registers and register usage} 147 \paragraph{Registers and register usage}
119 148
120 \begin{table}[h] 149 \begin{table}[h]
121 \begin{tabular*}{0.95\textwidth}{3 B} 150 \begin{tabular*}{0.95\textwidth}{3 B}
122 Name & Brief description\\ 151 Name & Brief description\\
123 \hline 152 \hline
124 {\bf eax} & scratch, return value\\ 153 {\bf eax} & scratch, return value\\
125 {\bf ebx} & permanent\\ 154 {\bf ebx} & preserve\\
126 {\bf ecx} & scratch, parameter 0\\ 155 {\bf ecx} & scratch, parameter 0\\
127 {\bf edx} & scratch, parameter 1, return value\\ 156 {\bf edx} & scratch, parameter 1, return value\\
128 {\bf esi} & permanent\\ 157 {\bf esi} & preserve\\
129 {\bf edi} & permanent\\ 158 {\bf edi} & preserve\\
130 {\bf ebp} & permanent\\ 159 {\bf ebp} & preserve\\
131 {\bf esp} & stack pointer\\ 160 {\bf esp} & stack pointer\\
132 {\bf st0} & scratch, floating point return value\\ 161 {\bf st0} & scratch, floating point return value\\
133 {\bf st1-st7} & scratch\\ 162 {\bf st1-st7} & scratch\\
134 \end{tabular*} 163 \end{tabular*}
135 \caption{Register usage on x86 fastcall (MS) calling convention} 164 \caption{Register usage on x86 fastcall (MS) calling convention}
136 \end{table} 165 \end{table}
137 166
138
139 \pagebreak
140
141 \paragraph{Parameter passing} 167 \paragraph{Parameter passing}
142 168
143 \begin{itemize} 169 \begin{itemize}
144 \item stack parameter order: right-to-left 170 \item stack parameter order: right-to-left
145 \item called function cleans up the stack 171 \item called function cleans up the stack
146 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments) 172 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments)
147 \item integer types 64 bits in size @@@ ? first in edx:eax ?
148 \item if first argument is a 64 bit integer, it is passed via ecx and edx 173 \item if first argument is a 64 bit integer, it is passed via ecx and edx
149 \item all other parameters are pushed onto the stack 174 \item all other parameters are pushed onto the stack
150 \end{itemize} 175 \end{itemize}
151 176
152 \paragraph{Return values} 177 \paragraph{Return values}
162 187
163 Stack directly after function prolog:\\ 188 Stack directly after function prolog:\\
164 189
165 \begin{figure}[h] 190 \begin{figure}[h]
166 \begin{tabular}{5|3|1 1} 191 \begin{tabular}{5|3|1 1}
167 \hhline{~-~~} 192 & \vdots & & \\
168 & \vdots & & \\ 193 \hhline{~=~~}
169 \hhline{~=~~} 194 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
170 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 195 \hhline{~-~~}
171 \hhline{~-~~} 196 local data & & & \\
172 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 197 \hhline{~-~~}
173 & \ldots & & \\ 198 \mrlbrace{3}{parameter area} & last arg & \mrrbrace{3}{stack parameters} & \\
174 & \ldots & & \\ 199 & \ldots & & \\
175 \hhline{~-~~} 200 & first arg passed via stack & & \\
176 & return address & & \\ 201 \hhline{~-~~}
177 \hhline{~=~~} 202 & return address & & \\
178 local data & & & \mrrbrace{3}{current frame} \\ 203 \hhline{~=~~}
179 \hhline{~-~~} 204 register save area & & & \mrrbrace{4}{current frame} \\
180 parameter area & & & \\ 205 \hhline{~-~~}
181 \hhline{~-~~} 206 local data & & & \\
182 & \vdots & & \\ 207 \hhline{~-~~}
183 \hhline{~-~~} 208 parameter area & & & \\
209 \hhline{~-~~}
210 & \vdots & & \\
184 \end{tabular} 211 \end{tabular}
185 \caption{Stack layout on x86 fastcall (MS) calling convention} 212 \caption{Stack layout on x86 fastcall (MS) calling convention}
186 \end{figure} 213 \end{figure}
187 214
188 215
195 \begin{table}[h] 222 \begin{table}[h]
196 \begin{tabular*}{0.95\textwidth}{3 B} 223 \begin{tabular*}{0.95\textwidth}{3 B}
197 Name & Brief description\\ 224 Name & Brief description\\
198 \hline 225 \hline
199 {\bf eax} & scratch, return value\\ 226 {\bf eax} & scratch, return value\\
200 {\bf ebx} & permanent\\ 227 {\bf ebx} & preserve\\
201 {\bf ecx} & scratch, parameter 0\\ 228 {\bf ecx} & scratch, parameter 0\\
202 {\bf edx} & scratch, parameter 1, return value\\ 229 {\bf edx} & scratch, parameter 1, return value\\
203 {\bf esi} & permanent\\ 230 {\bf esi} & preserve\\
204 {\bf edi} & permanent\\ 231 {\bf edi} & preserve\\
205 {\bf ebp} & permanent\\ 232 {\bf ebp} & preserve\\
206 {\bf esp} & stack pointer\\ 233 {\bf esp} & stack pointer\\
207 {\bf st0} & scratch, floating point return value\\ 234 {\bf st0} & scratch, floating point return value\\
208 {\bf st1-st7} & scratch\\ 235 {\bf st1-st7} & scratch\\
209 \end{tabular*} 236 \end{tabular*}
210 \caption{Register usage on x86 fastcall (GNU) calling convention} 237 \caption{Register usage on x86 fastcall (GNU) calling convention}
236 263
237 Stack directly after function prolog:\\ 264 Stack directly after function prolog:\\
238 265
239 \begin{figure}[h] 266 \begin{figure}[h]
240 \begin{tabular}{5|3|1 1} 267 \begin{tabular}{5|3|1 1}
241 \hhline{~-~~} 268 & \vdots & & \\
242 & \vdots & & \\ 269 \hhline{~=~~}
243 \hhline{~=~~} 270 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
244 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 271 \hhline{~-~~}
245 \hhline{~-~~} 272 local data & & & \\
246 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 273 \hhline{~-~~}
247 & \ldots & & \\ 274 \mrlbrace{3}{parameter area} & last arg & \mrrbrace{3}{stack parameters} & \\
248 & \ldots & & \\ 275 & \ldots & & \\
249 \hhline{~-~~} 276 & first arg passed via stack & & \\
250 & return address & & \\ 277 \hhline{~-~~}
251 \hhline{~=~~} 278 & return address & & \\
252 local data & & & \mrrbrace{3}{current frame} \\ 279 \hhline{~=~~}
253 \hhline{~-~~} 280 register save area & & & \mrrbrace{4}{current frame} \\
254 parameter area & & & \\ 281 \hhline{~-~~}
255 \hhline{~-~~} 282 local data & & & \\
256 & \vdots & & \\ 283 \hhline{~-~~}
257 \hhline{~-~~} 284 parameter area & & & \\
285 \hhline{~-~~}
286 & \vdots & & \\
258 \end{tabular} 287 \end{tabular}
259 \caption{Stack layout on x86 fastcall (GNU) calling convention} 288 \caption{Stack layout on x86 fastcall (GNU) calling convention}
260 \end{figure} 289 \end{figure}
261 290
262 291
263 \subsubsection{Borland fastcall} 292 \subsubsection{Borland fastcall}
293
294 Also called {\bf register convention} by Borland.
295
264 296
265 \paragraph{Registers and register usage} 297 \paragraph{Registers and register usage}
266 298
267 \begin{table}[h] 299 \begin{table}[h]
268 \begin{tabular*}{0.95\textwidth}{3 B} 300 \begin{tabular*}{0.95\textwidth}{3 B}
269 Name & Brief description\\ 301 Name & Brief description\\
270 \hline 302 \hline
271 {\bf eax} & scratch, parameter 0, return value\\ 303 {\bf eax} & scratch, parameter 0, return value\\
272 {\bf ebx} & permanent\\ 304 {\bf ebx} & preserve\\
273 {\bf ecx} & scratch, parameter 2\\ 305 {\bf ecx} & scratch, parameter 2\\
274 {\bf edx} & scratch, parameter 1, return value\\ 306 {\bf edx} & scratch, parameter 1, return value\\
275 {\bf esi} & permanent\\ 307 {\bf esi} & preserve\\
276 {\bf edi} & permanent\\ 308 {\bf edi} & preserve\\
277 {\bf ebp} & permanent\\ 309 {\bf ebp} & preserve\\
278 {\bf esp} & stack pointer\\ 310 {\bf esp} & stack pointer\\
279 {\bf st0} & scratch, floating point return value\\ 311 {\bf st0} & scratch, floating point return value\\
280 {\bf st1-st7} & scratch\\ 312 {\bf st1-st7} & scratch\\
281 \end{tabular*} 313 \end{tabular*}
282 \caption{Register usage on x86 fastcall (Borland) calling convention} 314 \caption{Register usage on x86 fastcall (Borland) calling convention}
285 \paragraph{Parameter passing} 317 \paragraph{Parameter passing}
286 318
287 \begin{itemize} 319 \begin{itemize}
288 \item stack parameter order: left-to-right 320 \item stack parameter order: left-to-right
289 \item called function cleans up the stack 321 \item called function cleans up the stack
290 \item first three integers/pointers (\textless=\ 32bit) are passed via eax, ecx and edx (even if preceded by other arguments@@@?) 322 \item first three integers/pointers (with exception of method pointers) (\textless=\ 32bit) are passed via eax, ecx and edx (even if preceded or interleaved by other arguments)
291 \item integer types 64 bits in size @@@ ?
292 \item all other parameters are pushed onto the stack 323 \item all other parameters are pushed onto the stack
293 \end{itemize} 324 \end{itemize}
294 325
295 326
296 \pagebreak 327 \pagebreak
328
329 \paragraph{Return values}
330
331 \begin{itemize}
332 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
333 \item integers \textgreater\ 32 bits are returned via the eax and edx registers
334 \item floating point types are returned via the st0 register
335 \item all others (e.g. all structs, return values \textgreater\ 64 bits, ...) are returned by the caller allocating the space and
336 passing a pointer to the callee as a new, implicit first parameter
337 \end{itemize}
338
339
340
341 \paragraph{Stack layout}
342
343 Stack directly after function prolog:\\
344
345 \begin{figure}[h]
346 \begin{tabular}{5|3|1 1}
347 & \vdots & & \\
348 \hhline{~=~~}
349 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
350 \hhline{~-~~}
351 local data & & & \\
352 \hhline{~-~~}
353 \mrlbrace{3}{parameter area} & first arg passed via stack & \mrrbrace{3}{stack parameters} & \\
354 & \ldots & & \\
355 & last arg & & \\
356 \hhline{~-~~}
357 & return address & & \\
358 \hhline{~=~~}
359 register save area & & & \mrrbrace{4}{current frame} \\
360 \hhline{~-~~}
361 local data & & & \\
362 \hhline{~-~~}
363 parameter area & & & \\
364 \hhline{~-~~}
365 & \vdots & & \\
366 \end{tabular}
367 \caption{Stack layout on x86 fastcall (Borland) calling convention}
368 \end{figure}
369
370
371 \subsubsection{Watcom fastcall}
372
373
374 \paragraph{Registers and register usage}
375
376 \begin{table}[h]
377 \begin{tabular*}{0.95\textwidth}{3 B}
378 Name & Brief description\\
379 \hline
380 {\bf eax} & scratch, parameter 0, return value\\
381 {\bf ebx} & scratch when used for parameter, otherwise preserve, parameter 2\\
382 {\bf ecx} & scratch when used for parameter, otherwise preserve, parameter 3\\
383 {\bf edx} & scratch when used for parameter, otherwise preserve, parameter 1, return value\\
384 {\bf esi} & scratch when used for return pointer, otherwise preserve\\
385 {\bf edi} & preserve\\
386 {\bf ebp} & preserve\\
387 {\bf esp} & stack pointer\\
388 {\bf st0} & scratch, floating point return value\\
389 {\bf st1-st7} & scratch\\
390 \end{tabular*}
391 \caption{Register usage on x86 fastcall (Watcom) calling convention}
392 \end{table}
393
394 \paragraph{Parameter passing}
395
396 \begin{itemize}
397 \item stack parameter order: right-to-left
398 \item called function cleans up the stack
399 \item first four integers/pointers (\textless=\ 32bit) are passed via eax, edx, ebx and ecx (even if preceded by other arguments)
400 \item arguments \textgreater 32 bits, as well as all subsequent arguments, are passed via the stack
401 \item all other parameters are pushed onto the stack
402 \item varargs are always passed via the stack
403 \end{itemize}
404
297 405
298 \paragraph{Return values} 406 \paragraph{Return values}
299 407
300 \begin{itemize} 408 \begin{itemize}
301 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register 409 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
302 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@ verify 410 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@ verify
303 \item floating point types are returned via the st0 register@@@ really ? 411 \item floating point types are returned via the st0 register@@@ really ?
304 \end{itemize} 412 \end{itemize}
305 413
306 414
307
308 \paragraph{Stack layout} 415 \paragraph{Stack layout}
309 416
310 Stack directly after function prolog:\\ 417 Stack directly after function prolog:\\
311 418
312 \begin{figure}[h] 419 \begin{figure}[h]
313 \begin{tabular}{5|3|1 1} 420 \begin{tabular}{5|3|1 1}
314 \hhline{~-~~} 421 & \vdots & & \\
315 & \vdots & & \\ 422 \hhline{~=~~}
316 \hhline{~=~~} 423 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
317 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 424 \hhline{~-~~}
318 \hhline{~-~~} 425 local data & & & \\
319 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 426 \hhline{~-~~}
320 & \ldots & & \\ 427 \mrlbrace{3}{parameter area} & last arg & \mrrbrace{3}{stack parameters} & \\
321 & \ldots & & \\ 428 & \ldots & & \\
322 \hhline{~-~~} 429 & first arg passed via stack & & \\
323 & return address & & \\ 430 \hhline{~-~~}
324 \hhline{~=~~} 431 & return address & & \\
325 local data & & & \mrrbrace{3}{current frame} \\ 432 \hhline{~=~~}
326 \hhline{~-~~} 433 register save area & & & \mrrbrace{4}{current frame} \\
327 parameter area & & & \\ 434 \hhline{~-~~}
328 \hhline{~-~~} 435 local data & & & \\
329 & \vdots & & \\ 436 \hhline{~-~~}
330 \hhline{~-~~} 437 parameter area & & & \\
331 \end{tabular} 438 \hhline{~-~~}
332 \caption{Stack layout on x86 fastcall (Borland) calling convention} 439 & \vdots & & \\
333 \end{figure}
334
335
336 \subsubsection{Watcom fastcall}
337
338
339 \paragraph{Registers and register usage}
340
341 \begin{table}[h]
342 \begin{tabular*}{0.95\textwidth}{3 B}
343 Name & Brief description\\
344 \hline
345 {\bf eax} & scratch, parameter 0, return value@@@\\
346 {\bf ebx} & scratch when used for parameter, parameter 2\\
347 {\bf ecx} & scratch when used for parameter, parameter 3\\
348 {\bf edx} & scratch when used for parameter, parameter 1, return value@@@\\
349 {\bf esi} & scratch when used for return pointer @@@??\\
350 {\bf edi} & permanent\\
351 {\bf ebp} & permanent\\
352 {\bf esp} & stack pointer\\
353 {\bf st0} & scratch, floating point return value\\
354 {\bf st1-st7} & scratch\\
355 \end{tabular*}
356 \caption{Register usage on x86 fastcall (Watcom) calling convention}
357 \end{table}
358
359 \paragraph{Parameter passing}
360
361 \begin{itemize}
362 \item stack parameter order: right-to-left
363 \item called function cleans up the stack
364 \item first four integers/pointers (\textless=\ 32bit) are passed via eax, edx, ebx and ecx (even if preceded by other arguments@@@?)
365 \item integer types 64 bits in size @@@ ?
366 \item all other parameters are pushed onto the stack
367 \end{itemize}
368
369
370 \paragraph{Return values}
371
372 \begin{itemize}
373 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register@@@verify, I thnik its esi?
374 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@ verify
375 \item floating point types are returned via the st0 register@@@ really ?
376 \end{itemize}
377
378
379 \paragraph{Stack layout}
380
381 Stack directly after function prolog:\\
382
383 \begin{figure}[h]
384 \begin{tabular}{5|3|1 1}
385 \hhline{~-~~}
386 & \vdots & & \\
387 \hhline{~=~~}
388 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\
389 \hhline{~-~~}
390 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\
391 & \ldots & & \\
392 & \ldots & & \\
393 \hhline{~-~~}
394 & return address & & \\
395 \hhline{~=~~}
396 local data & & & \mrrbrace{3}{current frame} \\
397 \hhline{~-~~}
398 parameter area & & & \\
399 \hhline{~-~~}
400 & \vdots & & \\
401 \hhline{~-~~}
402 \end{tabular} 440 \end{tabular}
403 \caption{Stack layout on x86 fastcall (Watcom) calling convention} 441 \caption{Stack layout on x86 fastcall (Watcom) calling convention}
404 \end{figure} 442 \end{figure}
405 443
406 444
412 \begin{table}[h] 450 \begin{table}[h]
413 \begin{tabular*}{0.95\textwidth}{3 B} 451 \begin{tabular*}{0.95\textwidth}{3 B}
414 Name & Brief description\\ 452 Name & Brief description\\
415 \hline 453 \hline
416 {\bf eax} & scratch, return value\\ 454 {\bf eax} & scratch, return value\\
417 {\bf ebx} & permanent\\ 455 {\bf ebx} & preserve\\
418 {\bf ecx} & scratch\\ 456 {\bf ecx} & scratch\\
419 {\bf edx} & scratch, return value\\ 457 {\bf edx} & scratch, return value\\
420 {\bf esi} & permanent\\ 458 {\bf esi} & preserve\\
421 {\bf edi} & permanent\\ 459 {\bf edi} & preserve\\
422 {\bf ebp} & permanent\\ 460 {\bf ebp} & preserve\\
423 {\bf esp} & stack pointer\\ 461 {\bf esp} & stack pointer\\
424 {\bf st0} & scratch, floating point return value\\ 462 {\bf st0} & scratch, floating point return value\\
425 {\bf st1-st7} & scratch\\ 463 {\bf st1-st7} & scratch\\
426 \end{tabular*} 464 \end{tabular*}
427 \caption{Register usage on x86 stdcall calling convention} 465 \caption{Register usage on x86 stdcall calling convention}
431 469
432 \begin{itemize} 470 \begin{itemize}
433 \item Stack parameter order: right-to-left 471 \item Stack parameter order: right-to-left
434 \item Called function cleans up the stack 472 \item Called function cleans up the stack
435 \item All parameters are pushed onto the stack 473 \item All parameters are pushed onto the stack
436 \item Stack is usually 4 byte aligned (GCC \textgreater=\ 3.x seems to use a 16byte alignement@@@) 474 \item Stack is usually 4 byte aligned (GCC \textgreater=\ 3.x seems to use a 16byte alignement)
437 \item Function name is decorated by prepending an underscore character and appending a '@' character and the number of bytes of stack space required 475 \item the direction flag is clear on entry and must be returned clear % mention it first, above @@@
438 \end{itemize} 476 \end{itemize}
439 477
478 % introduce mangling section? \item Function name is decorated by prepending an underscore character and appending a '@' character and the number of bytes of stack space required
440 479
441 \paragraph{Return values} 480 \paragraph{Return values}
442 481
443 \begin{itemize} 482 \begin{itemize}
444 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register 483 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
451 490
452 Stack directly after function prolog:\\ 491 Stack directly after function prolog:\\
453 492
454 \begin{figure}[h] 493 \begin{figure}[h]
455 \begin{tabular}{5|3|1 1} 494 \begin{tabular}{5|3|1 1}
456 \hhline{~-~~} 495 & \vdots & & \\
457 & \vdots & & \\ 496 \hhline{~=~~}
458 \hhline{~=~~} 497 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
459 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 498 \hhline{~-~~}
460 \hhline{~-~~} 499 local data & & & \\
461 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 500 \hhline{~-~~}
501 \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
462 & \ldots & & \\ 502 & \ldots & & \\
463 & \ldots & & \\ 503 & arg 0 & & \\
464 \hhline{~-~~} 504 \hhline{~-~~}
465 & return address & & \\ 505 & return address & & \\
466 \hhline{~=~~} 506 \hhline{~=~~}
467 local data & & & \mrrbrace{3}{current frame} \\ 507 register save area & & & \mrrbrace{4}{current frame} \\
508 \hhline{~-~~}
509 local data & & & \\
468 \hhline{~-~~} 510 \hhline{~-~~}
469 parameter area & & & \\ 511 parameter area & & & \\
470 \hhline{~-~~} 512 \hhline{~-~~}
471 & \vdots & & \\ 513 & \vdots & & \\
472 \hhline{~-~~}
473 \end{tabular} 514 \end{tabular}
474 \caption{Stack layout on x86 stdcall calling convention} 515 \caption{Stack layout on x86 stdcall calling convention}
475 \end{figure} 516 \end{figure}
476 517
477 \subsubsection{MS thiscall} 518 \subsubsection{MS thiscall}
481 \begin{table}[h] 522 \begin{table}[h]
482 \begin{tabular*}{0.95\textwidth}{3 B} 523 \begin{tabular*}{0.95\textwidth}{3 B}
483 Name & Brief description\\ 524 Name & Brief description\\
484 \hline 525 \hline
485 {\bf eax} & scratch, return value\\ 526 {\bf eax} & scratch, return value\\
486 {\bf ebx} & permanent\\ 527 {\bf ebx} & preserve\\
487 {\bf ecx} & scratch, parameter 0\\ 528 {\bf ecx} & scratch, parameter 0\\
488 {\bf edx} & scratch, return value\\ 529 {\bf edx} & scratch, return value\\
489 {\bf esi} & permanent\\ 530 {\bf esi} & preserve\\
490 {\bf edi} & permanent\\ 531 {\bf edi} & preserve\\
491 {\bf ebp} & permanent\\ 532 {\bf ebp} & preserve\\
492 {\bf esp} & stack pointer\\ 533 {\bf esp} & stack pointer\\
493 {\bf st0} & scratch, floating point return value\\ 534 {\bf st0} & scratch, floating point return value\\
494 {\bf st1-st7} & scratch\\ 535 {\bf st1-st7} & scratch\\
495 \end{tabular*} 536 \end{tabular*}
496 \caption{Register usage on x86 thiscall (MS) calling convention} 537 \caption{Register usage on x86 thiscall (MS) calling convention}
504 \begin{itemize} 545 \begin{itemize}
505 \item stack parameter order: right-to-left 546 \item stack parameter order: right-to-left
506 \item called function cleans up the stack 547 \item called function cleans up the stack
507 \item first parameter (this pointer) is passed via ecx 548 \item first parameter (this pointer) is passed via ecx
508 \item all other parameters are pushed onto the stack 549 \item all other parameters are pushed onto the stack
509 \item Function name is decorated by prepending a '@' character and appending a '@' character and the number of bytes (decimal) of stack space required 550 \end{itemize}
510 \end{itemize} 551
511 552 % introduce mangling section? \item Function name is decorated by prepending a '@' character and appending a '@' character and the number of bytes (decimal) of stack space required
512 553
513 \paragraph{Return values} 554 \paragraph{Return values}
514 555
515 \begin{itemize} 556 \begin{itemize}
516 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register 557 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
523 564
524 Stack directly after function prolog:\\ 565 Stack directly after function prolog:\\
525 566
526 \begin{figure}[h] 567 \begin{figure}[h]
527 \begin{tabular}{5|3|1 1} 568 \begin{tabular}{5|3|1 1}
528 \hhline{~-~~} 569 & \vdots & & \\
529 & \vdots & & \\ 570 \hhline{~=~~}
530 \hhline{~=~~} 571 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
531 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 572 \hhline{~=~~}
532 \hhline{~-~~} 573 local data & & & \\
533 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 574 \hhline{~-~~}
575 \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
534 & \ldots & & \\ 576 & \ldots & & \\
535 & \ldots & & \\ 577 & arg 1 & & \\
536 \hhline{~-~~} 578 \hhline{~-~~}
537 & return address & & \\ 579 & return address & & \\
538 \hhline{~=~~} 580 \hhline{~=~~}
539 local data & & & \mrrbrace{3}{current frame} \\ 581 register save area & & & \mrrbrace{4}{current frame} \\
582 \hhline{~-~~}
583 local data & & & \\
540 \hhline{~-~~} 584 \hhline{~-~~}
541 parameter area & & & \\ 585 parameter area & & & \\
542 \hhline{~-~~} 586 \hhline{~-~~}
543 & \vdots & & \\ 587 & \vdots & & \\
544 \hhline{~-~~}
545 \end{tabular} 588 \end{tabular}
546 \caption{Stack layout on x86 thiscall (MS) calling convention} 589 \caption{Stack layout on x86 thiscall (MS) calling convention}
547 \end{figure} 590 \end{figure}
548 591
549 592
550 593
551 \subsubsection{GNU thiscall} 594 \subsubsection{GNU thiscall}
595
596 This is equivalent to the cdecl calling convention, with the first parameter being the this pointer.
597
598 % \paragraph{Registers and register usage}
599 %
600 % \begin{table}[h]
601 % \begin{tabular*}{0.95\textwidth}{3 B}
602 % Name & Brief description\\
603 % \hline
604 % {\bf eax} & scratch, return value\\
605 % {\bf ebx} & preserve\\
606 % {\bf ecx} & scratch\\
607 % {\bf edx} & scratch, return value\\
608 % {\bf esi} & preserve\\
609 % {\bf edi} & preserve\\
610 % {\bf ebp} & preserve\\
611 % {\bf esp} & stack pointer\\
612 % {\bf st0} & scratch, floating point return value\\
613 % {\bf st1-st7} & scratch\\
614 % \end{tabular*}
615 % \caption{Register usage on x86 thiscall (GNU) calling convention}
616 % \end{table}
617 %
618 % \paragraph{Parameter passing}
619 %
620 % \begin{itemize}
621 % \item stack parameter order: right-to-left
622 % \item caller cleans up the stack
623 % \item all parameters are pushed onto the stack
624 % \end{itemize}
625 %
626 %
627 % \paragraph{Return values}
628 %
629 % \begin{itemize}
630 % \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
631 % \item integers \textgreater\ 32 bits are returned via the eax and edx registers
632 % \item floating point types are returned via the st0 register
633 % \end{itemize}
634 %
635 %
636 % \paragraph{Stack layout}
637 %
638 % Stack directly after function prolog:\\
639 %
640 % \begin{figure}[h]
641 % \begin{tabular}{5|3|1 1}
642 % & \vdots & & \\
643 % \hhline{~=~~}
644 % register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
645 % \hhline{~-~~}
646 % local data & & & \\
647 % \hhline{~-~~}
648 % \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
649 % & \ldots & & \\
650 % & arg 0 & & \\
651 % \hhline{~-~~}
652 % & return address & & \\
653 % \hhline{~=~~}
654 % register save area & & & \mrrbrace{4}{current frame} \\
655 % \hhline{~-~~}
656 % local data & & & \\
657 % \hhline{~-~~}
658 % parameter area & & & \\
659 % \hhline{~-~~}
660 % & \vdots & & \\
661 % \end{tabular}
662 % \caption{Stack layout on x86 thiscall (GNU) calling convention}
663 % \end{figure}
664
665
666
667 \subsubsection{pascal}
668
669 The best known uses of the pascal calling convention are the 16 bit OS/2 APIs, Microsoft Windows 3.x and Borland Delphi 1.x.
552 670
553 \paragraph{Registers and register usage} 671 \paragraph{Registers and register usage}
554 672
555 \begin{table}[h] 673 \begin{table}[h]
556 \begin{tabular*}{0.95\textwidth}{3 B} 674 \begin{tabular*}{0.95\textwidth}{3 B}
557 Name & Brief description\\ 675 Name & Brief description\\
558 \hline 676 \hline
559 {\bf eax} & scratch, return value\\ 677 {\bf eax} & scratch, return value\\
560 {\bf ebx} & permanent\\ 678 {\bf ebx} & preserve\\
561 {\bf ecx} & scratch\\ 679 {\bf ecx} & scratch\\
562 {\bf edx} & scratch, return value\\ 680 {\bf edx} & scratch, return value\\
563 {\bf esi} & permanent\\ 681 {\bf esi} & preserve\\
564 {\bf edi} & permanent\\ 682 {\bf edi} & preserve\\
565 {\bf ebp} & permanent\\ 683 {\bf ebp} & preserve\\
566 {\bf esp} & stack pointer\\ 684 {\bf esp} & stack pointer\\
567 {\bf st0} & scratch, floating point return value\\ 685 {\bf st0} & scratch, floating point return value\\
568 {\bf st1-st7} & scratch\\ 686 {\bf st1-st7} & scratch\\
569 \end{tabular*} 687 \end{tabular*}
570 \caption{Register usage on x86 thiscall (GNU) calling convention}
571 \end{table}
572
573 \paragraph{Parameter passing}
574
575 \begin{itemize}
576 \item stack parameter order: right-to-left
577 \item caller cleans up the stack
578 \item all parameters are pushed onto the stack
579 \end{itemize}
580
581
582 \paragraph{Return values}
583
584 \begin{itemize}
585 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register
586 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@verify
587 \item floating point types are returned via the st0 register@@@ really ?
588 \end{itemize}
589
590
591 \paragraph{Stack layout}
592
593 Stack directly after function prolog:\\
594
595 \begin{figure}[h]
596 \begin{tabular}{5|3|1 1}
597 \hhline{~-~~}
598 & \vdots & & \\
599 \hhline{~=~~}
600 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\
601 \hhline{~-~~}
602 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\
603 & \ldots & & \\
604 & \ldots & & \\
605 \hhline{~-~~}
606 & return address & & \\
607 \hhline{~=~~}
608 local data & & & \mrrbrace{3}{current frame} \\
609 \hhline{~-~~}
610 parameter area & & & \\
611 \hhline{~-~~}
612 & \vdots & & \\
613 \hhline{~-~~}
614 \end{tabular}
615 \caption{Stack layout on x86 thiscall (GNU) calling convention}
616 \end{figure}
617
618
619
620 \subsubsection{pascal}
621
622 The best known uses of the pascal calling convention are the 16 bit OS/2 APIs, Microsoft Windows 3.x and Borland Delphi 1.x.
623
624 \paragraph{Registers and register usage}
625
626 \begin{table}[h]
627 \begin{tabular*}{0.95\textwidth}{3 B}
628 Name & Brief description\\
629 \hline
630 {\bf eax} & scratch, return value\\
631 {\bf ebx} & permanent\\
632 {\bf ecx} & scratch\\
633 {\bf edx} & scratch, return value\\
634 {\bf esi} & permanent\\
635 {\bf edi} & permanent\\
636 {\bf ebp} & permanent\\
637 {\bf esp} & stack pointer\\
638 {\bf st0} & scratch, floating point return value\\
639 {\bf st1-st7} & scratch\\
640 \end{tabular*}
641 \caption{Register usage on x86 pascal calling convention} 688 \caption{Register usage on x86 pascal calling convention}
642 \end{table} 689 \end{table}
643 690
644 \paragraph{Parameter passing} 691 \paragraph{Parameter passing}
645 692
663 710
664 Stack directly after function prolog:\\ 711 Stack directly after function prolog:\\
665 712
666 \begin{figure}[h] 713 \begin{figure}[h]
667 \begin{tabular}{5|3|1 1} 714 \begin{tabular}{5|3|1 1}
668 \hhline{~-~~} 715 & \vdots & & \\
669 & \vdots & & \\ 716 \hhline{~=~~}
670 \hhline{~=~~} 717 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\
671 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 718 \hhline{~-~~}
672 \hhline{~-~~} 719 local data & & & \\
673 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 720 \hhline{~-~~}
721 \mrlbrace{3}{parameter area} & arg 0 & \mrrbrace{3}{stack parameters} & \\
674 & \ldots & & \\ 722 & \ldots & & \\
675 & \ldots & & \\ 723 & arg n-1 & & \\
676 \hhline{~-~~} 724 \hhline{~-~~}
677 & return address & & \\ 725 & return address & & \\
678 \hhline{~=~~} 726 \hhline{~=~~}
679 local data & & & \mrrbrace{3}{current frame} \\ 727 register save area & & & \mrrbrace{4}{current frame} \\
728 \hhline{~-~~}
729 local data & & & \\
680 \hhline{~-~~} 730 \hhline{~-~~}
681 parameter area & & & \\ 731 parameter area & & & \\
682 \hhline{~-~~} 732 \hhline{~-~~}
683 & \vdots & & \\ 733 & \vdots & & \\
684 \hhline{~-~~}
685 \end{tabular} 734 \end{tabular}
686 \caption{Stack layout on x86 pascal calling convention} 735 \caption{Stack layout on x86 pascal calling convention}
687 \end{figure} 736 \end{figure}
688 737
689 738
713 762
714 \paragraph{Parameter passing} 763 \paragraph{Parameter passing}
715 764
716 \begin{itemize} 765 \begin{itemize}
717 \item stack parameter order: right-to-left 766 \item stack parameter order: right-to-left
718 \item caller cleans up the stack%@@@ doesn't belong to "parameter passing" 767 \item caller cleans up the stack
719 \item all parameters are pushed onto the stack 768 \item all parameters are pushed onto the stack
720 \end{itemize} 769 \end{itemize}
721 770
722 \pagebreak 771 \pagebreak
723 772
731 \end{itemize} 780 \end{itemize}
732 781
733 782
734 \paragraph{Stack layout} 783 \paragraph{Stack layout}
735 784
736 Stack directly after function prolog:\\ 785 % verified/amended: TP nov 2019 (see also doc/disas_examples/x86.plan9call.disas)
786 Note there is no register save area at all. Stack directly after function prolog:\\
737 787
738 \begin{figure}[h] 788 \begin{figure}[h]
739 \begin{tabular}{5|3|1 1} 789 \begin{tabular}{5|3|1 1}
740 \hhline{~-~~}
741 & \vdots & & \\ 790 & \vdots & & \\
742 \hhline{~=~~} 791 \hhline{~=~~}
743 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ 792 local data & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\
744 \hhline{~-~~} 793 \hhline{~-~~}
745 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ 794 \mrlbrace{3}{parameter area} & arg n-1 & \mrrbrace{3}{stack parameters} & \\
746 & \ldots & & \\ 795 & \ldots & & \\
747 & \ldots & & \\ 796 & arg 0 & & \\
748 \hhline{~-~~} 797 \hhline{~-~~}
749 & return address & & \\ 798 & return address & & \\
750 \hhline{~=~~} 799 \hhline{~=~~}
751 local data & & & \mrrbrace{3}{current frame} \\ 800 local data & & & \mrrbrace{3}{current frame} \\
752 \hhline{~-~~} 801 \hhline{~-~~}
753 parameter area & & & \\ 802 parameter area & & & \\
754 \hhline{~-~~} 803 \hhline{~-~~}
755 & \vdots & & \\ 804 & \vdots & & \\
756 \hhline{~-~~}
757 \end{tabular} 805 \end{tabular}
758 \\
759 \\
760 \\
761 \caption{Stack layout on x86 plan9call calling convention} 806 \caption{Stack layout on x86 plan9call calling convention}
762 \end{figure} 807 \end{figure}
808