MOV src, dest (or) MOV dest, src?
Solution 1
mov dest, src
is called Intel syntax. (e.g. mov eax, 123
)
mov src, dest
is called AT&T syntax. (e.g. mov $123, %eax
)
UNIX assemblers including the GNU assembler uses AT&T syntax, all other x86 assemblers I know of uses Intel syntax. You can read up on the differences on wikipedia.
Solution 2
Yes, as/gas use AT&T syntax that uses the order src,dest. MASM, TASM, NASM, etc. all use the order 'dest, src". As it happens, AT&T syntax doesn't fit very well with Intel processors, and (at least IMO) is a nearly unreadable mess. E.g. movzx
comes out particularly bad.
Solution 3
There are two distinct types of assembly language syntax - Intel and AT&T syntax. You can find a comparison of both on Wikipedia's assembly language page.
Chances are your book uses the AT&T syntax, where the source operand comes before the destination.
Solution 4
As already mentioned in the answer by Jerry Coffin, the Intel syntax fits better with the encoding of instructions for the x86 architecture. As a comment in my debugger's disassembler states, "the operands appear in the instruction in the same order as they appear in the disassembly output". For example, consider this instruction:
-a
1772:0100 test word [AA55], 1234
1772:0106
-u 100 l 1
1772:0100 F70655AA3412 test word [AA55], 1234
-
As you can read in the opcode hexdump, the instruction opcode 0F7h
is first, then the ModR/M byte 06h
, then the little-endian offset word 0AA55h
, and then finally the immediate word 1234h
. The Intel syntax matches that order in the assembly source. In the AT&T syntax this would look like testw $0x1234, (0xAA55)
which swaps the order compared to the encoding.
Another example that obeys the Intel syntax order is comparison conditions. For example, consider this sequence:
cmp ax, 26
jae .label
This will jump to .label
if ax
is above-or-equal-to 26 (in unsigned comparison). This mnemonic is only true of the cmp dest, src
operand order, which sets flags as for dest -= src
.
Related videos on Youtube
claws
Updated on March 19, 2020Comments
-
claws about 4 years
MOV is probably the first instruction everyone learns while learning ASM.
Just now I encountered a book Assembly Language Programming in GNU/Linux for IA32 Architectures By Rajat Moona which says: (broken link removed)
But I learnt that it is
MOV dest, src
. Its like "Loaddest
withsrc
". Even Wiki says the same.I'm not saying that the author is wrong. I know that he is right. But what am I missing here?
btw.. he is using GCC's
as
to assemble these instructions. But that shouldn't change the instruction syntax right?-
claws about 14 yearsI should have read the preface and 1st paragraph of 1st chapter. I skipped them. :(
-
-
Paschalis almost 9 yearsSome people just want to watch the world burn.
-
Ruslan over 6 yearsNote that GNU assembler uses AT&T syntax by default, not unconditionally. You can switch it to Intel syntax using
.intel_syntax noprefix
directive. -
Peter Cordes over 5 yearsYASM has a gas syntax mode. clang's built-in assembler is gas-compatible and only supports GAS directives, so I guess you'd consider it a "Unix" assembler even when it's running on Windows, @fuz. Same for GAS on Windows, too, I guess? They have their origins in Unix.
-
fuz over 5 years@PeterCordes I was thinking along the lines of “what the
as
binary accepts on UNIX systems.” -
phuclv about 4 yearsthe more important thing is that Intel syntax matches everything in Intel and AMD's manual, whereas with the AT&T syntax you'll have trouble finding many instructions like movsbl...
-
ecm about 4 years@phuclv: There are (and can be) references and such involving the AT&T syntax too. But my point is that the machine code encoding fits only Intel syntax, so there's an objective reason to prefer it. The fact that most material uses Intel syntax is true too, but is incidental.
-
FreelanceConsultant almost 2 yearsIf I recall correctly there is some kind of command line switch, it might be
-Mintel
or something