Assembly Syntax
Assembly Language · Assembly & OS track
6 min read
In this module
Anatomy of an Assembly Line
An assembly program is a stack of text lines, one instruction per line at most. A complete line carries up to four parts:
label: mnemonic operand1, operand2 ; comment| Part | Purpose | Example |
|---|---|---|
| Label | Names a location in the code (used for jumps) | loop_start: |
| Mnemonic | The instruction name (what to do) | mov, add, jmp |
| Operands | What to operate on (registers, memory, constants) | rax, 42 |
| Comment | Human explanation, ignored by assembler | ; load counter |
Not every line needs all four. Labels can sit alone, comments can sit alone, and most instructions take only the mnemonic and operands.
Registers as Variables
Assembly has no named variables. You work directly with the CPU's registers — a small handful of fast slots wired into the silicon. On x86-64, the general-purpose ones you'll meet first are:
| Register | Common Use |
|---|---|
rax | Return values, arithmetic |
rbx | General purpose |
rcx | Loop counter |
rdx | High half of multiply results, remainder after divide |
rsp | Stack pointer |
rbp | Base (frame) pointer |
rdi, rsi, rdx, rcx, r8, r9 | The first six function arguments, in that order (System V calling convention) |
Plenty more exist — r8 through r15, plus the floating-point and SIMD registers — but you'll see the ones above the most.
Assembly uses registers in place of named variables. A register is a tiny storage cell wired right into the CPU, with no memory traffic in the way. The programmer is the one deciding which register holds what.
Common Instructions
A starter set of x86-64 instructions you'll see in nearly every program:
| Instruction | Meaning |
|---|---|
mov dst, src | Copy data from src to dst |
add dst, src | Add src to dst, store result in dst |
sub dst, src | Subtract src from dst |
cmp a, b | Compare two values (sets flags, no stored result) |
jmp label | Jump to a label unconditionally |
je label | Jump if the last comparison was equal |
call label | Call a function (pushes return address, jumps) |
ret | Return from a function (pops return address, jumps) |
Three Kinds of Operand
Every operand is one of three things, and you can tell which at a glance:
- Register: a bare name, like
raxoredi. - Immediate: a constant baked into the instruction, like
42or0x10. - Memory: anything in square brackets.
[rbp-8]means "the memory at the addressrbpminus 8", not the numberrbp-8itself.
The brackets are the part to watch. mov rax, rbx copies a register. mov rax, [rbx] treats rbx as a pointer and loads whatever it points at. One pair of brackets is the whole difference between a value and a dereference.
When the instruction can't work out how many bytes to touch from the other operand, you spell it out with a size keyword: BYTE PTR (1 byte), WORD PTR (2), DWORD PTR (4) or QWORD PTR (8). mov DWORD PTR [rbp-4], 0 writes four zero bytes. Without the size, the assembler wouldn't know whether you meant one byte or eight.
Worked Example: Reading Compiler Output
Here's a line straight out of gcc -O0 output for a function that reads an array element:
.L3: ; label: a jump target the compiler invented mov eax, DWORD PTR [rdi+rsi*4] ; load 4 bytes from rdi + rsi*4 add DWORD PTR [rbp-4], eax ; add them to the local at rbp-4Take the first instruction apart. The mnemonic is mov, the destination is the register eax, and the source is memory. The address inside the brackets is rdi + rsi*4: the base of an int array plus the index times 4 bytes per element. That single operand is the machine version of arr[i]. The second line adds the loaded value to a local variable in place, memory as the destination this time. Together they're the body of a loop that sums an array.
Here's the same base + index*4 pattern in a program you can run. It's written for NASM, which spells sizes as DWORD [...] without the PTR that gcc and objdump print, and it ends with two Linux system calls: one to print the answer, one to exit.
Intel vs. AT&T Syntax
You'll run into two styles of x86 assembly in the wild. Intel syntax puts the destination first: mov rax, 5. AT&T syntax flips that, sources first, with sigils for good measure: movq $5, %rax. Same instructions underneath, different paint job. We'll use Intel here because it reads more naturally for most newcomers, and most modern documentation has come round to it too.
You'll still meet AT&T constantly, because it's the default for gcc, objdump and gdb on Linux. The same two lines from above look like this:
movl (%rdi,%rsi,4), %eaxaddl %eax, -4(%rbp)Four rules translate between them. Operand order is reversed. Registers get a % and immediates get a $. The size moves from DWORD PTR into a suffix on the mnemonic (b, w, l, q for 1, 2, 4 and 8 bytes). And memory operands are written offset(base,index,scale) instead of [base+index*scale+offset]. If the AT&T output bothers you, objdump -M intel and gdb's set disassembly-flavor intel switch it over.
Predict: translate the AT&T instructions addq $8, %rsp and movb %al, 1(%rdx) into Intel syntax.
add rsp, 8 and mov BYTE PTR [rdx+1], al. Swap the operand order, drop the % and $, and turn 1(%rdx) into [rdx+1]. The size suffixes mostly disappear in Intel syntax, because a register operand already pins the size: rsp is 8 bytes and al is 1. Disassemblers still print BYTE PTR on memory operands to make the width obvious, and you need it whenever there's no register to go by, as in mov BYTE PTR [rdx+1], 0.
Six more to drill the rules. Translate each into the other syntax, then check your answers.
- Intel:
sub rsp, 16 - Intel:
mov QWORD PTR [rbp-8], rdi - Intel:
lea rax, [rbx+rcx*8+24] - AT&T:
movl $0, -4(%rbp) - AT&T:
cmpq %rsi, %rdi - AT&T:
movzbl (%rax), %edx
Answers
subq $16, %rspmovq %rdi, -8(%rbp)leaq 24(%rbx,%rcx,8), %raxmov DWORD PTR [rbp-4], 0. Thelsuffix is the only clue to the size, so Intel needsDWORD PTR.cmp rdi, rsi. Both computerdi - rsi, so a followingjgjumps whenrdiis greater, whichever syntax you read it in. Reversed operand order makescmpthe easiest one to misread.movzx edx, BYTE PTR [rax]. AT&T packs two sizes into the mnemonic here:bfor the source byte andlfor the 4-byte destination.
Now classify the operands. Registers and immediates are easy to spot. The memory operands are the bracketed (or, in AT&T, parenthesised) ones in 2, 4 and 6. Number 3 is the trap: lea borrows memory-operand syntax, but it only calculates the address and never reads memory at all.