Next Chapter | Previous Chapter | Contents | Index
This chapter attempts to cover some of the common issues involved when writing 64-bit code, to run under Win64 or Unix. It covers how to write assembly code to interface with 64-bit C routines, and how to write position-independent code for shared libraries.
All 64-bit code uses a flat memory model, since segmentation is not
available in 64-bit mode. The one exception is the
and registers,
which still add their bases.
Position independence in 64-bit mode is significantly simpler, since the
processor supports -relative addressing
directly; see the keyword
(section 3.3). On most 64-bit
platforms, it is probably desirable to make that the default, using the
directive
(section 5.2).
64-bit programming is relatively similar to 32-bit programming, but of course pointers are 64 bits long; additionally, all existing platforms pass arguments in registers rather than on the stack. Furthermore, 64-bit platforms use SSE2 by default for floating point. Please see the ABI documentation for your platform.
64-bit platforms differ in the sizes of the fundamental datatypes, not
just from 32-bit platforms but from each other. If a specific size data
type is desired, it is probably best to use the types defined in the
Standard C header .
On Unix, the 64-bit ABI is defined by the document:
Although written for AT&T-syntax assembly, the concepts apply equally well for NASM-style assembly. What follows is a simplified summary.
The first six integer arguments (from the left) are passed in
, ,
, ,
, and , in that
order. Additional integer arguments are passed on the stack. These
registers, plus ,
and are
destroyed by function calls, and thus are available for use by the function
without saving.
Integer return values are passed in and
, in that order.
Floating point is done using SSE registers, except for
. Floating-point arguments are passed
in to ; return
is and .
are passed on the stack, and returned
in and .
All SSE and x87 registers are destroyed by function calls.
On 64-bit Unix, is 64 bits.
The Win64 ABI is described at:
What follows is a simplified summary.
The first four integer arguments are passwd in
, ,
and , in that
order. Additional integer arguments are passed on the stack. These
registers, plus ,
and are
destroyed by function calls, and thus are available for use by the function
without saving.
Integer return values are passed in only.
Floating point is done using SSE registers, except for
. Floating-point arguments are passed
in to ; return
is only.
On Win64, is 32 bits;
or is
64 bits.