COA Notes
Complete guide to assembly language instructions including data transfer, arithmetic, logic, and control flow.
Introduction
Assembly instructions are the actual commands a CPU understands — the lowest-level human-readable programming language. Each instruction maps directly to a binary machine code operation. While high-level languages like C or Python handle thousands of things in one statement, an assembly instruction does exactly one thing: move data, add two numbers, compare values, or jump to another location. Understanding assembly instructions helps you see what the CPU actually does, which makes you a better programmer, debugger, and system designer.
Instruction Categories
Assembly instructions fall into these major categories:
| Data Transfer | Arithmetic & | Control Flow |
|---|---|---|
| Logic | ||
| MOV, LOAD | ADD, SUB | JMP, CALL |
| STORE, PUSH | MUL, DIV | RET, Bcc |
| POP, XCHG | AND, OR, XOR | LOOP, INT |
| LEA, IN/OUT | SHL, SHR, ROT | NOP, HLT |
Data Transfer Instructions
MOV (Move/Copy)
The most used instruction — copies data from source to destination:
Key rule in x86: You cannot MOV from memory to memory directly. You must go through a register. MIPS enforces this even more strictly — only LW/SW access memory.
PUSH and POP (Stack Operations)
LEA (Load Effective Address)
LEA calculates an address but does not access memory:
Arithmetic Instructions
Addition and Subtraction
Flags affected: Addition/subtraction set carry (C), zero (Z), sign (N), and overflow (V) flags.
Multiplication and Division
; MIPS
mul $t0, $t1, $t2 ; $t0 = $t1 × $t2 (lower 32 bits)
mult $t1, $t2 ; Hi:Lo = $t1 × $t2 (64-bit result)
mflo $t0 ; $t0 = Lo (lower 32 bits)
mfhi $t1 ; $t1 = Hi (upper 32 bits)
div $t1, $t2 ; Lo = $t1/$t2 (quotient), Hi = $t1%$t2 (remainder)
; x86
mul ebx ; EDX:EAX = EAX × EBX (unsigned)
imul ebx ; EDX:EAX = EAX × EBX (signed)
div ecx ; EAX = EDX:EAX / ECX, EDX = remainderWhy multiply produces double-width result: Multiplying two 32-bit numbers can produce a 64-bit result (e.g., 0xFFFFFFFF × 0xFFFFFFFF needs 64 bits). The hardware provides both halves.
Logic Instructions
Bitwise Operations
; MIPS
and $t0, $t1, $t2 ; Bitwise AND
or $t0, $t1, $t2 ; Bitwise OR
xor $t0, $t1, $t2 ; Bitwise XOR
nor $t0, $t1, $t2 ; Bitwise NOR (NOT OR)
andi $t0, $t1, 0xFF ; AND with immediate (mask lower byte)
; x86
and eax, 0x0F ; Keep only lower 4 bits
or eax, 0x80 ; Set bit 7
xor eax, eax ; Clear register (fastest way to zero a register!)
not eax ; Flip all bits (1's complement)
test eax, ebx ; AND without storing result (only sets flags)Common Bit Manipulation Patterns
; Check if bit N is set
and $t0, $t1, (1 << N) ; Result non-zero if bit N is set
; Clear bit N
andi $t0, $t1, ~(1 << N) ; Mask off bit N
; Set bit N
ori $t0, $t1, (1 << N) ; OR with bit mask
; Toggle bit N
xori $t0, $t1, (1 << N) ; XOR flips the bitShift and Rotate Instructions
; MIPS
sll $t0, $t1, 3 ; Shift left logical by 3 (multiply by 8)
srl $t0, $t1, 2 ; Shift right logical by 2 (unsigned divide by 4)
sra $t0, $t1, 1 ; Shift right arithmetic (signed divide by 2)
; x86
shl eax, 1 ; Shift left (multiply by 2)
shr eax, 3 ; Shift right logical (unsigned divide by 8)
sar eax, 1 ; Shift right arithmetic (preserves sign)
rol eax, 4 ; Rotate left (bits wrap around)
ror eax, 4 ; Rotate rightWhy shifts matter: Shift left by N = multiply by 2ᴺ. This is much faster than MUL instruction (1 cycle vs 3-5 cycles). Compilers convert multiplication by constants into shifts and adds.
Comparison and Test Instructions
; MIPS
slt $t0, $t1, $t2 ; Set if Less Than: $t0 = ($t1 < $t2) ? 1 : 0
slti $t0, $t1, 100 ; Set if less than immediate
beq $t1, $t2, label ; Branch if equal
bne $t1, $t2, label ; Branch if not equal
; x86
cmp eax, ebx ; Compute eax - ebx, set flags (discard result)
test eax, eax ; Compute eax AND eax, set flags (check if zero)CMP sets flags as if subtraction happened but does not store the result — it only affects flags for subsequent conditional branches.
Control Flow Instructions
Unconditional Jump
; MIPS
j target ; Jump to address (26-bit pseudo-direct)
jr $ra ; Jump to address in register (return)
jal function ; Jump and link ($ra = PC+4, then jump)
; x86
jmp label ; Unconditional jump
call function ; Push return address, jump to function
ret ; Pop return address, jump to itConditional Branches
; MIPS (compare and branch in separate instructions)
beq $t0, $t1, equal_label ; Branch if $t0 == $t1
bne $t0, $t1, not_equal ; Branch if $t0 != $t1
bgtz $t0, positive ; Branch if $t0 > 0
bltz $t0, negative ; Branch if $t0 < 0
; x86 (branch based on flags from previous CMP/TEST)
je label ; Jump if Equal (ZF=1)
jne label ; Jump if Not Equal (ZF=0)
jg label ; Jump if Greater (signed)
jl label ; Jump if Less (signed)
ja label ; Jump if Above (unsigned)
jb label ; Jump if Below (unsigned)
jc label ; Jump if Carry
jo label ; Jump if OverflowSystem Instructions
; MIPS system calls
li $v0, 1 ; Syscall code 1 = print integer
move $a0, $t0 ; Argument = value to print
syscall ; Execute system call
; x86 interrupts
int 0x80 ; Linux system call (32-bit)
syscall ; Linux system call (64-bit)
hlt ; Halt processor (wait for interrupt)
nop ; No operation (pipeline placeholder)Instruction Encoding
Every assembly instruction is encoded as a binary number:
MIPS R-type (register operations)
[opcode:6][rs:5][rt:5][rd:5][shamt:5][funct:6] = 32 bits
Example: ADD $t0, $t1, $t2
[000000][01001][01010][01000][00000][100000]
opcode $t1 $t2 $t0 shift ADD
MIPS I-type (immediate operations)
[opcode:6][rs:5][rt:5][immediate:16] = 32 bits
Example: ADDI $t0, $t1, 100
[001000][01001][01000][0000000001100100]
ADDI $t1 $t0 100
Key Takeaways
- Data transfer instructions (MOV, LW, SW) are the most frequently executed — up to 40% of instructions in typical programs
- Arithmetic and logic operations are performed by the ALU using values from registers — memory operands must be loaded first (in RISC)
- Shifts are the fastest way to multiply/divide by powers of 2 — compilers use them aggressively
- CMP/TEST set flags without storing results — separating comparison from branching gives the pipeline more flexibility
- Every assembly instruction maps directly to binary machine code — understanding encoding helps debug at the hardware level
- Control flow instructions (branches, jumps, calls) modify the PC — they are what make programs do different things based on conditions
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Assembly Instructions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, assembly, language, instructions, assembly instructions
Related Computer Organization & Architecture Topics