Chapter 1.3: Computers
Introduction
Learning Outcomes
Explain the consequences of a fixed number of bits (negative numbers, overflow, floating point).
Distinguish ASCII, Unicode and strings.
Explain the stored-program concept and the fetch–decode–execute cycle.
Interpret how the CPU, memory, registers and buses work together.
Introduction
The Basic Parts of a Computer
Executes instructions.
Stores data and instructions.
Exchanges data with the outside world.
Data Representation
From Information to Bits
Numbers, letters, images and program instructions are all stored in memory as bit strings. The data type and encoding decide what a bit string means.
Data Representation · Step by step
Number Bases
| System | Base | Digits | Example |
|---|---|---|---|
| Binary | 2 | 0–1 | 1101₂ |
| Octal | 8 | 0–7 | 15₈ |
| Decimal | 10 | 0–9 | 13₁₀ |
| Hexadecimal | 16 | 0–9, A–F | D₁₆ |
45₁₀ = 101101₂. Octal and hexadecimal are shorthand for long binary numbers.
Since 8 = 2³, group the bits in threes from the right: 101 101 → 55₈.
Since 16 = 2⁴, group them in fours: 0010 1101 → 2 and 13 (D) → 2D₁₆.
Data Representation
Negative Integers
Modern computers usually store negative integers in two's complement.
There is no separate positive and negative zero.
The same adder circuit works for positive and negative numbers.
From −2ⁿ⁻¹ to 2ⁿ⁻¹−1.
8 bits: −128 … 127Data Representation · Step by step
Two's Complement: How Do We Write −5?
In 8 bits, +5 = 00000101. Two steps give −5.
1. Invert every bit (0 ↔ 1): 11111010. This is the ones' complement.
2. Add 1: 11111010 + 1 = 11111011 = −5. The leading 1 marks the number as negative.
Check: 00000101 + 11111011 = 100000000. The 9th bit doesn't fit in 8 bits and is dropped → 0. The adder needs no special case for negatives.
Data Representation · Step by step
Subtraction Is Really Addition: 5 − 9
The processor needs no separate subtractor: a − b = a + (−b). First we find the two's complement of b.
Invert all the bits (0 ↔ 1): 11110110.
Add 1: 11110111 = −9. (Read as unsigned, 247 = 256 − 9.)
Plain binary addition: 5 + 247 = 252 = 11111100. It doesn't reach 256, so there is no carry into a 9th bit.
Signed reading: the leading bit is 1, so the number is negative. For its magnitude, invert and add 1 again: 00000100 = 4 → s = −4 ✓.
a ← 00000101 ; +5 b ← 00001001 ; +9 inv ← NOT b ; flip bits neg ← inv + 1 ; −9 s ← a + neg ; 5 + (−9) print(s) ; read signed
Data Representation · Step by step
Overflow: 200 + 100 in 8 Bits
An 8-bit unsigned number is at most 255. Let's add 200 + 100.
The true sum 300 = 100101100₂ needs 9 bits.
The register holds only 8 bits; the 9th bit (256) is lost → the result is 44. This is overflow: 300 − 256 = 44.
In signed 8 bits, 127 + 1 also overflows to −128. That is why choosing types matters in programs.
Data Representation
Signed Overflow vs. Carry
| Operation (8 bits) | Binary sum | Carry | Signed overflow | Result |
|---|---|---|---|---|
| 5 + (−9) | 00000101 + 11110111 = 11111100 | 0 | no | −4 ✓ |
| (−1) + (−1) | 11111111 + 11111111 = 1 11111110 | 1 | no | −2 ✓ |
| 100 + 50 | 01100100 + 00110010 = 10010110 | 0 | yes | −106 ✗ |
| (−100) + (−50) | 10011100 + 11001110 = 1 01101010 | 1 | yes | +106 ✗ |
Means the unsigned result doesn't fit in 8 bits. In signed addition the carry is discarded and the result can still be correct: (−1) + (−1) = −2.
Happens when two numbers of the same sign add up to a result of the opposite sign. Adding numbers of different signs never overflows.
In Java, (byte) (100 + 50) is −106: integer overflow raises no error, it silently gives a wrong result.
Data Representation
Floating-Point Numbers
Stores very large and very small values, much like scientific notation.
Not every decimal value can be represented exactly with finitely many bits.
Calculations can pick up small errors.
Compare floating-point results within a tolerance.
0.1 + 0.2 → 0.30000000000000004 // computed with double
Data Representation · Step by step
Why Can't 0.1 Be Stored Exactly?
How do we convert a decimal fraction to binary? Multiply by 2, take the integer part (0 or 1) as a bit, and continue with the remaining fraction.
0.1 × 2 = 0.2 → integer part 0.
0.2 × 2 = 0.4 → integer part 0.
0.4 × 2 = 0.8 → integer part 0.
0.8 × 2 = 1.6 → integer part 1; subtract 1 and continue with the fraction.
0.6 × 2 = 1.2 → integer part 1; subtract 1 and continue. The fraction is 0.2 again: the expansion has entered a cycle and never ends.
Data Representation
Floating-Point Numbers (IEEE 754)
- A float is 32 bits (1 + 8 + 23); a double is 64 bits (1 + 11 + 52). For normal numbers the leading 1 is not stored.
- 0.1 = 1.1001 1001 1001…₂ × 2⁻⁴: the fraction repeats forever and is rounded when it doesn't fit in 23 bits.
- The stored value is exactly 0.100000001490116119384765625: very close to 0.1, but not equal.
Data Representation · Step by step
Encoding −6.25 as a float
A float is 32 bits: 1 sign + 8 exponent + 23 fraction. The number is negative, so the sign bit is 1; the remaining steps work on 6.25.
The integer part 6 = 110₂; the fraction 0.25 becomes 0.01₂ by “multiply by 2, take the integer part”, and reaches 0. 6.25 = 110.01₂: unlike 0.1, finite and exact.
As in scientific notation, the point moves until a single 1 is left of it: 2 places left → 1.1001 × 2².
The exponent field stores the true exponent plus 127 (the bias): 2 + 127 = 129. This lets negative exponents be stored unsigned.
For normal numbers the leading 1 is always there, so it is not stored; only 1001 after the point is written and the remaining 19 bits are 0.
The 32 bits in groups of four: 1100 0000 1100 1000 0000 … → C0C80000. In Java, Float.floatToIntBits(-6.25f) returns this value.
x ← −6.25 sign ← 1 ; negative bin ← 110.01 ; 6 = 110, 0.25 = 0.01 norm ← 1.1001 × 2² ; point 2 left exp ← 2 + 127 = 129 ; 10000001 frac ← 1001 0000… ; leading 1 dropped print(hex(bits))
Text Representation
ASCII
ASCII maps basic Latin letters, digits, punctuation and control characters to numeric codes.
ASCII is a 7-bit code: it defines 128 characters (0–127); stored in a byte, the top bit is 0.
65
0100000197
0110000148
00110000Control character: 10
00001010Text Representation · Step by step
Bitwise Operations: Changing a Letter's Case
“G” = 71, “g” = 103: the difference is 32 = 2⁵. Upper and lower case differ only in bit 5 (counting from 0 on the right).
OR: a bit that is 1 in the mask becomes 1; the other bits are unchanged. Uppercase became lowercase.
AND with the inverted mask: the masked bit is cleared to 0, the rest are kept. Lowercase became uppercase again.
XOR: the masked bit is flipped. XOR with the same mask twice gives back the original value.
Left shift: every bit moves up one place and a 0 enters on the right → the value is multiplied by 2. Right shift (>> 1) halves positive numbers (rounding down): 71 → 35. Negative odd numbers differ: −3 >> 1 = −2, but −3 / 2 = −1.
c ← 'G' ; 71 = 01000111 m ← 00100000 ; mask: bit 5 (32) k ← c OR m ; set the bit b ← k AND (NOT m) ; clear the bit t ← b XOR m ; flip the bit s ← c << 1 ; shift left
Text Representation
Unicode
Unicode assigns a unique code point to every character in every writing system.
The character's universal number.
A → U+0041Encodes Unicode characters as variable-length byte sequences.
Turkish letters, other alphabets, math symbols and emoji.
The same code point can be stored in different Unicode transformation formats.
Text Representation · Step by step
UTF-8 Encoding: “ğ”
Unicode assigns the letter “ğ” the code point U+011F (decimal 287). How does UTF-8 write this number as bytes?
287 in binary is 00100011111. It doesn't fit in 7 bits (not ASCII) but fits in 11 → 2 bytes are needed.
The 2-byte UTF-8 template is 110xxxxx 10xxxxxx: the 11 bits are split as 5 + 6.
The bits drop into the template: 11000100 10011111 = C4 9F. The leading 110 and 10 tell each byte's role.
ASCII characters take 1 byte; Turkish letters 2, € 3, and emoji 4 bytes. That is why UTF-8 is variable-length.
Text Representation
Strings
A string is a data type made of a sequence of characters.
The number of characters or code units.
Joining two strings end to end.
Finding a character or substring.
Checking contents and ordering.
Data Representation
Structured Information
Bits gain meaning when organized into larger structures.
- A student record might consist of id, name and grade fields.
- A file format defines how the fields are ordered and interpreted.
Data Representation
Common Mistakes in Data Representation
| Common mistake | Correct |
|---|---|
| Reading without the base: “1101 is one thousand one hundred one” | 1101₂ = 13. Give the base as a subscript or with a 0b/0x prefix. |
| Taking 10₁₆ for ten | 10₁₆ = 16; in hexadecimal, ten is written A. |
| In two's complement, only inverting the bits | 11111010 is the ones' complement (not −5); adding 1 gives 11111011 = −5. |
| Comparing floating-point results with == | 0.1 + 0.2 ≠ 0.3; check whether |a − b| is below a small tolerance. |
| Confusing a digit character with a number | The character '7' has code 55; its numeric value is '7' − '0' = 7. |
| Taking character count for byte count | In UTF-8, “çağ” is 3 characters but 5 bytes (ç 2 + a 1 + ğ 2). |
| Confusing carry with signed overflow | Carry is a problem for the unsigned reading, overflow for the signed one; either can happen without the other. |
Computer Architecture
Logic Gates
| A | B | AND | OR | XOR | NOT A |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 | 0 |
| 1 | 1 | 1 | 1 | 0 | 0 |
- AND gives 1 if both inputs are 1, OR if at least one is 1; NOT inverts its input.
- XOR (exclusive or): 1 if the inputs differ. In an adder it produces the sum digit.
Gates are built from a few transistors; every operation in a processor is a combination of these basic gates.
Computer Architecture · Step by step
The Half Adder: From Gates to Addition
A half adder adds two one-bit numbers (A, B). SUM = A XOR B gives the result digit and CARRY = A AND B the carry. Two gates are enough.
0 + 0: XOR(0, 0) = 0, AND(0, 0) = 0 → 00₂ = 0.
0 + 1: XOR(0, 1) = 1, AND(0, 1) = 0 → 01₂ = 1. No carry.
1 + 1: XOR(1, 1) = 0 (digit 0), AND(1, 1) = 1 (carry 1) → 10₂ = 2. As with 9 + 1 = 10 in decimal, the carry moves to the next place.
A half adder does not take an incoming carry. Multi-bit addition needs a three-input full adder (two half adders and an OR gate).
Computer Architecture · Step by step
A Chain of Full Adders: 0110 + 0111
Four full adders (FA) are chained: each one's carry out is the carry in of the one to its left. We compute 6 + 7.
i = 0: a0 = 0, b0 = 1, c = 0 → t = 1, s0 = 1; the inputs are not both 1, so the carry is 0.
i = 1: 1 + 1 + 0 → t = 0, s1 = 0; a1 AND b1 = 1, so the carry is 1 (like 5 + 5 in decimal).
i = 2: 1 + 1 + 1 = 3 = 11₂ → s2 = 1, carry 1 again.
i = 3: 0 + 0 + 1 → s3 = 1, carry 0. The final carry is 0, so the result fits in 4 bits.
The result is 1101₂ = 13 = 6 + 7 ✓. The carry ripples from right to left (ripple-carry adder): each FA waits for the previous one, so the delay grows with the number of bits.
a ← 0110 ; 6 b ← 0111 ; 7 c ← 0 ; initial carry for i = 0, 1, 2, 3: ; right to left t ← a[i] XOR b[i] s[i] ← t XOR c c ← (a[i] AND b[i]) OR (t AND c) print(c, s)
Computer Architecture
The Stored-Program Concept
Program instructions and the data they work on are stored as numeric codes in the same memory. The processor reads and executes the instructions in order.
The same hardware does different jobs with different programs.
Behavior can change through software, without changing the hardware.
One system can handle computing, communication and media.
Computer Architecture
The Fetch–Decode–Execute Cycle
- The program counter (PC) holds the address of the next instruction; the instruction register (IR) holds the fetched instruction.
- The control unit interprets the operation code (opcode).
- The ALU or another unit carries out the operation.
- Instructions and data usually come from the processor's cache rather than RAM; registers and cache are much faster than RAM.
Computer Architecture
The Main Parts of a CPU
Manages the order of instructions and the movement of data.
Performs arithmetic and logical operations.
Small, fast storage used during an operation.
Keeps frequently used data and instructions close to the processor.
Computer Architecture · Step by step
Buses: Reading from Memory
Three buses link the CPU to memory and I/O. Example: the CPU reads address 1004.
Address bus: the CPU sends the address → 1004. One-way.
Control bus: the CPU sends READ (or WRITE), plus timing signals.
Data bus: memory returns the contents (12). It is bidirectional.
| Bus | Role |
|---|---|
| Data bus | Carries data between CPU, memory and devices. |
| Address bus | Selects the memory or device location. |
| Control bus | Carries read, write and timing signals. |
Computer Architecture
Memory Addresses
| Address | Contents |
|---|---|
| 1000 | LOAD 1004 |
| 1001 | ADD 1005 |
| 1002 | STORE 1006 |
| 1003 | HALT |
| 1004 | 12 |
| 1005 | 8 |
| 1006 | 0 |
- Every memory cell has an address.
- Instructions reach data through addresses.
- The same bit string can be read as an instruction or as data.
- Without HALT, the CPU would treat the 12 at 1004 as an instruction and try to run it.
Computer Architecture · Endianness
Multi-Byte Values and Byte Order
Memory is byte-addressable: each address holds 1 byte. A 32-bit int spans 4 consecutive addresses; the order in which its bytes are written is the byte order (endianness).
The most significant byte at the lowest address. Network protocols use this order (network byte order).
The least significant byte at the lowest address. x86-64 and most ARM systems work this way.
Raw binary data read on a machine with the opposite order turns 0x12345678 into 0x78563412.
Single bytes (e.g. ASCII characters) have no order problem; UTF-8 is read byte by byte, so byte order doesn't affect it either.
Computer Architecture · Fetch–decode–execute step by step
Running a Program Step by Step
Start: PC = 1000 (address of the first instruction), ACC = 0. The CPU starts the cycle.
Fetch: PC's 1000 goes out on the address bus; the instruction LOAD 1004 arrives in IR over the data bus. PC increments → 1001.
Decode: the control unit splits IR: opcode LOAD, address 1004 → mem[1004] will be read into ACC.
Execute: address 1004 is read and 12 is loaded into ACC.
Fetch: PC's 1001 goes out on the address bus; the instruction ADD 1005 arrives in IR over the data bus. PC increments → 1002.
Decode: the control unit splits IR: opcode ADD, address 1005 → mem[1005] will be added to ACC.
Execute: the 8 at address 1005 is read, the ALU computes 12 + 8 = 20, and the result goes into ACC.
Fetch: PC's 1002 goes out on the address bus; the instruction STORE 1006 arrives in IR over the data bus. PC increments → 1003.
Decode: the control unit splits IR: opcode STORE, address 1006 → ACC will be written to mem[1006].
Execute: the 20 in ACC is written to address 1006 over the data bus.
Fetch: PC's 1003 goes out on the address bus; the instruction HALT arrives in IR over the data bus. PC increments → 1004.
Decode: opcode HALT → the CPU stops. Result: mem[1006] = 20. 4 instructions, 4 fetch–decode–execute cycles.
1000: LOAD 1004 ; ACC ← mem[1004] 1001: ADD 1005 ; ACC ← ACC + mem[1005] 1002: STORE 1006 ; mem[1006] ← ACC 1003: HALT ; stop 1004: 12 ; data 1005: 8 ; data 1006: 0 ; result
Computer Architecture · Fetch–decode–execute step by step
Branching: How Does PC Change in a Loop?
1000: LOAD 1010 ; ACC ← count 1001: JUMPZ 1008 ; jump if ACC = 0 1002: ADD 1011 ; ACC ← ACC + sum 1003: STORE 1011 ; sum ← ACC 1004: LOAD 1010 1005: SUB 1012 ; ACC ← ACC − 1 1006: STORE 1010 ; count ← ACC 1007: JUMP 1000 ; PC ← 1000 1008: HALT 1010: 2 1011: 0 1012: 1
count runs down from 2; when the loop ends, sum = 2 + 1.
LOAD → ACC = 2. JUMPZ: ACC ≠ 0, no jump; PC stays at the next address, 1002.
ADD: 2 + 0 = 2; STORE → sum = 2.
LOAD, SUB, STORE: count 2 − 1 = 1.
JUMP 1000: PC, which the fetch set to 1008, is overwritten with 1000. The loop goes back to the top.
The second pass runs the same 8 instructions: sum = 1 + 2 = 3, count = 0, JUMP again.
LOAD → ACC = 0. This time JUMPZ jumps: PC ← 1008, skipping the loop body.
HALT: the CPU stops. 19 instructions were fetched (8 + 8 + 3). A Java while loop compiles to conditional and unconditional jumps like these.
Computer Architecture
The von Neumann Architecture
- Program instructions and data are kept in a shared memory.
- The CPU processes instructions in order; branch instructions can change that order.
- Instructions and data arrive over the same path, so CPU–memory transfer limits performance (the von Neumann bottleneck).
Computer Architecture
von Neumann vs. Harvard
| Property | von Neumann | Harvard |
|---|---|---|
| Memory | Instructions and data share one memory | Instructions and data in separate memories |
| Bus | One bus: instructions and data move in turn | Two buses: instructions and data are read at once |
| Flexibility | A program can be loaded and modified like data | Program memory is usually fixed (flash) |
| Typical use | General-purpose computers | Microcontrollers, signal processors |
Modern processors combine both: main memory is shared, but the L1 cache next to the core is split into instruction and data caches (modified Harvard).
Computer Architecture
Instruction Encoding
A machine instruction usually specifies the operation and the values or addresses it uses.
Identifies the kind of operation.
ADDThe register, value or address the operation uses.
R1, R2The architecture fixes how many bit fields there are and where.
All the instructions the processor understands directly.
Computer Architecture · RISC / CISC
Instruction Set Design: RISC and CISC
| Property | RISC | CISC |
|---|---|---|
| Instruction set | A few simple instructions | Many complex instructions |
| Instruction length | Fixed (e.g. 32 bits) | Variable (1–15 bytes on x86-64) |
| Memory access | Only LOAD / STORE instructions | Arithmetic instructions can take memory operands directly |
| Example | ARM, RISC-V | x86-64 |
RISC: a = a + b in memory
LOAD R1, a LOAD R2, b ADD R1, R1, R2 STORE R1, a
CISC (x86-64): the same job
MOV EAX, [b] ADD [a], EAX
2 instructions instead of 4, but the second is split internally into read, add and write steps.
Computer Architecture
Architectural Limits
Only so much data can be stored at once.
Every instruction costs time and energy.
A fixed number of bits can cause overflow and rounding.
The CPU can sit idle while waiting for data from memory.
Not every problem can be parallelized equally well.
Programs and data share system resources, so they must be isolated.
Computer Architecture · Example
Performance: Instruction Count, CPI and Clock Rate
CPU time = instruction count × CPI ÷ clock rate
CPI: average clock cycles per instruction
| Program: 2 × 10⁹ instructions | Clock rate | CPI | Time |
|---|---|---|---|
| Processor A | 3 GHz | 1.5 | 2 × 10⁹ × 1.5 ÷ (3 × 10⁹) = 1.0 s |
| Processor B | 4 GHz | 2.5 | 2 × 10⁹ × 2.5 ÷ (4 × 10⁹) = 1.25 s |
| A, with a better compiler (1.6 × 10⁹ instructions) | 3 GHz | 1.5 | 1.6 × 10⁹ × 1.5 ÷ (3 × 10⁹) = 0.8 s |
Depends on the algorithm, compiler and instruction set.
Depends on the internal design and memory stalls (cache misses).
Limited by process technology, power and heat.
B has the higher clock rate, yet A is 1.25× faster: no single number determines performance.
Programming
Levels of Abstraction in Programming
Each level hides the details of the one below. High-level languages are readable and portable; assembly and its binary form, machine code, are specific to a processor architecture. The sample lines are illustrative.
Programming · Run the code step by step
Tracing total = a + b
One line of Java becomes three processor instructions. Memory holds a = 12, b = 8.
LOAD: the value of a moves from memory into a register → R1 = 12. The ALU only works on registers.
ADD: b is read, the ALU computes 12 + 8 = 20, and the result goes into R1.
STORE: the 20 in R1 is written to memory, at the location of total. The Java line is done: total = 20.
// Java: int total = a + b; LOAD R1, a ; R1 ← a ADD R1, b ; R1 ← R1 + b STORE R1, total ; total ← R1
Summary
Chapter Summary
- A computer represents all data as bit strings.
- Binary is exact for integers; floating point can only approximate some decimals.
- ASCII and Unicode map characters to numeric codes.
- In the stored-program model, instructions and data are kept in memory.
- Logic gates are the building blocks of circuits such as adders; binary addition uses a chain of full adders.
- The CPU runs instructions in the fetch–decode–execute cycle.
- High-level languages abstract away hardware details.
Summary
Review Questions
- What is 1101₂ in decimal?
- What is the range of an 8-bit signed integer?
- What is the key difference between ASCII and Unicode?
- What is the advantage of storing program instructions in memory?
- What do the data, address and control buses do?
- How does an assembly instruction relate to a machine instruction?
Summary
Exercises
- In 8-bit two's complement, what number is 11111110?
- Convert 3F₁₆ to binary and decimal.
- Encode “ç” (U+00E7) in UTF-8; how many bytes does it take?
- What is 250 + 10 in an 8-bit unsigned register? Why?
- In “Running a Program Step by Step”, if 1004 = 7 and 1005 = 5, what would be in 1006 when the program ends? How many instructions would be fetched?
- Write the assembly for
int diff = a - b;using LOAD / SUB / STORE.
Show answers
- Invert and add 1: 00000001 + 1 = 00000010 = 2 → the number is −2.
- 3F₁₆ = 0011 1111₂ = 63.
- U+00E7 = 00011100111 → 11000011 10100111 = C3 A7, 2 bytes.
- 260 doesn't fit in 8 bits; the 9th bit is dropped → 4 (overflow).
- 1006 = 7 + 5 = 12; 4 instructions are fetched (LOAD, ADD, STORE, HALT).
LOAD R1, a·SUB R1, b·STORE R1, diff.
Summary
References
- Patterson and Hennessy, Computer Organization and Design.
- Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective.
- Unicode Consortium, The Unicode Standard.
- IEEE 754, Floating-Point Arithmetic.