Assembly Language
Complete Notes
Registers to Procedures · Memory · Interrupts — All in Easy English
📋 Table of Contents — 13 Units
Introduction to Assembly Language
What is assembly and why is it the closest language to hardware?
What is Assembly Language?
Assembly language is a low-level programming language that uses mnemonic codes (like MOV, ADD, SUB) instead of binary machine code. It provides direct control over hardware and is specific to a processor architecture.
Why Learn Assembly?
- Understand how computers work at the hardware level
- Write highly optimized code for performance-critical applications
- Essential for system programming, device drivers, and embedded systems
- Reverse engineering and malware analysis
- Foundation for understanding operating systems
Programming Language Levels
| Level | Example | Readability | Control |
|---|---|---|---|
| Machine Code | 10110000 01100001 | Very Hard | Complete |
| Assembly | MOV AL, 61h | Hard | Complete |
| High-Level | int x = 97; | Easy | Abstract |
Assembler vs Compiler
| Feature | Assembler | Compiler |
|---|---|---|
| Input | Assembly code | High-level code |
| Output | Machine code | Machine code |
| Translation | 1:1 (one instruction = one machine code) | Many-to-many |
| Example | MASM, NASM | GCC, Javac |
Common Assemblers
- MASM — Microsoft Macro Assembler (Windows)
- NASM — Netwide Assembler (Cross-platform)
- TASM — Turbo Assembler (DOS/Windows)
- GAS — GNU Assembler (Linux)
CPU Architecture & Registers
Understanding the processor and its storage.
What is a Register?
A register is a small, fast storage location inside the CPU used to hold data temporarily during processing.
Types of Registers (8086/8088)
| Register | Size | Purpose |
|---|---|---|
| AX | 16-bit | Accumulator — arithmetic operations |
| BX | 16-bit | Base — memory addressing |
| CX | 16-bit | Counter — loop operations |
| DX | 16-bit | Data — I/O operations |
Segment Registers
| Register | Purpose |
|---|---|
| CS | Code Segment — holds code instructions |
| DS | Data Segment — holds data |
| SS | Stack Segment — holds stack |
| ES | Extra Segment — additional data |
Special Registers
| Register | Purpose |
|---|---|
| IP | Instruction Pointer — points to next instruction |
| SP | Stack Pointer — points to top of stack |
| BP | Base Pointer — base of stack frame |
| SI | Source Index — source for string operations |
| DI | Destination Index — destination for string operations |
FLAGS Register
Contains single-bit flags that indicate the status of the CPU and the result of operations.
- ZF (Zero Flag) — Set if result is zero
- CF (Carry Flag) — Set if carry/borrow occurs
- SF (Sign Flag) — Set if result is negative
- OF (Overflow Flag) — Set if signed overflow occurs
- PF (Parity Flag) — Set if result has even parity
Addressing Modes
Different ways to access data in memory.
What is an Addressing Mode?
An addressing mode specifies how the operand of an instruction is accessed.
Types of Addressing Modes
| Mode | Example | Description |
|---|---|---|
| Immediate | MOV AX, 5 | Data is part of the instruction |
| Register | MOV AX, BX | Data is in a register |
| Direct | MOV AX, [1234h] | Direct memory address |
| Indirect | MOV AX, [BX] | Address is in a register |
| Indexed | MOV AX, [SI] | Uses index register |
| Based | MOV AX, [BX+2] | Base register + offset |
Examples
; Immediate Addressing MOV AX, 10 ; AX = 10 ; Register Addressing MOV AX, BX ; AX = value in BX ; Direct Addressing MOV AX, [2000h] ; AX = value at memory location 2000h ; Register Indirect MOV AX, [BX] ; AX = value at address stored in BX
Data Transfer Instructions
Moving data between registers and memory.
MOV Instruction
The MOV instruction copies data from source to destination.
; Syntax: MOV destination, source MOV AX, 10 ; AX = 10 MOV BX, AX ; BX = AX MOV [1000h], AX ; Memory[1000h] = AX
XCHG Instruction
Exchanges (swaps) values between two operands.
MOV AX, 5 MOV BX, 10 XCHG AX, BX ; AX = 10, BX = 5
LEA, LDS, LES Instructions
| Instruction | Purpose | Example |
|---|---|---|
| LEA | Load Effective Address | LEA BX, [SI+2] |
| LDS | Load pointer using DS | LDS SI, [1000h] |
| LES | Load pointer using ES | LES DI, [2000h] |
PUSH and POP
; PUSH: Store value on stack MOV AX, 10 PUSH AX ; Stack now contains 10 ; POP: Retrieve value from stack POP BX ; BX = 10
Arithmetic Instructions
Performing calculations in assembly.
ADD and SUB
; ADD: Addition MOV AX, 5 MOV BX, 3 ADD AX, BX ; AX = 8 ; SUB: Subtraction SUB AX, BX ; AX = 5
INC and DEC
MOV AX, 10 INC AX ; AX = 11 (increment by 1) DEC AX ; AX = 10 (decrement by 1)
MUL and DIV
| Instruction | Operation | Result Location |
|---|---|---|
| MUL BX | AX = AL * BX (8-bit) | AX |
| MUL BX | DX:AX = AX * BX (16-bit) | DX:AX |
| DIV BX | AX = AX / BX | Quotient in AX, Remainder in DX |
; Multiplication MOV AX, 5 MOV BX, 3 MUL BX ; AX = 15 ; Division MOV AX, 10 MOV BX, 3 DIV BX ; AX = 3, DX = 1 (remainder)
NEG Instruction
Negates a number (two's complement).
MOV AX, 5 NEG AX ; AX = -5
Logical & Bitwise Instructions
Bit manipulation and logical operations.
Logical Instructions
| Instruction | Operation | Example |
|---|---|---|
| AND | Bitwise AND | AND AL, 0Fh |
| OR | Bitwise OR | OR AL, 0Fh |
| XOR | Bitwise XOR | XOR AX, AX (clears AX) |
| NOT | Bitwise NOT (invert) | NOT AL |
; AND Example MOV AL, 11110000b AND AL, 00001111b ; AL = 00000000b ; OR Example MOV AL, 11110000b OR AL, 00001111b ; AL = 11111111b ; XOR trick to clear register XOR AX, AX ; AX = 0 (faster than MOV AX, 0)
Shift Instructions
| Instruction | Operation | Example |
|---|---|---|
| SHL | Shift Left | SHL AL, 1 (multiply by 2) |
| SHR | Shift Right | SHR AL, 1 (divide by 2) |
| SAL | Shift Arithmetic Left | SAL AL, 2 |
| SAR | Shift Arithmetic Right | SAR AL, 2 |
MOV AL, 00001100b ; AL = 12 SHL AL, 1 ; AL = 00011000b = 24 SHR AL, 1 ; AL = 00001100b = 12
Rotate Instructions
| Instruction | Operation |
|---|---|
| ROL | Rotate Left |
| ROR | Rotate Right |
| RCL | Rotate Left through Carry |
| RCR | Rotate Right through Carry |
Control Flow Instructions
Jumps, loops, and decision making.
Unconditional Jump
JMP label ; Jump to label label: MOV AX, 5
Conditional Jumps
| Instruction | Condition | Flag Checked |
|---|---|---|
| JE / JZ | Jump if Equal / Zero | ZF = 1 |
| JNE / JNZ | Jump if Not Equal / Not Zero | ZF = 0 |
| JG / JNLE | Jump if Greater | Signed comparison |
| JL / JNGE | Jump if Less | Signed comparison |
| JA / JNBE | Jump if Above | Unsigned comparison |
| JB / JNAE | Jump if Below | Unsigned comparison |
CMP Instruction
Compares two values by subtracting them (without storing result) and sets flags.
MOV AX, 10 MOV BX, 5 CMP AX, BX ; Compare AX and BX JG greater ; Jump if AX > BX greater: MOV CX, 1
LOOP Instruction
MOV CX, 5 ; Counter loop_start: ; Your code here LOOP loop_start ; Decrement CX, jump if CX != 0
Example: Print 1 to 5
MOV CX, 5 MOV AX, 1 print_loop: ; Print AX value (pseudo-code) INC AX LOOP print_loop
Stack Operations
Understanding the stack — LIFO structure.
What is a Stack?
The stack is a LIFO (Last In, First Out) data structure used for temporary storage. SP (Stack Pointer) points to the top of the stack.
PUSH and POP
| Instruction | Operation | Effect on SP |
|---|---|---|
| PUSH AX | Store AX on stack | SP decreases by 2 |
| POP AX | Retrieve from stack to AX | SP increases by 2 |
MOV AX, 10 MOV BX, 20 PUSH AX ; Stack: [10] PUSH BX ; Stack: [20, 10] POP CX ; CX = 20, Stack: [10] POP DX ; DX = 10, Stack: []
Why Use the Stack?
- Save register values before using them
- Pass parameters to procedures
- Store return addresses for function calls
- Allocate local variables
PUSHF and POPF
Save and restore the FLAGS register.
PUSHF ; Save FLAGS register ; ... some operations ... POPF ; Restore FLAGS register
Procedures & Functions
Modular programming in assembly.
What is a Procedure?
A procedure (or subroutine) is a reusable block of code that can be called multiple times.
Defining a Procedure
myProc PROC ; Procedure code here RET ; Return to caller myProc ENDP
CALL and RET
| Instruction | Purpose |
|---|---|
| CALL | Call a procedure (pushes return address on stack) |
| RET | Return from procedure (pops return address) |
; Main program MOV AX, 5 CALL addTen ; Call procedure ; AX now contains 15 addTen PROC ADD AX, 10 RET addTen ENDP
Passing Parameters
Parameters can be passed via registers or the stack.
; Via Registers MOV AX, 10 MOV BX, 20 CALL addNumbers ; Result in AX addNumbers PROC ADD AX, BX RET addNumbers ENDP
Preserving Registers
myProc PROC PUSH AX ; Save register PUSH BX ; Procedure code POP BX ; Restore registers POP AX RET myProc ENDP
String Operations
Manipulating sequences of bytes.
String Instructions
| Instruction | Purpose | Registers Used |
|---|---|---|
| MOVSB | Move byte from DS:SI to ES:DI | SI, DI |
| MOVSW | Move word from DS:SI to ES:DI | SI, DI |
| CMPSB | Compare bytes | SI, DI |
| SCASB | Scan byte (search) | DI |
| LODSB | Load byte into AL from DS:SI | SI |
| STOSB | Store byte from AL to ES:DI | DI |
REP Prefix
Repeats a string instruction CX times.
MOV CX, 10 ; Repeat 10 times REP MOVSB ; Move 10 bytes
Example: Copy String
; Source string source DB 'Hello$' dest DB 6 DUP(0) ; Copy string LEA SI, source LEA DI, dest MOV CX, 6 REP MOVSB
Direction Flag
| Instruction | Effect |
|---|---|
| CLD | Clear Direction Flag (forward: SI++, DI++) |
| STD | Set Direction Flag (backward: SI--, DI--) |
Interrupts & BIOS Services
Interacting with hardware and system services.
What is an Interrupt?
An interrupt is a signal that temporarily stops the CPU to execute a special routine (ISR — Interrupt Service Routine).
INT Instruction
INT number ; Call interrupt
Common BIOS Interrupts
| Interrupt | Purpose |
|---|---|
| INT 10h | Video services |
| INT 16h | Keyboard services |
| INT 21h | DOS services (print, input, file I/O) |
| INT 13h | Disk services |
Example: Print Character (INT 10h)
MOV AH, 0Eh ; Function: Teletype output MOV AL, 'A' ; Character to print INT 10h ; Call BIOS video service
Example: Print String (INT 21h)
msg DB 'Hello, World!$' LEA DX, msg MOV AH, 09h ; Function: Display string INT 21h ; Call DOS service
Example: Read Keyboard (INT 16h)
MOV AH, 00h ; Function: Read key INT 16h ; Wait for key press ; AL now contains ASCII code
Program Termination
MOV AH, 4Ch ; Function: Terminate program INT 21h
Macros
Code templates for repeated patterns.
What is a Macro?
A macro is a code template that gets expanded at assembly time. It's like a function but is replaced inline.
Defining a Macro
macroName MACRO parameter1, parameter2 ; Macro body ENDM
Example: Simple Macro
addTen MACRO reg ADD reg, 10 ENDM ; Usage MOV AX, 5 addTen AX ; Expands to: ADD AX, 10
Macro vs Procedure
| Feature | Macro | Procedure |
|---|---|---|
| Execution | Code is copied inline | Code is called via CALL |
| Performance | Faster (no CALL/RET overhead) | Slower (function call overhead) |
| Code Size | Larger (code duplicated) | Smaller (code shared) |
| Use Case | Small, frequently used code | Large, complex functions |
Example: Print Character Macro
printChar MACRO char MOV AH, 02h MOV DL, char INT 21h ENDM ; Usage printChar 'A' printChar 'B'
Final Project — Calculator Program
Building a complete application.
Project Requirements
Build a simple Calculator that performs addition, subtraction, multiplication, and division.
Features to Implement
- Display a menu with options: 1. Add, 2. Subtract, 3. Multiply, 4. Divide, 5. Exit
- Take two numbers as input from the user
- Perform the selected operation
- Display the result
- Loop back to menu until user selects Exit
Sample Structure
.model small
.stack 100h
.data
menu DB '1. Add$'
; ... more menu items
.code
main PROC
MOV AX, @data
MOV DS, AX
menu_loop:
; Display menu
CALL displayMenu
; Get user choice
CALL getChoice
; Process choice
CMP AL, '1'
JE addition
; ... handle other cases
JMP menu_loop
addition:
CALL addNumbers
JMP menu_loop
main ENDP
END mainBonus Features
- Handle division by zero error
- Support multi-digit numbers
- Add modulus operation
- Display result in decimal format
- Use macros for repetitive tasks
📚 Complete Course Summary
| Unit | Topic | Key Concepts |
|---|---|---|
| 1 | Introduction to Assembly | Low-level programming, Assemblers, MASM/NASM |
| 2 | CPU Architecture & Registers | AX, BX, CX, DX, Segment Registers, FLAGS |
| 3 | Addressing Modes | Immediate, Register, Direct, Indirect, Indexed |
| 4 | Data Transfer | MOV, XCHG, LEA, PUSH, POP |
| 5 | Arithmetic Instructions | ADD, SUB, INC, DEC, MUL, DIV, NEG |
| 6 | Logical & Bitwise | AND, OR, XOR, NOT, SHL, SHR, ROL, ROR |
| 7 | Control Flow | JMP, JE, JNE, CMP, LOOP |
| 8 | Stack Operations | PUSH, POP, SP, LIFO, PUSHF, POPF |
| 9 | Procedures | PROC, CALL, RET, Parameter Passing |
| 10 | String Operations | MOVSB, CMPSB, LODSB, STOSB, REP |
| 11 | Interrupts & BIOS | INT 10h, INT 16h, INT 21h, ISR |
| 12 | Macros | MACRO, ENDM, Code Templates |
| 13 | Final Project | Calculator — Complete Application |
🎉 Congratulations!
You've completed the Assembly Language course! You now understand how computers work at the hardware level. Keep practicing! ⚙️🌟

