IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

FCS 13 Sep

13 Sep 20262h 56m5 parts

Source: IITP-big-files\Sem-1\ECS5102-FCS\Lectures\2026-09-13 0000 FCS 13 Sep\summary\notes.md

Overview

This class introduced the CPU as the central hardware component for program execution. It covered the physical structure of a CPU, including cores, caches (L1, L2, L3), and internal units like the control unit, ALU, and registers. The session traced the flow of a simple program (adding two integers) from secondary storage through RAM, CPU caches, registers, and the ALU, highlighting the roles of different components. It also distinguished between compiled and interpreted languages at the decode level, explained the storage hierarchy, and introduced the four-phase instruction cycle (fetch, decode, execute, write-back). This lecture establishes the foundational hardware context for understanding how machine code is executed.

Learning outcomes

  • Identify and describe the key components inside a CPU and their roles (cores, caches, control unit, ALU, registers).
  • Trace the path of data and instructions for a simple operation (e.g., integer addition) from storage to output.
  • Explain the storage hierarchy (registers, cache, RAM, secondary storage) in terms of speed and capacity.
  • Describe the four-phase instruction cycle (fetch, decode, execute, write-back).
  • Distinguish how compiled and interpreted languages are processed at the CPU decode level.
  • Explain the role of cache memory (L1, L2, L3) in instruction and data flow.

Core concepts

CPU Components and Roles

The CPU is a palm-sized chip containing multiple processing cores, a cache hierarchy, and specialized units. Cores are individual processing units. The control unit manages instruction flow and decoding but does not handle data. The ALU (Arithmetic Logic Unit) and FPU are execution units that perform calculations. Registers (e.g., R1, R2) are very fast, small storage locations inside the core for holding operands and immediate results. This structure matters because it defines the hardware environment where all software ultimately runs.

Storage Hierarchy

A ranking of storage types by speed and capacity: Registers (fastest, smallest, inside CPU core), Cache (L1/L2 per core, L3 shared, faster than RAM), Main Memory (RAM) (directly accessible, larger, slower), and Secondary Storage (HDD/SSD, permanent, slowest). This hierarchy is crucial for performance; the CPU tries to keep frequently used data in the fastest storage levels.

Cache Hierarchy (L1, L2, L3)

L1 cache is the fastest and smallest, split into instruction (L1I) and data (L1D) caches, one per core. L2 cache is larger and slower, also one per core, acting as a queue for instructions before they are split for L1. L3 cache is shared by all cores and used for storage/swapping when registers are full or for inter-core communication, not for incoming instructions. Caches temporarily hold data and instructions to bridge the speed gap between the CPU and RAM.

An L1 miss does not automatically mean a trip to DRAM: if the line is present in L2, the processor can get the data from L2 without necessarily touching DRAM. Only when the line is absent from the lower levels does the full DRAM penalty apply.

Instruction Cycle

The basic cycle a CPU follows to process each instruction: Fetch (read instruction from memory into IR, increment PC), Decode (control unit interprets the instruction), Execute (operation performed in a unit like the ALU), and Write-back (result stored to a register or memory). This cycle is fundamental to understanding how a sequence of machine code commands is carried out.

Compiled vs. Interpreted Languages at CPU Level

Compiled programs (e.g., EXE, JAR) are already decoded into assembly language, so the CPU only needs to convert assembly to machine language (binary). Interpreted languages require the CPU to decode at two levels (high-level language to assembly, then assembly to machine), making them slower. This distinction highlights the relationship between software abstraction and hardware execution time.

CPU Bit-width and OS

A 32-bit CPU can process up to 32 bits in one operation. It can run a 16-bit OS (underutilized) but cannot fully support a 64-bit OS; processing will be limited to 32 bits. The OS bit-width (e.g., 64-bit) is often wider to handle complex communication with memory and devices, while the CPU's primary processing may still use 32-bit instructions.

Instruction Set Architecture (ISA)

The ISA is the software-visible contract between binary and hardware: it defines the instruction formats and their encoding. Instruction length is not required to be the same across architectures — the ISA defines the formats, and length may vary between architectures. Two processors with the same ISA can run the same binary even with different pipelines and caches, because the microarchitecture implements the ISA; the ISA does not fix the pipeline, caches or execution units. The ISA is the binary contract, though a binary can still fail on a same-ISA processor that lacks a required optional extension (e.g. AVX-512, or a newer instruction) — hence "required features".

Addressing Modes

Immediate addressing means the operand value is encoded as part of the instruction itself; the constant comes from the instruction, not from a register or memory. Register-indirect addressing uses a register as an address: MOV R1, R4 copies the register's contents, while MOV R1, (R4) / LDR R1, [R4] uses R4 as an address. Confusing the two is a common conceptual error.

Instruction Categories

Moving bits from one location to another without arithmetic is not arithmetic: using ADD to move data also destroys the carry/overflow flags and is slower. This is why ISAs have a dedicated MOV/LD/ST family.

Register Allocation and Spilling

When a program has more simultaneously live values than general-purpose registers, the appropriate response is spilling: the allocator keeps the hottest values in registers and writes the rest to the stack frame. Spills are a major source of extra memory traffic; the term for this situation is register pressure.

Condition Flags and Branching

Conditionals branch on flags (Z, N, C, V); a wrong flag write-back or a wrong condition code sends execution down the wrong path. On x86 this is the EFLAGS register; the mnemonic JZ tests ZF.

Write-back Caches

In a write-back cache, a modified line is updated and marked dirty; write-down to the next level is deferred to a later write-back event.

ALU Control Signals

If the operands are correct but the operation is wrong, the select lines are wrong — the control unit decoded/asserted the wrong ALU op. The diagnostic split is "data path right, control wrong"; data errors show up as wrong values.

Toolchain Flow

The conceptual translation from a programmer's requirement to processor execution goes source → compiler → assembly → assembler → linker → loader → execution.

Formulas and maths

Not covered in this session.

Worked examples

Example: Adding Two Integers (10 + 25)

  1. Program stored on secondary storage (HDD/SSD).
  2. User inputs 10 and 25 via keyboard (I/O controller).
  3. Input transferred to RAM. RAM creates instructions and allocates variables.
  4. Memory controller takes instructions/data to CPU.
  5. Control unit fetches and controls flow; instruction decoder decodes to machine language.
  6. Data moved to CPU registers: e.g., R1=10, R2=25.
  7. ALU executes addition: 10 + 25 = 35.
  8. Result stored in register R3=35.
  9. Result can be sent back to RAM or hard disk.
  10. Result output to user via I/O controller to screen. Cache behavior: After operation, R1 and R2 are released; result (35) may be cached (L3 cache mentioned for infrequent reuse).

Instruction Format Example

A common 32-bit instruction format was described: - Bits 26-31: Opcode (6 bits). Example: 000000 for ADD. - Bits 21-25 and 16-20: Register fields (5 bits each). Example: For add R1, R2, R3, the machine code representation places the result register (R3) first after the opcode, then the source registers. The control unit decodes this.

Scale of Storage Hierarchy

  • Registers: few KBs.
  • Cache: few MBs maximum.
  • RAM: GBs (e.g., 4, 8, 16, 32, 64, 256 GB).
  • Secondary Storage: up to petabytes.

Program to Instruction Count

A simple calculator operation (25 + 30) translates to a minimum of 5 CPU instructions (e.g., load R1, load R2, add, store result, display).

Questions asked in class

Q: What is the role of L1 and L2 cache between RAM and CPU registers? A: L1 cache holds the instruction and the data to feed the core. L2 cache acts as a queue, especially important for multi-processing/multi-cache scenarios.

Q: What is the role of the flag? A: After computation completes, a flag in the status register is updated with the operation's status (completed, stuck, etc.) to communicate to other devices.

Q: At the CPU level, is there conversion after assembly? A: Yes. Machine language is binary. Assembly must be decoded to binary for the hardware.

Q: In the add example, is cache memory utilized? A: Only the result will go to cache. Cache is for post-processing, not pre-processing. For huge inputs, cache stores changes after processing (e.g., Word document differences until saved).

Q: Are the positions of opcode and registers fixed in the instruction format? A: In the machine code example shown, the result register comes first after the opcode, then the source registers. The control unit decodes from machine code and rearranges for assembly.

Q: Why have both L1 and L2 caches per core, and then a shared L3? A: L2 cache receives instructions from the control unit/program counter. It splits instructions (opcode to L1I, data to L1D) for faster core access. L3 is shared storage for swapping/temporary data across all cores, not for incoming instructions.

Q: Are the register names (R1, R2, R3) fixed in number? A: No. The number of registers used depends on the number of variables in the program. If you have 10 variables, you need 10 registers.

Q: How does a 10GB CSV file get processed if registers have limited capacity? A: Data is processed row-wise. Each row's validation becomes separate instructions. The OS loads only necessary instructions into memory. Software (e.g., Spark) partitions the data and identifies instructions/data for the RAM and CPU.

Q: Why are CPUs still 32-bit when operating systems are 64-bit? A: The CPU's primary job is processing, and 32-bit instructions are sufficient for that. The OS handles more complex communication with memory, disk, network, etc., requiring a wider bit width (64-bit).

Q: How do registers and caches (L1, L2, L3) interact? Does data go to the register first? A: The core gets instructions from the cache. Registers are temporary storage within the core. The flow is from memory to cache, then to the core/registers.

Key takeaways

  • The CPU contains cores, caches (L1/L2 per core, L3 shared), a control unit, execution units (ALU/FPU), and registers.
  • Registers are the fastest storage, inside the CPU core, used for holding operands and immediate results during an operation.
  • Cache memory (L1, L2, L3) bridges the speed gap between CPU registers and main memory (RAM); it is faster than RAM but smaller.
  • The instruction cycle has four phases: Fetch, Decode, Execute, Write-back.
  • Compiled programs are faster at the CPU level because they only require assembly-to-machine conversion, while interpreted languages need an extra decode step.
  • A 32-bit CPU processes up to 32 bits at a time and cannot fully utilize a 64-bit operating system's capabilities.
  • The control unit fetches and decodes instructions but does not perform arithmetic/logic operations; those are done in the ALU.
  • Data flows from secondary storage to RAM, then to CPU cache, then to registers for processing, and back through the hierarchy for output/storage.
  • The number of registers used by a program is variable and depends on the number of variables/operands.
  • Large datasets are processed incrementally (e.g., row-by-row) because of limited register and cache capacity.
  • Vendor-customized operating systems (e.g., Samsung's Android) receive updates from the vendor, not directly from the base OS developer.
  • The CPU and GPU are separate components; the control unit decides which instructions are sent to which processor.
  • The ISA is the software-visible contract; the microarchitecture implements it, so same-ISA processors can differ in pipeline, caches and execution units.
  • Immediate addressing encodes the operand value in the instruction itself; register-indirect addressing uses a register as an address.
  • Moving data needs a dedicated MOV/LD/ST family; using ADD to move data destroys the carry/overflow flags and is slower.
  • Register pressure is handled by spilling: hot values stay in registers, the rest go to the stack frame.
  • Conditionals branch on flags (Z, N, C, V); a wrong flag write-back or condition code sends execution down the wrong path.
  • In a write-back cache, a modified line is marked dirty and written down to the next level only at a later write-back event.
  • An L1 miss whose line is in L2 can be served from L2 without necessarily touching DRAM.
  • Correct operands with the wrong operation point to wrong ALU select lines — a control-unit decode error, not a data-path error.
  • A binary is not portable across incompatible ISAs; with source available, recompile with a compatible toolchain (subject to platform dependencies), otherwise emulation/translation (Rosetta, QEMU) is needed.

Exam-style questions

  1. Describe the journey of a simple "add R1, R2" instruction from the point it is fetched from memory until the result is written back. Mention all relevant CPU components (PC, IR, control unit, ALU, registers).

    • Outline: Fetch: PC has address, instruction loaded into IR, PC incremented. Decode: Control unit interprets opcode and register fields. Execute: Operands from R1 and R2 sent via internal bus to ALU, which computes the sum. Write-back: Result sent back to a destination register (possibly R1 or a third register) via internal bus.
  2. A student argues that a 64-bit operating system must be running on a 64-bit CPU. Using concepts from the lecture, explain why this is not strictly necessary, but what the limitation would be.

    • Outline: A 32-bit CPU can run a 64-bit OS, but it cannot fully support it. The CPU's processing will be limited to 32 bits at a time, so the wider capabilities of the 64-bit OS (e.g., addressing more memory) will not be fully utilized. The OS handles higher-level communication, but the CPU's execution width is the bottleneck.
  3. Compare the roles of L1, L2, and L3 cache in a multi-core CPU. Explain why having both L1 and L2 per core is beneficial.

    • Outline: L1 is fastest, split into instruction/data cache, feeds the core directly. L2 is larger, acts as a queue/buffer from the control unit, splitting instructions for L1. L3 is shared, used for storage/swapping data between cores or when registers are full. Benefit: L2 handles instruction intake and organization, allowing L1 to be optimized purely for speed in feeding the core.
  4. A program written in Python (interpreted) and the same logic written in C (compiled) both perform an identical calculation. Explain, from the CPU's perspective, why the C program is likely to execute faster.

    • Outline: The compiled C program is already in assembly language. The CPU's control unit only needs to decode this assembly into machine language (binary). The interpreted Python code must first be decoded from its high-level form into assembly, and then from assembly into machine language, requiring an extra decode step and thus more time.

Glossary

  • ALU (Arithmetic Logic Unit) - The CPU component that performs arithmetic and logical operations.
  • Cache - Fast memory inside the CPU, smaller than RAM, used to hold frequently accessed data and instructions.
  • Control Unit - The part of the CPU that manages the fetch-decode-execute-write-back cycle and directs data flow.
  • Core - An independent processing unit within a CPU.
  • Dirty line - A cache line modified in a write-back cache, written down to the next level only at a later write-back event.
  • Immediate addressing - An addressing mode where the operand value is encoded as part of the instruction.
  • Instruction Register (IR) - The register that holds the current instruction being executed.
  • Instruction Set Architecture (ISA) - The software-visible contract defining instruction formats and encoding; the microarchitecture implements it.
  • L1/L2/L3 Cache - Levels of cache memory; L1 is fastest/smallest per core, L2 is larger per core, L3 is shared.
  • Machine Language - The binary code (1s and 0s) that a CPU executes directly.
  • Microarchitecture - The implementation of an ISA (pipeline, caches, execution units), which may differ between same-ISA processors.
  • Program Counter (PC) - The register that holds the memory address of the next instruction to be executed.
  • Register - A very small, extremely fast storage location inside the CPU core for temporary data during processing.
  • Register pressure - Having more simultaneously live values than general-purpose registers, resolved by spilling.
  • Register-indirect addressing - An addressing mode where a register holds the address of the operand.
  • Spilling - Writing values that do not fit in registers to the stack frame, keeping the hottest values in registers.
  • Storage Hierarchy - The ranking of memory types (registers, cache, RAM, secondary storage) by speed and capacity.
  • Write-back cache - A cache that marks modified lines dirty and defers writing them to the next level.

Follow-ups

  • The four-phase instruction cycle was introduced; execute and write-back stages were noted but not covered in detail.
  • The methodology the CPU uses to decide which core executes which application and core allocation was flagged as important but not covered in depth.
  • "Other deciding units" besides the ALU for different operations were mentioned to be covered later.
  • The difference between a core and a thread ("master chef and assistant chefs") was mentioned but not elaborated; it was stated this is managed by the OS.
  • The detailed architecture of the internal data bus and its correlation with CPU generations (i3, i7) was noted for later.
  • Operating system scheduling for GPU processing will be covered in the OS part of the course.
  • A detailed exploration of different Instruction Set Architectures (ISAs) like x86, MIPS, RISC is forthcoming.
  • Assignment 1 will be uploaded, with a scenario to model an "ATM withdrawal" as a sequence of CPU instructions through the fetch-decode-execute-write-back cycle.