The cmp
instruction does not support a 64-bit immediate operand. As such, you cannot put a 64-bit immediate address reference in one of its operands - load name+8
into a register then compare to that register.
You can see what instruction encodings are permitted in the Intel ISA manual (warning: huge PDF). As you can see on the entry for CMP, there are CMP r/m32,
imm32
and CMP r/m64,
imm32
encodings, which allow for comparisons of a 32-bit immediate with both 32-bit and 64-bit registers, but not a CMP r/m64, imm64
. There is, however, a MOV r64, imm64
encoding.
Or even better, use a RIP-relative LEA: Use default rel
then lea r64, [name+8]
. This is more efficient and smaller than mov r64, imm64
.
Since nasm is crashing, the failure of MOV rcx, name+8
is just plain a bug in nasm. Please report it to the nasm devs (after making sure you're using the latest version of nasm; also, check that this patch doesn't fix the problem). In any case, though, one workaround would be to add a symbol for the end of name
:
name:
resb 8
name_end:
Now simply use MOV rcx, name_end
. This has the advantage of not needing to update the referents when the size of name
changes. Alternately you could use a different assembler, such as the clang or GNU binutils assemblers.
Discussion in comments points out that Linux can use a symbol address as a 32-bit immediate. This is true only in non-PIE executables which are linked with a base address in the low 2GiB of virtual address space. But MacOS chooses to put the image base address above 4GiB so you can't use mov r32, imm32
or cmp r64, sign_extended_imm32
with symbol addresses.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…