- Assembly is a low-level, human-readable language that closely reflects the binary machine code executed by the CPU.
- Operates on registers—64-bit “scratchpad” storage inside the processor.
- Assembly instructions correspond to operations on data inside registers or memory.
| Size Name | Bytes | Suffix |
| Byte | 1 | b |
| Word | 2 | w |
| Double | 4 | l |
| Quad | 8 | q |
| Size | Register |
| 64-bit | %rax |
| 32-bit | %eax |
| 16-bit | %ax |
| 8-bit | %al |
%rax – Return value
%rdi – First argument
%rsi – Second argument
%rdx – Third argument
%rsp – Stack pointer (top of stack)
%rip – Instruction pointer
movabsq $0x1122334455667788, %rax
- Use
movabsq to load a full 64-bit immediate constant into a register.
| Instruction | Effect |
movzbw | byte → word |
movzbl | byte → double |
movzwq | word → quad |
| Instruction | Effect |
movsbw | byte → word |
movsbl | byte → double |
movslq | double → quad |
cltq ; Sign-extend %eax into %rax
lea 6(%rax), %rdx
- Not a memory access — computes the address expression and places the result into the destination.
- Useful for address arithmetic.
| Expression | mov Meaning | lea Meaning |
6(%rax), %rdx | Read from memory at address | Compute 6 + %rax |
(%rax,%rcx),%rdx | Read from address %rax + %rcx | Compute %rax + %rcx |
(%rax,%rcx,4) | Read from %rax + 4 * %rcx | Compute %rax + 4 * %rcx |
7(%rax,%rax,8) | Read from 7 + %rax + 8 * %rax | Compute 7 + %rax + 8 * %rax |
incq 16(%rax)
dec %rdx
neg %rax
not %rcx
| Instruction | Description |
inc | Increment |
dec | Decrement |
neg | Negate (2’s complement) |
not | Bitwise NOT |
addq %rcx, (%rax)
xorq $16, (%rax, %rdx, 8)
subq %rdx, 8(%rax)
| Instruction | Effect |
add | D ← D + S |
sub | D ← D - S |
imul | D ← D * S |
xor | D ← D ^ S |
or | `D ← D |
and | D ← D & S |
- Result split across
%rdx:%rax.
imulq %rbx ; Signed multiply: %rax × %rbx → %rdx:%rax
mulq %rbx ; Unsigned multiply
%rdx:%rax is the dividend
- Operand is the divisor
%rax gets the quotient
%rdx gets the remainder
cqto ; Sign-extend %rax into %rdx
idivq %rdi ; Signed divide
divq %rdi ; Unsigned divide
shll $3, (%rax) ; logical left shift
shr %cl, (%rax,%rdx,8) ; logical right shift
sar $4, 8(%rax) ; arithmetic right shift
| Instruction | Meaning |
sal / shl | Left shift |
shr | Logical right shift (zero-fill) |
sar | Arithmetic right shift (sign-extend) |
- Shift amount can be an immediate or stored in
%cl.
%cl = 0xFF
shlb uses only the lowest 3 bits → shift by 7
shlw uses 4 bits → shift by 15
- Higher-width shifts use more bits.
int add_to_first(int x, int arr[]) {
int sum = x;
sum += arr[0];
return sum;
}
long full_divide(long x, long y, long *remainder_ptr) {
long quotient = x / y;
long remainder = x % y;
*remainder_ptr = remainder;
return quotient;
}