0

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.

Community
  • 1
  • 1
Vencat
  • 1,272
  • 11
  • 36
  • What the document means to say is that **either** operand can be memory. It doesn’t mean **both**, but that isn’t clear. – prl Dec 20 '19 at 08:52

1 Answers1

4

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.

zx485
  • 28,498
  • 28
  • 50
  • 59