Search code examples
assemblyx86-64nasm

x86_64 cmp instruction invalid combination of opcode and operands


I'm new to NASM and x86_64 assembly. I'm confused with wiki document for cmp instruction. As per the document operands could be either one of below.

cmp minuend, subtrahend

minuend

AL/AX/EAX (only if subtrahend is immediate)
Register
Memory

subtrahend

Register
Immediate
Memory

when I try to compile below code snippet.

var_1 dd 100
var_2 dd 200
cmp dword[var_1], dword[var_2]

it throws an error: invalid combination of opcode and operands

but after I change the cmp instruction to below it compiles fine.

var_1 dd 100
var_2 dd 200
mov eax, [var_1]
cmp eax, dword[var_2]

But as per the wiki document, both the operands could be a memory if so then the first code snippet should be compiled. It would be really helpful if anyone explains me this syntax clearly.


Solution

  • But as per the wiki document, both the operands could be a memory if so then the first code snippet should be compiled.

    No. This is wrong. The x86 architecture in general only allows one memory operand per instruction. So

    mov eax, [var_1]
    cmp eax, dword[var_2]
    

    is valid, because each instruction only has one memory operand.
    This principle reaches its bounds in some of the newest x86_64 SIMD instructions.
    This SO answer describes some (possible) exceptions to the rule.