Tuesday, November 18, 2025

CPU Registers and Their types in details with examples.

 

CPU Registers and Their Types

What are CPU Registers?

Registers are small, high-speed storage locations inside the CPU used to hold data, addresses, and instructions temporarily while the processor is executing programs.

  • Faster than main memory (RAM)
  • Directly accessible by the CPU
  • Used to speed up processing

Types of CPU Registers

1. Accumulator (AC)

  • Stores intermediate results of arithmetic and logical operations.
  • Most operations use the accumulator by default.

Example:
For A = B + C, CPU loads B into AC, then adds C, and stores the result in AC.


2. Program Counter (PC)

  • Holds the address of the next instruction to be executed.
  • Automatically increments after fetching an instruction.

Example:
If the current instruction is at address 2000, PC = 2000. After fetching, PC = 2001.


3. Instruction Register (IR)

  • Stores the current instruction being executed.
  • Retrieved from memory using the address in the Program Counter.

Example:
If instruction at 2001 is ADD B, it will be loaded into the IR.


4. Memory Address Register (MAR)

  • Holds the memory address from which data will be fetched or to which data will be written.

Example:
If CPU needs data from address 3050, MAR = 3050.


5. Memory Buffer Register (MBR) / Memory Data Register (MDR)

  • Holds the data being transferred to or from memory.
  • Works along with MAR.

Example:

  • CPU wants to write value 42h at address 3050h
  • MAR = 3050h, MBR = 42h

6. General Purpose Registers (GPRs)

  • Temporarily hold data or intermediate results.
  • Examples: AX, BX, CX, DX in Intel 8086.

Example:
To compute (A + B) * C,

  • Store A and B in AX, BX → Add → result in AX
  • Store C in CX → Multiply AX and CX

7. Status Register / Flags Register

  • Stores flags indicating the status of operations:
    • Zero flag (Z) – set if result is 0
    • Carry flag (C) – set if there’s a carry/borrow
    • Sign flag (S) – set if result is negative
    • Overflow flag (O) – set on overflow

Example:
If you subtract 5 from 5 → result is 0 → Zero flag is set.


8. Stack Pointer (SP)

  • Points to the top of the stack in memory.
  • Stack is a LIFO (Last In First Out) memory area used for storing return addresses, local variables, etc.

Example:
During function calls, the return address is pushed to stack. SP updates accordingly.


9. Base Register and Index Register

  • Used for addressing modes in memory access.
  • Helps calculate effective addresses in array and structure access.


Summary Table

RegisterPurpose
ACIntermediate results
PCNext instruction address
IRCurrent instruction
MARAddress to access
MBR/MDRData to/from memory
GPRsTemporary data
SPStack management
FlagsOperation status
Base/IndexComplex addressing

Computer Instruction and it's types

Computer Instruction:

A computer instruction is a binary-encoded command given to the processor to perform a specific operation. Instructions tell the CPU what operation to execute, what data to use, and where to find that data.

Types of Computer Instructions:

Computer instructions are commands given to a processor to perform specific operations. They are classified based on their functionality and purpose. Here’s a breakdown of the main types:


1️⃣ Data Transfer Instructions

  • These instructions are used to move data from one location to another without modification.
  • Common operations include:
    • MOV — Move data from source to destination.
    • PUSH — Push data onto the stack.
    • POP — Pop data from the stack.
    • LOAD (LDA) — Load data from memory to a register.
    • STORE (STA) — Store data from a register to memory.

Example:

MOV AX, BX   ; Copy contents of BX into AX
PUSH AX      ; Push contents of AX onto the stack
POP BX       ; Pop the top value from the stack into BX

2️⃣ Arithmetic Instructions

  • These instructions perform arithmetic operations like addition, subtraction, multiplication, and division.
  • Common operations include:
    • ADD — Add two values.
    • SUB — Subtract one value from another.
    • MUL — Multiply two values.
    • DIV — Divide one value by another.
    • INC — Increment (add 1).
    • DEC — Decrement (subtract 1).

Example:

ADD AX, BX   ; AX = AX + BX
SUB AX, 10   ; AX = AX - 10
INC AX       ; AX = AX + 1

3️⃣ Logical Instructions

  • These instructions perform bitwise operations and logical comparisons.
  • Common operations include:
    • AND — Bitwise AND operation.
    • OR — Bitwise OR operation.
    • XOR — Bitwise XOR operation.
    • NOT — Bitwise NOT operation.
    • TEST — Test bits (like AND but without storing the result).

Example:

AND AX, BX   ; Perform AND operation between AX and BX
OR AX, 0xFF  ; Perform OR operation with 0xFF
NOT AX       ; Invert all bits in AX

4️⃣ Control Transfer Instructions

  • These instructions change the sequence of execution. They allow for loops, jumps, and conditional branches.
  • Common operations include:
    • JMP — Unconditional jump to a specified address.
    • JZ (Jump if Zero) — Jump if the Zero flag is set.
    • JNZ (Jump if Not Zero) — Jump if the Zero flag is not set.
    • CALL — Call a subroutine.
    • RET — Return from a subroutine.

Example:

JMP START         ; Jump unconditionally to START
JZ LOOP           ; Jump to LOOP if Zero flag is set
CALL SUBROUTINE   ; Call a subroutine
RET               ; Return from subroutine

5️⃣ Input/Output Instructions

  • These instructions are used to read data from input devices and send data to output devices.
  • Common operations include:
    • IN — Read data from an I/O port.
    • OUT — Write data to an I/O port.

Example:

IN AL, 60h     ; Read data from port 60h into AL
OUT 70h, AL    ; Output data from AL to port 70h

6️⃣ Shift and Rotate Instructions

  • These instructions shift bits left or right or rotate bits around within a register.
  • Common operations include:
    • SHL — Shift bits to the left.
    • SHR — Shift bits to the right.
    • ROL — Rotate bits to the left.
    • ROR — Rotate bits to the right.

Example:

SHL AX, 1     ; Shift AX left by 1 bit (multiply by 2)
ROR BX, 2     ; Rotate BX right by 2 bits

7️⃣ Machine Control Instructions

  • These instructions control the operation of the processor.
  • Common operations include:
    • HLT — Halt the processor.
    • NOP — No operation (just a wait).
    • WAIT — Wait for an interrupt.
    • ESC — Escape to external devices.

Example:

NOP         ; Do nothing, just wait
HLT         ; Stop processor execution

Summary Table:

Instruction TypeCommon InstructionsPurpose
Data TransferMOV, PUSH, POP, LOAD, STOREMove data between registers or memory
ArithmeticADD, SUB, MUL, DIV, INC, DECPerform arithmetic calculations
LogicalAND, OR, XOR, NOT, TESTPerform bitwise and logical operations
Control TransferJMP, JZ, JNZ, CALL, RETControl the flow of program execution
Input/OutputIN, OUTRead from or write to I/O devices
Shift and RotateSHL, SHR, ROL, RORShift or rotate bits in registers
Machine ControlHLT, NOP, WAIT, ESCControl processor operations


Instruction Formats and it's types

Instruction Format defines the layout of bits in an instruction, specifying the structure of an operation. It includes details like the opcodeoperands, and addressing mode. The format determines how the CPU interprets and executes instructions.


Main Components of an Instruction Format:

FieldDescription
OpcodeSpecifies the operation to be performed (e.g., ADD, MOV).
Operand(s)Specifies the data or the addresses of data for the operation.
Addressing ModeIndicates how to interpret the operands (e.g., direct, indirect).
Mode Bits(Optional) Specifies special operation modes.

Types of Instruction Formats:


1️⃣ Zero-Operand (0-Address) Instruction Format

  • Typically used in Stack-based Architectures.
  • All operations are performed on the stack.
  • No operand is specified; the CPU automatically uses the top elements of the stack.

Example:

PUSH 10     ; Push 10 onto the stack
PUSH 20     ; Push 20 onto the stack
ADD         ; Pop two values, add them, and push the result back
Opcode
ADD

2️⃣ One-Operand (1-Address) Instruction Format

  • Contains only one operand; the second operand is implied (usually the Accumulator (AC)).
  • Efficient for simple operations and memory access.

Example:

LOAD 1000   ; Load the value from memory address 1000 into AC
ADD 1001    ; Add the value from address 1001 to AC
STORE 1002  ; Store the result back to address 1002
OpcodeAddress
ADD1001

3️⃣ Two-Operand (2-Address) Instruction Format

  • Contains two operands: a source and a destination.
  • The result is stored in the destination operand.

Example:

MOV AX, BX  ; Move the contents of BX to AX
ADD AX, CX  ; Add the contents of CX to AX
OpcodeSourceDestination
MOVBXAX

4️⃣ Three-Operand (3-Address) Instruction Format

  • Contains three operands: two sources and one destination.
  • Used in RISC (Reduced Instruction Set Computer) architectures for more complex operations.

Example:

ADD R1, R2, R3   ; R1 = R2 + R3
SUB R4, R5, R6   ; R4 = R5 - R6
OpcodeOperand 1Operand 2Destination
ADDR2R3R1

5️⃣ Variable-Length Instruction Format

  • Instructions can have different lengths.
  • Common in CISC (Complex Instruction Set Computer) architectures.
  • More flexible but more complex to decode.

Example:

MOV AX, 1234      ; 16-bit instruction
MOV BX, [5678]    ; 32-bit instruction
OpcodeOperands (Variable Size)
MOVAX, 1234

Comparison of Instruction Formats:

Format TypeOperandsUsageArchitecture
0-AddressNoneStack operationsStack Machines
1-AddressOneSimple memory operationsAccumulator-based
2-AddressTwoData transfer and arithmeticGeneral Purpose
3-AddressThreeComplex arithmetic and logicRISC Processors
Variable-LengthVariesFlexible operationsCISC Processors


Instruction Cycle in Computer Architecture

The Instruction Cycle is the complete process in which a computer fetches, decodes, executes, and sometimes stores the result of an instruction. This cycle is repeated for each instruction in a program.


Phases of the Instruction Cycle:

1. Fetch Cycle:

  • The Program Counter (PC) contains the address of the next instruction.
  • The address is sent to Memory to fetch the instruction.
  • The fetched instruction is stored in the Instruction Register (IR).
  • The PC is incremented to point to the next instruction.

Example:

PC → MAR       ; Program Counter address moved to Memory Address Register
MEM[MAR] → MDR ; Data fetched from memory into Memory Data Register
MDR → IR       ; Data moved to Instruction Register
PC = PC + 1    ; Increment Program Counte

2. Decode Cycle:

  • The Control Unit (CU) reads the instruction from the IR.
  • It interprets the opcode (operation code) to understand the action required.
  • The operands and addressing modes are identified.

Example:

IR → CU             ; Instruction decoded by Control Unit
Opcode → Operation  ; Identify the operation (e.g., ADD, MOV)
Operands → Addressing Mode Interpretation 3.

3. Execute Cycle:

  • The ALU (Arithmetic Logic Unit) or relevant hardware executes the operation.
  • Operations can be arithmetic (ADD, SUB)logical (AND, OR), or data transfer.
  • If required, memory or register content is read or modified.

Example:

ADD AX, BX         ; AX = AX + BX
MOV CX, 1000       ; Move value 1000 to CX4. 

4. Memory Access (Optional):

  • If the instruction involves memory read/write, this phase is triggered.
  • Data is either fetched from or stored into the specified memory location.

Example:

LOAD AX, [1000]    ; Load the value at address 1000 into AX
STORE AX, [2000]   ; Store the value of AX into address 2000

5. Write Back (Optional):

  • If the operation produces a result, it may need to be stored back to memory or a register.
  • This step is skipped for instructions like JMP or NOP.

Example:

MDR → [Address]    ; Move data from Memory Data Register to memory
AX → [2000]        ; Store the result in memory location 2000

6. Interrupt Check (Optional):

  • At the end of each cycle, the CPU checks if there are pending interrupts.
  • If an interrupt exists, the CPU executes the Interrupt Service Routine (ISR) before the next instruction cycle.

Graphical Representation of the Instruction Cycle:

+------------------+      +------------------+      +------------------+
|   Fetch Cycle   | ---> |   Decode Cycle   | ---> |   Execute Cycle  |
+------------------+      +------------------+      +------------------+
        |                       |                       |
        V                       V                       V
+------------------+      +------------------+      +------------------+
|   Memory Access | ---> |   Write Back    | ---> | Interrupt Check  |
+------------------+      +------------------+      +------------------+

Summary Table:

PhasePurposeRegisters Used
FetchGet the instruction from memoryPC, MAR, MDR, IR
DecodeInterpret the instructionIR, CU
ExecutePerform the operationALU, Registers
Memory AccessRead/write data if neededMAR, MDR
Write BackStore the result backRegisters, Memory
Interrupt CheckCheck for and handle interruptsInterrupt Register (if any)


Pipelining in Computer Architecture

Pipelining is a technique used in modern processors to overlap the execution of multiple instructions, thereby improving the overall performance and throughput of the CPU. It breaks down the instruction cycle into separate stages, allowing different instructions to be processed simultaneously.


Why Pipelining?

Without pipelining, the CPU would process one instruction at a time, waiting for each phase (Fetch → Decode → Execute → Write Back) to complete before starting the next instruction. This creates idle time.
With pipelining, the CPU starts executing the next instruction before the previous one finishes, making more efficient use of processor resources.


Phases of the Pipeline:

StageDescription
1. Fetch (IF)The instruction is fetched from memory and loaded into the Instruction Register (IR).
2. Decode (ID)The Control Unit (CU) decodes the opcode and identifies the operands and addressing mode.
3. Execute (EX)The Arithmetic Logic Unit (ALU) performs the required operation (add, subtract, etc.).
4. Memory Access (MEM)Data is read from or written to memory if needed.
5. Write Back (WB)The result is written back to the register or memory location.

Example of a Pipelined Execution:

Assume we have three instructions: I1, I2, I3

Clock CycleFetch (IF)Decode (ID)Execute (EX)Memory (MEM)Write Back (WB)
1I1
2I2I1
3I3I2I1
4I4I3I2I1
5I5I4I3I2I1
6I6I5I4I3I2
  • At Cycle 5, five different instructions are in five different stages.
  • Instruction I1 is in the Write Back stage, while I5 is just being fetched.

Speedup Calculation:

If there are n stages in the pipeline, ideally the speedup is approximately:

Speedup = n

Pipeline Hazards:

While pipelining increases speed, it also introduces challenges known as pipeline hazards:

  1. Data Hazards: Occur when instructions depend on the results of previous instructions.

    • Example: ADD R1, R2 followed by SUB R3, R1 → SUB needs the result of ADD.
  2. Control Hazards: Occur during branch instructions (JMPCALL) where the next instruction address is unknown until execution.

    • Example: If a jump is taken, the pipeline may have fetched the wrong instructions.
  3. Structural Hazards: Occur when hardware resources (like memory or registers) are insufficient for parallel execution.


Solutions to Hazards:

Hazard TypeSolution Techniques
Data HazardsForwarding (Bypassing)Stalling
Control HazardsBranch PredictionDelay Slots
Structural HazardsPipeline InterleavingResource Duplication

Benefits of Pipelining:

  • Increased Throughput: More instructions are executed in less time.
  • Higher CPU Utilization: Less idle time for the processor.
  • Efficient Use of Hardware: Simultaneous usage of ALU, memory, and registers.

Drawbacks of Pipelining:

  • Complex Control Logic: More sophisticated handling of dependencies.
  • Hazard Management: Requires additional mechanisms to handle hazards.
  • Branch Penalties: Incorrect predictions can cause delays.

Addressing modes in computer architecture

Addressing modes are techniques used in computer architecture to specify the location of an operand (data) required by an instruction. These modes determine how the instruction retrieves its operands. Here are the main types of addressing modes with examples:

1. Immediate Addressing Mode

  • The operand is directly specified in the instruction.
  • Example:
    MOV A, #5  ; Move the value 5 directly into register A
    

2. Register Addressing Mode

  • The operand is in a register, and the register's name is specified in the instruction.
  • Example:
    MOV A, B   ; Move the contents of register B to register A
    

3. Direct Addressing Mode

  • The effective address of the operand is given explicitly in the instruction.
  • Example:
    MOV A, 500 ; Move the value at memory address 500 to register A
    

4. Indirect Addressing Mode

  • The effective address of the operand is held in a register or memory location.
  • Example:
    MOV A, @R0 ; Move the value at the address pointed to by R0 into register A
    

5. Indexed Addressing Mode

  • The effective address is generated by adding a constant value to the contents of a register.
  • Example:
    MOV A, 2000(R1) ; Add 2000 to the contents of R1, and fetch the value from that address into A
    

6. Relative Addressing Mode

  • The effective address is determined by the sum of the current program counter (PC) and an offset value.
  • Example:
    JMP LABEL   ; Jump to the address calculated by PC + offset
    

7. Base Register Addressing Mode

  • A base address stored in a base register is added to an offset to find the effective address.
  • Example:
    MOV A, 1000(BX) ; Fetch the value from [BX + 1000] into register A
    


Memory organization in computer architecture

Memory Organization in Detail

Memory organization is the structure and management of computer memory. It is crucial for optimal data processing and efficient program execution. Here’s a breakdown of how it is organized:


1. Types of Memory

  • Primary Memory (Volatile):

    • RAM (Random Access Memory): Temporary storage for active processes and data.
    • Cache Memory: Faster than RAM; stores frequently accessed data for quick retrieval.
    • Registers: Small, fast storage within the CPU for immediate processing.
  • Secondary Memory (Non-Volatile):

    • Hard Disk Drives (HDD), Solid State Drives (SSD): Long-term storage for OS, software, and files.
    • Optical Drives (CD, DVD): Used for distributing software and media.
    • Flash Drives, SD Cards: Portable storage options.
  • Tertiary Memory (Backup Storage):

    • Magnetic Tape, Cloud Storage: Mainly for backup and archival.

2. Memory Hierarchy

  • Organized in layers to balance speed, cost, and capacity:
    1. Registers — Fastest but smallest capacity (in CPU).
    2. Cache Memory — Divided into L1, L2, and sometimes L3 (on-chip and off-chip).
    3. RAM — Main memory, slower than cache but larger.
    4. Storage (HDD/SSD) — Persistent, large capacity, slower access.

3. Memory Addressing

  • Byte Addressable: Each byte has a unique address.
  • Word Addressable: Each word (2, 4, or 8 bytes) is addressable.
  • 32-bit vs. 64-bit Addressing: Determines the maximum addressable memory (4GB for 32-bit, 16EB for 64-bit).

4. Memory Mapping Techniques

  • Direct Mapping: Each block of main memory maps to only one cache line.
  • Associative Mapping: Any block of main memory can load into any line of the cache.
  • Set-Associative Mapping: A middle ground where memory maps to a set of cache lines.

5. Virtual Memory

  • Extends RAM by using a portion of secondary storage (disk) as additional memory.
  • Managed through paging and segmentation.
  • Paging: Divides memory into fixed-size pages.
  • Segmentation: Divides memory into variable-size segments based on logical divisions.

6. Memory Protection and Management

  • Ensures programs do not interfere with each other's memory space.
  • Memory Management Unit (MMU): Handles address translation and protection.
  • Protection Techniques: Paging, segmentation, privilege levels.

7. Interleaved Memory Organization

  • Divides memory into multiple modules to improve access times.
  • Allows simultaneous data access from different modules.