Assembly Language – University Level – Pak Notes Hub
⚙️ University Level — BS CS / BS IT

Assembly Language
Complete Notes

Registers to Procedures · Memory · Interrupts — All in Easy English

⚙️ 13 Units 🎓 University Level 💻 Code Examples 📝 Practice Tasks 🚀 Final Project
Unit 1

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.

💡 Assembly language is one step above machine code (binary) and one step below high-level languages like C or Java.

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

LevelExampleReadabilityControl
Machine Code10110000 01100001Very HardComplete
AssemblyMOV AL, 61hHardComplete
High-Levelint x = 97;EasyAbstract

Assembler vs Compiler

FeatureAssemblerCompiler
InputAssembly codeHigh-level code
OutputMachine codeMachine code
Translation1:1 (one instruction = one machine code)Many-to-many
ExampleMASM, NASMGCC, Javac

Common Assemblers

  • MASM — Microsoft Macro Assembler (Windows)
  • NASM — Netwide Assembler (Cross-platform)
  • TASM — Turbo Assembler (DOS/Windows)
  • GAS — GNU Assembler (Linux)
✏️ Practice: Install an assembler (NASM or emu8086) and verify it runs on your system.
Unit 2

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)

RegisterSizePurpose
AX16-bitAccumulator — arithmetic operations
BX16-bitBase — memory addressing
CX16-bitCounter — loop operations
DX16-bitData — I/O operations

Segment Registers

RegisterPurpose
CSCode Segment — holds code instructions
DSData Segment — holds data
SSStack Segment — holds stack
ESExtra Segment — additional data

Special Registers

RegisterPurpose
IPInstruction Pointer — points to next instruction
SPStack Pointer — points to top of stack
BPBase Pointer — base of stack frame
SISource Index — source for string operations
DIDestination 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
✏️ Practice: Write down the purpose of each general-purpose register (AX, BX, CX, DX).
Unit 3

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

ModeExampleDescription
ImmediateMOV AX, 5Data is part of the instruction
RegisterMOV AX, BXData is in a register
DirectMOV AX, [1234h]Direct memory address
IndirectMOV AX, [BX]Address is in a register
IndexedMOV AX, [SI]Uses index register
BasedMOV 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
✏️ Practice: Identify the addressing mode: MOV AX, 25 / MOV AX, BX / MOV AX, [SI]
Unit 4

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

InstructionPurposeExample
LEALoad Effective AddressLEA BX, [SI+2]
LDSLoad pointer using DSLDS SI, [1000h]
LESLoad pointer using ESLES 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
✏️ Practice: Write instructions to: load 25 into AX, copy AX to BX, swap AX and CX.
Unit 5

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

InstructionOperationResult Location
MUL BXAX = AL * BX (8-bit)AX
MUL BXDX:AX = AX * BX (16-bit)DX:AX
DIV BXAX = AX / BXQuotient 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
✏️ Practice: Write a program to add two numbers, multiply the result by 2, and store in BX.
Unit 6

Logical & Bitwise Instructions

Bit manipulation and logical operations.

Logical Instructions

InstructionOperationExample
ANDBitwise ANDAND AL, 0Fh
ORBitwise OROR AL, 0Fh
XORBitwise XORXOR AX, AX (clears AX)
NOTBitwise 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

InstructionOperationExample
SHLShift LeftSHL AL, 1 (multiply by 2)
SHRShift RightSHR AL, 1 (divide by 2)
SALShift Arithmetic LeftSAL AL, 2
SARShift Arithmetic RightSAR AL, 2
MOV AL, 00001100b   ; AL = 12
SHL AL, 1           ; AL = 00011000b = 24
SHR AL, 1           ; AL = 00001100b = 12

Rotate Instructions

InstructionOperation
ROLRotate Left
RORRotate Right
RCLRotate Left through Carry
RCRRotate Right through Carry
✏️ Practice: Use SHL to multiply a number by 4. Use SHR to divide by 2.
Unit 7

Control Flow Instructions

Jumps, loops, and decision making.

Unconditional Jump

JMP label          ; Jump to label

label:
    MOV AX, 5

Conditional Jumps

InstructionConditionFlag Checked
JE / JZJump if Equal / ZeroZF = 1
JNE / JNZJump if Not Equal / Not ZeroZF = 0
JG / JNLEJump if GreaterSigned comparison
JL / JNGEJump if LessSigned comparison
JA / JNBEJump if AboveUnsigned comparison
JB / JNAEJump if BelowUnsigned 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
✏️ Practice: Write a loop that adds numbers from 1 to 10 and stores the result in AX.
Unit 8

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

InstructionOperationEffect on SP
PUSH AXStore AX on stackSP decreases by 2
POP AXRetrieve from stack to AXSP 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
✏️ Practice: Push three values onto the stack, then pop them in reverse order.
Unit 9

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

InstructionPurpose
CALLCall a procedure (pushes return address on stack)
RETReturn 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
✏️ Practice: Write a procedure that multiplies a number in AX by 3 and returns the result.
Unit 10

String Operations

Manipulating sequences of bytes.

String Instructions

InstructionPurposeRegisters Used
MOVSBMove byte from DS:SI to ES:DISI, DI
MOVSWMove word from DS:SI to ES:DISI, DI
CMPSBCompare bytesSI, DI
SCASBScan byte (search)DI
LODSBLoad byte into AL from DS:SISI
STOSBStore byte from AL to ES:DIDI

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

InstructionEffect
CLDClear Direction Flag (forward: SI++, DI++)
STDSet Direction Flag (backward: SI--, DI--)
✏️ Practice: Write code to copy a 5-byte string from one memory location to another.
Unit 11

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

InterruptPurpose
INT 10hVideo services
INT 16hKeyboard services
INT 21hDOS services (print, input, file I/O)
INT 13hDisk 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
✏️ Practice: Write a program that prints "Pakistan" using INT 21h.
Unit 12

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

FeatureMacroProcedure
ExecutionCode is copied inlineCode is called via CALL
PerformanceFaster (no CALL/RET overhead)Slower (function call overhead)
Code SizeLarger (code duplicated)Smaller (code shared)
Use CaseSmall, frequently used codeLarge, complex functions

Example: Print Character Macro

printChar MACRO char
    MOV AH, 02h
    MOV DL, char
    INT 21h
ENDM

; Usage
printChar 'A'
printChar 'B'
✏️ Practice: Create a macro that swaps two registers using XOR operations.
Unit 13

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 main

Bonus Features

  • Handle division by zero error
  • Support multi-digit numbers
  • Add modulus operation
  • Display result in decimal format
  • Use macros for repetitive tasks
🚀 Final Project: Complete the calculator with all four operations, user input, error handling, and a looping menu.

📚 Complete Course Summary

UnitTopicKey Concepts
1Introduction to AssemblyLow-level programming, Assemblers, MASM/NASM
2CPU Architecture & RegistersAX, BX, CX, DX, Segment Registers, FLAGS
3Addressing ModesImmediate, Register, Direct, Indirect, Indexed
4Data TransferMOV, XCHG, LEA, PUSH, POP
5Arithmetic InstructionsADD, SUB, INC, DEC, MUL, DIV, NEG
6Logical & BitwiseAND, OR, XOR, NOT, SHL, SHR, ROL, ROR
7Control FlowJMP, JE, JNE, CMP, LOOP
8Stack OperationsPUSH, POP, SP, LIFO, PUSHF, POPF
9ProceduresPROC, CALL, RET, Parameter Passing
10String OperationsMOVSB, CMPSB, LODSB, STOSB, REP
11Interrupts & BIOSINT 10h, INT 16h, INT 21h, ISR
12MacrosMACRO, ENDM, Code Templates
13Final ProjectCalculator — Complete Application

🎉 Congratulations!

You've completed the Assembly Language course! You now understand how computers work at the hardware level. Keep practicing! ⚙️🌟