主页 Swift UNIX C Assembly Go Plan 9 Web MCU Research Non-Tech

「Updating」The syntax style of Apple's as assembler

2023-05-19 | Assembly | #Words: 484

The syntax of assembly has two kinds in usual: AT&T and Intel syntx.

as is Apple’s assembler. The assembly code generated by Xcode is AT&T syntax (Unix birthplace). The Microsoft’s masm style that most people learned in college is Intel syntax (You know, it is “Wintel”), so this blog will shows some differences between them. However, there are too many details to write out all the differences at once, so I will continue to update them.

You may find a manual called “Mac OS X Assembler Guide”, but this manual is from 2005 and was written for the transition to Intel. But many things have changed now, and the current as manual was last updated on April 23, 2020, which is the day the M1 debuted.

last update of as manual on April 23, 2020

Register expression

In Intel syntax like masm, registers are written directly, for example:

mov eax, ebx

But in AT&T syntax like as, in order to not be confused with the identifier (usually it is called as variable), you need to add a percent sign % in front of the register, and it must be lowercase. as follows:

movq    %rsp, %rbp

Command parameters (the most important difference)

This is the most important difference! In masm, the assembly instruction representing a=a+b is as follows:

add a, b

will add the latter to the former.

But in as, the assembly instruction indicating a=a+b is the following structure:

add b, a

It is executed directly in sequence.

Value of instruction

In masm, if you want to set a numerical parameter, such as 10, you can write:

mov a, 10

But in as, you need to add $:

mov $10, a

It is few troublesome, but you can use parentheses to perform some simple calculations, as follows:

mov $(2*5), a

Address offset

In masm, if you want to represent an address offset (in many cases, it represents the address of a variable), as follows:

mov eax, [ebp+8]

But in as, its format is offset (base register, subscript register, scaling), which looks very complicated, but the most actual situation is simple as follows:

movl %edi, -4(%rbp)

Hexadecimal notation

In masm, hexadecimal numbers has suffix H or h, such as 5c0dH. But in as, it needs to be written with prefix 0x, such as 0x1234.