mov rax,r9+rcx*2
Why is this invalid syntax? Doesn't this count as "mov r64,r/m64" ? I'm a beginner so I'm sorry for my ignorance.
It becomes valid when the expression is enclosed in square brackets mov rax,[r9+rcx*2]. Why is that?
mov rax,r9+rcx*2
Why is this invalid syntax? Doesn't this count as "mov r64,r/m64" ? I'm a beginner so I'm sorry for my ignorance.
It becomes valid when the expression is enclosed in square brackets mov rax,[r9+rcx*2]. Why is that?
On
The hardware machine code is very specific in what it allows and doesn't allow by way of encoding.
There is no general purpose expression capability in the machine code by way of a single, maybe larger instruction, but certain very limited computations are allowed, for one, by addressing modes.
Many assemblers will allow computation expressions if all the operands are constant and it can figure it out during program construction.
For general purpose expressions we need to use a high level language — which will translate into machine code using one or more instructions as needed.
And for assembly programmers, multiple instructions for small computations is normal.
On
In order to perform mathematical operations, relevant commands in assembly language must be used. You cannot perform arithmetic operations directly with registers in assembly language. You cannot do like these: rcx * 2 or r9 + 5. If you want to do "mov rax, r9 + rcx * 2" operation legally, you can do it with the help of the following commands:
This is an invalid syntax because the source operand (rightmost operand) contains registers and operations that the assembler can't perform at compile-time simply because the assembler does not know what values these registers contain. Only at runtime are these values revealed.
Using the square brackets,
[r9 + rcx * 2]becomes a valid addressing mode that uses a base register (R9) and an index register (RCX) that gets scaled by a factor (2).When combined with the
movinstruction, the memory contents at the calculated address will be returned in the indicated register (RAX).When combined with the
leainstruction, the calculated address itself will be returned in the indicated register (RAX).In both cases, the address calculation happens at runtime.