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.
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:
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.
Memory in a computer is organised in a hierarchy to balance speed, size, and cost. The hierarchy includes:
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.
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.
Now, let’s connect what we’ve learned to real-world hardware. Consider the following examples:
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.