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.

1
  • What the document means to say is that either operand can be memory. It doesn’t mean both, but that isn’t clear. Commented Dec 20, 2019 at 8:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.