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
Position independence in 64-bit mode is significantly simpler, since the
processor supports 
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 
In 64-bit mode, the default instruction size is still 32 bits. When loading a value into a 32-bit register (but not an 8- or 16-bit register), the upper 32 bits of the corresponding 64-bit register are set to zero.
NASM uses the following names for general-purpose registers in 64-bit mode, for 8-, 16-, 32- and 64-bit references, respecitively:
     AL/AH, CL/CH, DL/DH, BL/BH, SPL, BPL, SIL, DIL, R8B-R15B 
     AX, CX, DX, BX, SP, BP, SI, DI, R8W-R15W 
     EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, R8D-R15D 
     RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI, R8-R15
This is consistent with the AMD documentation and most other assemblers.
The Intel documentation, however, uses the names
In 64-bit mode, immediates and displacements are generally only 32 bits wide. NASM will therefore truncate most displacements and immediates to 32 bits.
The only instruction which takes a full 64-bit immediate is:
     MOV reg64,imm64
NASM will produce this instruction whenever the programmer uses
     mov rax,foo             ; 64-bit immediate 
     mov rax,qword foo       ; (identical) 
     mov eax,foo             ; 32-bit immediate, zero-extended 
     mov rax,dword foo       ; 32-bit immediate, sign-extended
The length of these instructions are 10, 5 and 7 bytes, respectively.
The only instructions which take a full 64-bit displacement is
loading or storing, using 
     default abs 
     mov eax,[foo]           ; 32-bit absolute disp, sign-extended 
     mov eax,[a32 foo]       ; 32-bit absolute disp, zero-extended 
     mov eax,[qword foo]     ; 64-bit absolute disp 
     default rel 
     mov eax,[foo]           ; 32-bit relative disp 
     mov eax,[a32 foo]       ; d:o, address truncated to 32 bits(!) 
     mov eax,[qword foo]     ; error 
     mov eax,[abs qword foo] ; 64-bit absolute disp
A sign-extended absolute displacement can access from -2 GB to +2 GB; a zero-extended absolute displacement can access from 0 to 4 GB.
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
Integer return values are passed in 
Floating point is done using SSE registers, except for
All SSE and x87 registers are destroyed by function calls.
On 64-bit Unix, 
Integer and SSE register arguments are counted separately, so for the case of
     void foo(long a, double b, int c)
The Win64 ABI is described at:
What follows is a simplified summary.
The first four integer arguments are passed in
Integer return values are passed in 
Floating point is done using SSE registers, except for
On Win64, 
Integer and SSE register arguments are counted together, so for the case of
     void foo(long long a, double b, int c)