impera Open Impera

IB Diploma Computer Science · Concepts of computer science

Computer fundamentals

1The central processing unit

The central processing unit (CPU) carries out the instructions of programs. Its main parts:

  • Arithmetic logic unit (ALU): does calculations (add, subtract) and logical comparisons (AND, OR, greater than).
  • Control unit (CU): decodes instructions and sends control signals that coordinate the other parts.
  • Registers: tiny, very fast storage locations inside the CPU:
  • the program counter (PC) holds the address of the next instruction,
  • the memory address register (MAR) holds the address being read from or written to,
  • the memory data register (MDR) holds the data just read or about to be written,
  • the current instruction register (CIR) holds the instruction being decoded and executed,
  • the accumulator (AC) holds the results of ALU calculations.
  • Buses connect the CPU to memory: the address bus carries addresses (one way, CPU to memory), the data bus carries data (both ways) and the control bus carries signals such as "read" or "write".

Inside the CPU: registers, control unit and ALU, linked to memory by buses

Check yourself

Which register holds the address of the next instruction to be fetched?

  • The accumulator

  • The memory data register

  • The program counter

  • The current instruction register

Show the answer

The program counter

The PC is then incremented to point to the following instruction.

2The fetch–decode–execute cycle

The CPU repeats the same cycle billions of times a second:

  1. Fetch: the address in the PC is copied to the MAR. The instruction at that address travels along the data bus into the MDR, then into the CIR. The PC is incremented.
  2. Decode: the control unit works out what the instruction means (its operation and any data or addresses it needs).
  3. Execute: the instruction is carried out, for example the ALU adds two numbers and stores the result in the accumulator, or data is loaded from or saved to memory.

Things that make a CPU faster:

  • Clock speed: cycles per second, measured in gigahertz (GHz). 3 GHz means 3 billion cycles a second.
  • Multiple cores: each core can run its own instructions, so tasks that can be split run in parallel.
  • Cache: a small amount of very fast memory on or near the CPU that holds recently and frequently used data, so the CPU waits less for main memory. Levels L1 (smallest, fastest), L2 and L3.
  • Pipelining: while one instruction executes, the next is decoded and the one after is fetched, so stages overlap.

A GPU (graphics processing unit) has thousands of simpler cores designed to do the same operation on lots of data at once. This suits graphics, video and machine learning, while the CPU is better at varied, sequential tasks.

Check yourself

What happens to the program counter during the fetch stage?

  • It is incremented to point to the next instruction

  • It stores the result of the last calculation

  • It is reset to zero ready for the next program

  • It is copied into the accumulator for decoding

Show the answer

It is incremented to point to the next instruction

So the next cycle fetches the next instruction.

3Memory and storage

  • Primary memory is directly accessed by the CPU:
  • RAM (random access memory) holds the programs and data currently in use. It is volatile: its contents are lost when power is off.
  • ROM (read-only memory) is non-volatile and holds firmware such as the instructions to start the computer (the boot program).
  • Virtual memory uses part of secondary storage as extra RAM when RAM is full. It is much slower and can cause "thrashing" if used heavily.
  • Secondary storage keeps data permanently:
  • Hard disk drives (HDD): magnetic spinning platters; cheap per gigabyte, large capacity, but slower and damaged by knocks.
  • Solid-state drives (SSD): flash memory with no moving parts; much faster, tougher and quieter, but more expensive per gigabyte.
  • Optical discs (CD, DVD, Blu-ray) and cloud storage (data kept on remote servers over the internet, accessible anywhere but needing a connection and trust in the provider).

The memory hierarchy runs from registers (fastest, smallest, most expensive per byte) through cache and RAM to secondary storage (slowest, largest, cheapest).

Compression reduces file size. Lossless compression (such as ZIP, PNG) lets the original be rebuilt exactly; lossy compression (JPEG, MP3) throws away detail people are unlikely to notice, giving much smaller files.

Check yourself

Why is RAM described as volatile?

  • It is slower than a hard disk, so data fades over time

  • It can only be read from, never written to by programs

  • Its contents are lost when the power is switched off

  • It stores the boot program permanently for start-up

Show the answer

Its contents are lost when the power is switched off

ROM is non-volatile.

4Representing data in binary

Computers store everything as binary digits (bits), 0 or 1. Eight bits make a byte.

  • A binary number has place values 128, 64, 32, 16, 8, 4, 2, 1 (for 8 bits). So 1011 0110 is $128 + 32 + 16 + 4 + 2 = 182$.
  • Hexadecimal (base 16) uses 0–9 and A–F (A = 10 ... F = 15). Each hex digit is exactly 4 bits, so hex is a compact way to write binary: 1011 0110 is B6.
  • Negative integers are usually stored in two's complement: the leftmost bit is worth −128 in 8 bits. To make −5: write 5 (0000 0101), flip every bit (1111 1010), add 1 (1111 1011).
  • Characters are stored as numbers using a character set: ASCII uses 7 bits (128 characters, enough for English); Unicode uses up to 32 bits and covers over 150 000 characters from all writing systems, plus emoji.
  • Images are grids of pixels. Each pixel's colour is a binary number; colour depth is the number of bits per pixel (24 bits gives about 16.7 million colours). File size = width × height × colour depth.
  • Sound is stored by sampling the wave many times a second. Higher sample rate and bit depth give better quality but larger files.
Worked example

Size of an image

  1. A 1920 × 1080 image has 2 073 600 pixels.
  2. At 24 bits (3 bytes) per pixel: 2 073 600 × 3 = 6 220 800 bytes.
  3. That is about 6.2 MB before compression.
Worked example

Size of a sound file

  1. One minute of CD-quality stereo: 44 100 samples per second, 16 bits (2 bytes) per sample, 2 channels.
  2. 44 100 × 2 × 2 × 60 = 10 584 000 bytes, about 10.6 MB.
Check yourself

What is the binary number 0010 1101 in denary?

  • 90

  • 45

  • 23

  • 41

Show the answer

45

32 + 8 + 4 + 1 = 45.

5Logic gates

Logic gates are the building blocks of circuits. Each takes binary inputs and gives one output:

  • AND: output 1 only if both inputs are 1.
  • OR: output 1 if at least one input is 1.
  • NOT: one input; the output is the opposite.
  • NAND (NOT AND): output 0 only if both inputs are 1.
  • NOR (NOT OR): output 1 only if both inputs are 0.
  • XOR (exclusive OR): output 1 if the inputs are different.

A truth table lists the output for every combination of inputs: $n$ inputs give $2^n$ rows.

Truth table for the six two-input logic gates

Worked example

Evaluating a Boolean expression

  1. Find Q = (A AND B) OR (NOT C) when A = 1, B = 0, C = 0.
  2. A AND B = 0.
  3. NOT C = 1.
  4. Q = 0 OR 1 = 1.

NAND and NOR are called universal gates: any circuit can be built from NAND gates alone.

Check yourself

Which gate outputs 1 only when its two inputs are different?

  • NAND

  • NOR

  • XOR

  • AND

Show the answer

XOR

Exclusive OR: one or the other, but not both.

6Operating systems and translation

The operating system (OS) manages the computer's resources and gives users and programs a way to use it:

  • Memory management: allocating RAM to programs and keeping them separate.
  • Process management and scheduling: sharing CPU time between programs so they appear to run at once (for example by giving each a short time slice in turn: round robin).
  • Peripheral management using drivers, small programs that let the OS talk to each device.
  • File management, security (user accounts, permissions) and the user interface.
  • Interrupts are signals that make the CPU pause its current task to deal with something urgent (a key press, a printer out of paper). Polling means the CPU checks devices repeatedly instead, which wastes time if nothing has happened.

Translation: programs are written in high-level languages (Python, Java) that people can read. The CPU only runs machine code.

  • A compiler translates the whole program into machine code before it runs. The result runs fast and can be distributed without the source code, but errors are reported only after compiling the whole program.
  • An interpreter translates and runs the program line by line. It is easier for testing and debugging, but runs more slowly and needs the interpreter each time.
  • Assembly language is a low-level language with one short mnemonic (such as LDA, ADD) for each machine instruction; an assembler translates it.
  • Java is compiled to bytecode, which a virtual machine then interprets or compiles on the fly, so the same program runs on many platforms.
Check yourself

What is an advantage of a compiler over an interpreter?

  • It does not produce machine code, so programs are easier to share

  • The compiled program runs faster and needs no translator each time

  • It shows errors line by line while the program is actually running

  • It is always easier for beginners because it runs code immediately

Show the answer

The compiled program runs faster and needs no translator each time

Interpreters are handier for debugging.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera