COMP1310 · Lecture 1 companion
Computer Basics
The slides give you the vocabulary. This page puts it together, so you can see how the parts of a computer depend on each other. Read it once from top to bottom; it should take about fifteen minutes.
1What a computer does
A computer (حاسوب) is a machine that takes in data, stores it, processes it into something useful, and gives the result out. Everything else in this lecture is about one of those four jobs: the CPU processes, memory stores, input and output devices bring data in and send results out, and the operating system organises all of it.
The difference between data and information is the difference between what goes in and what comes out.
Thirty marks from a quiz. All correct, and on their own not much use.
The class average is 71. The top mark is 97. Four students scored below 60 and need help before the midterm.
The second box was produced from the first by processing: adding, dividing, comparing each mark with 60. Doing that by hand for thirty marks takes you a few minutes.
2Why we use computers
Three properties make computers worth having.
- Speed. A modern processor carries out billions of simple operations every second. The thirty-mark average above takes it far less than a millionth of a second.
- Reliability. Given the same program and the same data, a computer gives the same answer every time, and it does not get tired at 2 a.m. When a result is wrong, the cause is almost always a mistake in the program or in the data, not the machine.
- Storage capability. It can keep huge amounts of data and find any part of it quickly. Your phone holds thousands of photos and can show you any one of them in a moment.
3Hardware, software, people
A computer system has three components.
- Hardware (المكونات المادية) is the physical part, anything you can touch: screen, keyboard, processor, memory chips, the storage drive.
- Software (البرمجيات) is the programs: sets of instructions telling the hardware what to do. A web browser, Microsoft Word, WhatsApp.
- People. A programmer writes software. An end-user uses it. Later in this course you will be both: you will write small C programs and then run them yourself.
A laptop with no programs installed is a box that lights up; WhatsApp with no phone to run on is just a file.
4Kinds of computers
All computers have a processor, memory, and some way to get data in and out. They differ in size, cost, power, and what they are for.
| Class | What it is | Examples you know |
|---|---|---|
| Personal computer | A desktop machine for one person at a time | The PCs in the university labs |
| Portable | A personal computer you can carry, running on a battery | Laptops, tablets |
| Handheld | Small enough to hold in one hand, always with you | Smartphones, smartwatches |
| Server | Runs all day and night, answering requests from many users over a network | The servers behind Ritaj, a bank's online banking, Google |
| Supercomputer | Thousands of processors working on one huge calculation; very expensive | Weather forecasting, climate models, designing aircraft and medicines |
| Embedded system | A computer built inside another device to control it; you rarely notice it is there | A washing machine's controller, a car's anti-lock brakes (ABS), a microwave, a traffic light |
Embedded systems are by far the most numerous. A modern car alone contains dozens of small computers. (The slides also mention palmtops; that was the name for pocket computers in the 1990s and early 2000s. The smartphone replaced them.)
5The CPU: where the processing happens
The central processing unit, or processor (المعالج), is the part that actually runs programs. It is a single chip made of electronic circuits, and it has three main parts.
- Control unit (CU). The coordinator. It fetches the next instruction of the program from memory, works out what that instruction asks for, and directs the other parts to carry it out.
- Arithmetic logic unit (ALU). The part that calculates. It does arithmetic operations (add, subtract, multiply, divide) and logical operations: comparisons (equal, less than, greater than) and combining true/false results with AND, OR, NOT. Comparing each quiz mark with 60 is a logical operation.
- Registers. A handful of very small, very fast storage locations inside the CPU. They hold the numbers being calculated with and two special values. The instruction register holds the instruction being carried out right now. The program counter holds the address of the next one.
“Address” needs a word of explanation. Main memory (RAM, section 7) is a long row of numbered boxes. Each box holds a number or an instruction, and its number is its address, like a house number on a street. In the demo below, [200] means “the box at address 200”.
A program is a list of simple instructions sitting in memory. The CPU repeats the same three steps for each one, billions of times a second:
- Fetch: copy the next instruction from memory into the CPU.
- Decode: the control unit works out what the instruction means.
- Execute: carry it out, using the ALU if there is calculating to do.
Step through a tiny program below that adds two numbers. Watch which part is busy at each step. The instructions are simplified, but real processors work this way.
Inside the CPU
| Program counter | |
| Instruction register | |
| R1 | |
| R2 | |
| R3 | |
| ALU |
Main memory (RAM)
| 100 | LOAD R1, [200] |
| 101 | LOAD R2, [201] |
| 102 | ADD R3, R1, R2 |
| 103 | STORE R3, [202] |
| 200 | 45 |
| 201 | 26 |
| 202 |
Notice the pattern: the ALU never works on numbers while they sit in RAM. They are first copied into registers, the ALU works on them there, and the result is written back. Why the extra copying? Compared with the CPU, RAM is slow: fetching one number from it can take as long as a hundred additions or more. Registers sit right next to the ALU, so the CPU keeps the values it is working on right now in them.
6Bits and bytes
Inside the hardware, everything is stored as a pattern of two states: voltage or no voltage, a switch on or off. We write the two states as 1 and 0. One such 0-or-1 digit is a bit (binary digit). Numbers, text, photos, music and programs are all, in the end, long rows of bits. Lecture 2 is about exactly how numbers are written this way.
A single bit can only say yes or no, so bits are grouped. Eight bits make a byte. Each of the 8 bits has 2 possible values, so a byte has 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 28 = 256 different patterns, from 00000000 to 11111111. That is enough to give every English letter, digit and punctuation mark its own pattern, which is why we say one byte holds one character. An Arabic letter usually takes two bytes, and an emoji four, because there are far more characters to give patterns to; “about one byte per character” is still a fair rule for English text.
If your copy of the slides says a byte represents “28 = 256” values, read it as 28: the superscript was lost.
Memory and storage sizes are counted in bytes, with prefixes for large amounts:
| Unit | About | Roughly holds |
|---|---|---|
| 1 byte (B) | 8 bits | one character, such as A |
| kilobyte (KB) | a thousand bytes (≈ 1,000 characters) | a short paragraph or two |
| megabyte (MB) | a million bytes (≈ 1 million characters) | a whole book of plain text; one phone photo is a few MB |
| gigabyte (GB) | a billion bytes (≈ 1 billion characters) | a small library of books; a few hundred phone photos |
| terabyte (TB) | a trillion bytes (≈ 1 trillion characters) | a very large library; a typical external backup drive |
Why does a 1 TB drive show only about 931 GB in Windows? Two ways of counting. Storage makers use powers of ten: 1 KB = 1000 bytes, 1 GB = 1,000,000,000 bytes. Windows counts in powers of two: 1024 bytes, then 1024 × 1024, and so on, so its “GB” is 1024 × 1024 × 1024 = 1,073,741,824 bytes (strictly called a GiB). 1,000,000,000,000 ÷ 1,073,741,824 ≈ 931. Same bytes, different ruler. Phones usually count in powers of ten; there, the space you seem to lose is taken by the operating system and the apps that come preinstalled.
7Main memory and secondary memory
Memory (الذاكرة) is where programs and data are kept. A computer uses several kinds, because no single kind is fast, large, cheap and permanent at once. You trade one property against another.
| Main (primary) memory | Secondary storage | |
|---|---|---|
| Speed | Fast; the CPU reads it directly | Much slower |
| Cost per GB | Expensive | Cheap |
| Typical size | 8 to 16 GB in a laptop | 256 GB to a few TB |
| Power off | RAM forgets everything (volatile); ROM keeps its contents | Keeps everything (non-volatile) |
| Examples | RAM, ROM | SSD, hard disk, USB flash drive, memory card |
Think of RAM as your desk and secondary storage as the bookshelf. You can only work on what is on the desk, and the desk is cleared every night when the power goes off. Anything you want to keep must go back on the shelf. That is what Save does: it copies your document from RAM to the SSD or disk. If the power cuts before you save, the work in RAM is gone.
RAM and ROM
Both are main memory, but they do different jobs.
- RAM (Random Access Memory) is read and write, and volatile. It holds the programs you are running right now and the data they are using. More RAM means more programs and files can be open at once without slowing down.
- ROM (Read-Only Memory) is non-volatile and, in normal use, only read. It keeps its contents with the power off, so it holds the small start-up program (the firmware, called BIOS or UEFI on a PC) that the computer needs before anything else can happen. In modern machines this lives on a flash chip that can be updated occasionally, but its role is the same.
Secondary storage today
Hard disks store bits as magnetic patterns on spinning platters; they are cheap for very large amounts. Most laptops and every phone now use SSDs and flash memory instead: no moving parts, much faster, and tougher if dropped. CDs and DVDs, which store bits as reflective spots read by a laser, are mostly history.
Switching on: the boot sequence
When you press the power button, RAM is empty, and the operating system is sitting on the SSD, where the CPU cannot run it directly.
- The CPU starts by running the firmware in ROM. It is the only program available, because ROM is the only main memory that kept anything while the power was off. The firmware checks the hardware.
- The firmware finds the operating system on the SSD or disk and starts copying it into RAM.
- Once the operating system is in RAM, the CPU runs it, and the OS takes over the machine. Now you see the login screen.
This is also why a computer with an SSD starts much faster than one with a hard disk: step 2 is mostly waiting for the storage to deliver the OS.
Where does it live?
Try placing each item. Tap the place where it lives at that moment.
8Input and output
Input (الإدخال) devices bring data from the outside world into the computer: keyboard, mouse, microphone, camera, scanner, a fingerprint sensor. Output (الإخراج) devices send results back out: screen, speakers, printer, headphones.
Some devices do both. A phone's touchscreen shows you pictures (output) and senses your finger (input) on the same piece of glass. A network card sends and receives.
9Software and the operating system
A program is a collection of instructions the computer carries out to finish some task. Most software you use is application software: Word, Excel, a browser, Photoshop, a game. Each application does one kind of job for you.
Underneath all of them is the operating system (نظام التشغيل). It is system software: software that runs the computer itself rather than doing a job for you. It stands between the hardware and the applications, and it shares out the computer's limited resources. Concretely, the OS:
- shares the CPU between programs, switching between them so fast that music, a browser and WhatsApp all seem to run at once;
- manages memory: decides which program gets which part of RAM, loads programs from storage when you open them, and frees the space when you close them;
- controls devices: the keyboard, screen, printer, Wi-Fi, so that each application does not have to know how every printer works;
- organises files into folders on the storage drive and remembers where each one is;
- gives you an interface: the desktop, windows, icons and touch gestures you use to work with the computer (a GUI, graphical user interface).
Desktops and laptops mostly run Windows, macOS or Linux. Phones run Android or iOS. Most of the world's servers run Linux. Your first lab is about using an operating system.
Labs. The lab plan on the Lecture 1 slides is from an older semester. For this semester's lab topics and quiz dates, use the course outline (PDF).
10Check yourself
Answer in your head first, then open the answer.