Python Computer Science
Beginner
80 mins
Teacher/Student led
+90 XP
What you need:
Chromebook/Laptop/PC

Computer Architecture (CPU, Memory, I/O)

In this lesson, you will explore the fascinating world of computer architecture. Learn how the CPU, memory, and input/output devices work together to make a computer function. You'll also dive into the fetch-decode-execute cycle and understand the memory hierarchy with real-world hardware examples.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction to Computer Architecture

    In this lesson we will start learning about what computer architecture is and why it is important. Computer architecture refers to the design and organisation of a computer's core components, including the CPU (Central Processing Unit), memory, and input/output (I/O) systems.

    Take a moment to think about the devices you use every day, such as smartphones, laptops, or gaming consoles. All of these devices rely on computer architecture to function efficiently.

    2 - Understanding the CPU and the Fetch-Decode-Execute Cycle

    The CPU is the brain of the computer. It performs all the calculations and processes instructions. The CPU operates using a cycle called the fetch-decode-execute cycle. Here’s how it works:

    1. Fetch: The CPU retrieves an instruction from memory.
    2. Decode: The CPU interprets the instruction to understand what needs to be done.
    3. Execute: The CPU performs the action required by the instruction.

    To simulate this process, let’s write a Python script that mimics the fetch-decode-execute cycle:

    Add the following code:

    instructions = ["LOAD A", "ADD B", "STORE C"]
    registers = {"A": 5, "B": 3, "C": 0}
    
    def fetch(instruction_pointer):
        return instructions[instruction_pointer]
    
    def decode(instruction):
        operation, operand = instruction.split()
        return operation, operand
    
    def execute(operation, operand):
        if operation == "LOAD":
            registers[operand] = registers["A"]
        elif operation == "ADD":
            registers[operand] += registers["A"]
        elif operation == "STORE":
            registers[operand] = registers["A"]
    
    instruction_pointer = 0
    while instruction_pointer < len(instructions):
        instruction = fetch(instruction_pointer)
        operation, operand = decode(instruction)
        execute(operation, operand)
        instruction_pointer += 1
    
    print(registers)

    Run the code and observe how the CPU processes the instructions step by step.

    3 - Exploring the Memory Hierarchy

    Memory in a computer is organised in a hierarchy to balance speed, size, and cost. The hierarchy includes:

    • Registers: The fastest memory located within the CPU.
    • Cache: A small, fast memory close to the CPU.
    • RAM (Random Access Memory): A larger, slower memory used for active processes.
    • Storage: The slowest memory used for long-term data storage, such as SSDs or HDDs.

    To understand how memory hierarchy impacts performance, let’s write a Python script that simulates accessing data from different levels of memory:

    Add the following code:

    import time
    
    def access_memory(memory_type):
        start_time = time.time()
        if memory_type == "register":
            time.sleep(0.00001)  # Simulate fast access
        elif memory_type == "cache":
            time.sleep(0.0001)  # Simulate slower access
        elif memory_type == "RAM":
            time.sleep(0.001)  # Simulate even slower access
        elif memory_type == "storage":
            time.sleep(0.01)  # Simulate slowest access
        end_time = time.time()
        return end_time - start_time
    
    memory_types = ["register", "cache", "RAM", "storage"]
    for memory in memory_types:
        print(f"Accessing {memory} took {access_memory(memory):.5f} seconds")

    Run the code and notice how the access times differ for each type of memory.

    4 - Understanding Input/Output (I/O) Systems

    Input/Output systems allow a computer to interact with the outside world. Examples include keyboards, mice, monitors, and printers. The CPU communicates with I/O devices using protocols and interfaces.

    Let’s write a Python script that simulates a simple I/O operation where the user inputs data, and the system processes and outputs it:

    Add the following code:

    def input_device():
        return input("Enter a number: ")
    
    def process_data(data):
        return int(data) * 2
    
    def output_device(result):
        print(f"The result is: {result}")
    
    # Simulate I/O operation
    data = input_device()
    result = process_data(data)
    output_device(result)

    Run the code, input a number, and observe how the system processes and outputs the data.

    5 - Real-World Hardware Examples

    Now, let’s connect what we’ve learned to real-world hardware. Consider the following examples:

    • CPU: Intel Core i7, AMD Ryzen processors.
    • Memory: DDR4 RAM, NVMe SSDs.
    • I/O Devices: USB keyboards, HDMI monitors.

    Think about how these components work together in your own computer or a device you use regularly. Write a short paragraph describing how the CPU, memory, and I/O devices in your device might interact to perform a task like opening a web browser.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more