MOV src, dest (or) MOV dest, src?
Asked Answered
S

4

28

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 "Load dest with src". 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?

Spanner answered 7/3, 2010 at 19:10 Comment(1)
I should have read the preface and 1st paragraph of 1st chapter. I skipped them. :(Spanner
I
68

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.

Inspirational answered 7/3, 2010 at 19:14 Comment(5)
Some people just want to watch the world burn.Klepht
Note that GNU assembler uses AT&T syntax by default, not unconditionally. You can switch it to Intel syntax using .intel_syntax noprefix directive.Spoor
YASM 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.Inebriate
@PeterCordes I was thinking along the lines of “what the as binary accepts on UNIX systems.”Leola
If I recall correctly there is some kind of command line switch, it might be -Mintel or somethingSupererogatory
Q
8

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.

Queenstown answered 7/3, 2010 at 19:14 Comment(0)
S
2

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.

Spoof answered 7/3, 2010 at 19:14 Comment(0)
R
1

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.

Rawlins answered 9/3, 2020 at 8:35 Comment(2)
the 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...Bowls
@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.Rawlins

© 2022 - 2024 — McMap. All rights reserved.