Immediate operands in
NASM may be 8 bits, 16 bits, 32 bits, and even 64 bits in size. The immediate size can be
directly specified through the use of the BYTE
, WORD
, or DWORD
keywords, respectively.
64 bit immediate operands are limited to direct 64-bit register move instructions in
BITS 64
mode. For all other instructions in 64-bit mode,
immediate values remain 32 bits; their value is sign-extended into the upper 32 bits of
the target register prior to being used. The exception is the mov instruction, which can
take a 64-bit immediate when the destination is a 64-bit register.
All unsized immediate values in BITS 64
in Yasm default
to 32-bit size for consistency. In order to get a 64-bit immediate with a label, specify
the size explicitly with the QWORD
keyword. For ease of use,
Yasm will also try to recognize 64-bit values and change the size to 64 bits
automatically for these cases.
Examples in NASM syntax:
add rax, 1 ; optimized down to signed 8-bit add rax, dword 1 ; force size to 32-bit add rax, 0xffffffff ; sign-extended 32-bit add rax, -1 ; same as above add rax, 0xffffffffffffffff ; truncated to 32-bit (warning) mov eax, 1 ; 5 byte mov rax, 1 ; 5 byte (optimized to signed 32-bit) mov rax, qword 1 ; 10 byte (forced 64-bit) mov rbx, 0x1234567890abcdef ; 10 byte mov rcx, 0xffffffff ; 10 byte (does not fit in signed 32-bit) mov ecx, -1 ; 5 byte, equivalent to above mov rcx, sym ; 5 byte, 32-bit size default for symbols mov rcx, qword sym ; 10 byte, override default size
A caution for users using both Yasm and NASM 2.x: the handling of mov reg64, unsized immediate is different between Yasm and NASM 2.x; YASM follows the above behavior, while NASM 2.x does the following:
add rax, 0xffffffff ; sign-extended 32-bit immediate add rax, -1 ; same as above add rax, 0xffffffffffffffff ; truncated 32-bit (warning) add rax, sym ; sign-extended 32-bit immediate mov eax, 1 ; 5 byte (32-bit immediate) mov rax, 1 ; 10 byte (64-bit immediate) mov rbx, 0x1234567890abcdef ; 10 byte instruction mov rcx, 0xffffffff ; 10 byte instruction mov ecx, -1 ; 5 byte, equivalent to above mov ecx, sym ; 5 byte (32-bit immediate) mov rcx, sym ; 10 byte (64-bit immediate) mov rcx, qword sym ; 10 byte, same as above