Skip to main content
Be Bitwise

Assembly Syntax

Assembly Language · Assembly & OS track

6 min read

Part ofSystems Programming Companion, week 3

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:

text
label: mnemonic operand1, operand2 ; comment
PartPurposeExample
LabelNames a location in the code (used for jumps)loop_start:
MnemonicThe instruction name (what to do)mov, add, jmp
OperandsWhat to operate on (registers, memory, constants)rax, 42
CommentHuman 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:

RegisterCommon Use
raxReturn values, arithmetic
rbxGeneral purpose
rcxLoop counter
rdxHigh half of multiply results, remainder after divide
rspStack pointer
rbpBase (frame) pointer
rdi, rsi, rdx, rcx, r8, r9The 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.

Key Concept

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:

InstructionMeaning
mov dst, srcCopy data from src to dst
add dst, srcAdd src to dst, store result in dst
sub dst, srcSubtract src from dst
cmp a, bCompare two values (sets flags, no stored result)
jmp labelJump to a label unconditionally
je labelJump if the last comparison was equal
call labelCall a function (pushes return address, jumps)
retReturn 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 rax or edi.
  • Immediate: a constant baked into the instruction, like 42 or 0x10.
  • Memory: anything in square brackets. [rbp-8] means "the memory at the address rbp minus 8", not the number rbp-8 itself.

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:

asm
.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-4

Take 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.

Try itChange the numbers in nums (keep the total under 10) and predict the output before you run it.
Assembly (NASM 2.14.02)

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:

asm
movl (%rdi,%rsi,4), %eax
addl %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.

  1. Intel: sub rsp, 16
  2. Intel: mov QWORD PTR [rbp-8], rdi
  3. Intel: lea rax, [rbx+rcx*8+24]
  4. AT&T: movl $0, -4(%rbp)
  5. AT&T: cmpq %rsi, %rdi
  6. AT&T: movzbl (%rax), %edx
Answers
  1. subq $16, %rsp
  2. movq %rdi, -8(%rbp)
  3. leaq 24(%rbx,%rcx,8), %rax
  4. mov DWORD PTR [rbp-4], 0. The l suffix is the only clue to the size, so Intel needs DWORD PTR.
  5. cmp rdi, rsi. Both compute rdi - rsi, so a following jg jumps when rdi is greater, whichever syntax you read it in. Reversed operand order makes cmp the easiest one to misread.
  6. movzx edx, BYTE PTR [rax]. AT&T packs two sizes into the mnemonic here: b for the source byte and l for 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.