Chapter 1.3: Computers

Computer Fundamentals and Introduction to Programming
Data representationTextStored programsCPU and memoryAbstraction
Sercan KÜLCÜ · Giresun University

Introduction

Learning Outcomes

Number representation

Explain the consequences of a fixed number of bits (negative numbers, overflow, floating point).

Text representation

Distinguish ASCII, Unicode and strings.

Program execution

Explain the stored-program concept and the fetch–decode–execute cycle.

Architecture

Interpret how the CPU, memory, registers and buses work together.

Introduction

The Basic Parts of a Computer

Inputkeyboard, sensorCPUexecutes instructionsMemoryinstructions + dataOutput: screen, printerwriteread
CPU

Executes instructions.

Memory

Stores data and instructions.

Input / output

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.

Informationletter “A”EncodingASCII 65Bit string01000001Programto lowercase:add 32Output“a” on screen01000001 (65) + 00100000 (32) = 01100001 (97) = “a”

Data Representation · Step by step

Number Bases

SystemBaseDigitsExample
Binary20–11101₂
Octal80–715₈
Decimal100–913₁₀
Hexadecimal160–9, A–FD₁₆
45₁₀ =101101₂(32 + 8 + 4 + 1)

45₁₀ = 101101₂. Octal and hexadecimal are shorthand for long binary numbers.

45₁₀ =101101₂(32 + 8 + 4 + 1)groups of 3:101= 5101= 5→ 55₈(5·8 + 5 = 45)

Since 8 = 2³, group the bits in threes from the right: 101 101 → 55₈.

45₁₀ =101101₂(32 + 8 + 4 + 1)groups of 3:101= 5101= 5→ 55₈(5·8 + 5 = 45)groups of 4:0010= 2 = 21101= 13 = D→ 2D₁₆(2·16 + 13 = 45)

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.

A single zero

There is no separate positive and negative zero.

Arithmetic

The same adder circuit works for positive and negative numbers.

Range of n bits

From −2ⁿ⁻¹ to 2ⁿ⁻¹−1.

8 bits: −128 … 127
00000000110010200113010040101501106011171000−81001−71010−61011−51100−41101−31110−21111−14 bitsleading bit 1 → negative7 + 1 → −8 (overflow)

Data Representation · Step by step

Two's Complement: How Do We Write −5?

+500000101

In 8 bits, +5 = 00000101. Two steps give −5.

+500000101invert11111010

1. Invert every bit (0 ↔ 1): 11111010. This is the ones' complement.

+500000101invert11111010+1 → −511111011

2. Add 1: 11111010 + 1 = 11111011 = −5. The leading 1 marks the number as negative.

+500000101−511111011sum100000000carry out is discardedleft in 8 bits: 00000000 = 0 ✓

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

76543210a00000101= +5b00001001= +9

The processor needs no separate subtractor: a − b = a + (−b). First we find the two's complement of b.

76543210b00001001= +9NOTevery bit is invertedinv11110110ones' complement

Invert all the bits (0 ↔ 1): 11110110.

76543210inv11110110+00000001neg11110111= −9

Add 1: 11110111 = −9. (Read as unsigned, 247 = 256 − 9.)

76543210a00000101+5neg+11110111−9s11111100= 252 unsigned

Plain binary addition: 5 + 247 = 252 = 11111100. It doesn't reach 256, so there is no carry into a 9th bit.

76543210s11111100sign bit 1 → negativeNOT s00000011+100000100= 4 → s = −4

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

20011001000+ 10001100100

An 8-bit unsigned number is at most 255. Let's add 200 + 100.

20011001000+ 10001100100= 3001001011009th bit

The true sum 300 = 100101100₂ needs 9 bits.

20011001000+ 10001100100= 3001001011009th bitleft in 8 bits: 00101100 = 44

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 sumCarrySigned overflowResult
5 + (−9)00000101 + 11110111 = 111111000no−4 ✓
(−1) + (−1)11111111 + 11111111 = 1 111111101no−2 ✓
100 + 5001100100 + 00110010 = 100101100yes−106 ✗
(−100) + (−50)10011100 + 11001110 = 1 011010101yes+106 ✗
Carry (9th bit)

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.

Signed overflow

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

Floating point

Stores very large and very small values, much like scientific notation.

Limited precision

Not every decimal value can be represented exactly with finitely many bits.

Rounding

Calculations can pick up small errors.

Comparison

Compare floating-point results within a tolerance.

0.1 + 0.2  →  0.30000000000000004   // computed with double
stored value when you write 0.30.29999999999999998890result of 0.1 + 0.20.30000000000000004441true 0.3 (not exactly a double)gap between neighboring doubles ≈ 5.55 × 10⁻¹⁷ → 0.1 + 0.2 == 0.3 is false

Data Representation · Step by step

Why Can't 0.1 Be Stored Exactly?

Fraction × 2 repeatedly; the integer part is the next bit0.1₁₀ =0,

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.

Fraction × 2 repeatedly; the integer part is the next bit0.1 × 2 = 0.2bit 00.1₁₀ =0.0

0.1 × 2 = 0.2 → integer part 0.

Fraction × 2 repeatedly; the integer part is the next bit0.1 × 2 = 0.2bit 00.2 × 2 = 0.4bit 00.1₁₀ =0.00

0.2 × 2 = 0.4 → integer part 0.

Fraction × 2 repeatedly; the integer part is the next bit0.1 × 2 = 0.2bit 00.2 × 2 = 0.4bit 00.4 × 2 = 0.8bit 00.1₁₀ =0.000

0.4 × 2 = 0.8 → integer part 0.

Fraction × 2 repeatedly; the integer part is the next bit0.1 × 2 = 0.2bit 00.2 × 2 = 0.4bit 00.4 × 2 = 0.8bit 00.8 × 2 = 1.6bit 10.1₁₀ =0.0001

0.8 × 2 = 1.6 → integer part 1; subtract 1 and continue with the fraction.

Fraction × 2 repeatedly; the integer part is the next bit0.1 × 2 = 0.2bit 00.2 × 2 = 0.4bit 00.4 × 2 = 0.8bit 00.8 × 2 = 1.6bit 10.6 × 2 = 1.2bit 10.1₁₀ =0.00011…0.2 is back → “0011” repeats foreverCut off at finitely many bits → small rounding error

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)

00111101110011001100110011001101sign (1)exp. (8 bits)fraction (23 bits)0+01111011₂ = 123exp = 123 − 127 = −41.10011001100110011001101₂≈ 1.6000000238value = (−1)^sign × 1.frac × 2^(exp − 127)0.1 (float) = 0x3DCCCCCD≈ 1.6000000238 × 2⁻⁴ ≈ 0.10000000149
  • 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

x = −6.25 → sign bit 1signexp. (8 bits)fraction (23 bits)1

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.

6 = 110₂ · 0.25 × 2 = 0.5 → 0 · 0.5 × 2 = 1.0 → 1signexp. (8 bits)fraction (23 bits)1

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.

110.01₂ = 1.1001₂ × 2²signexp. (8 bits)fraction (23 bits)1

As in scientific notation, the point moves until a single 1 is left of it: 2 places left → 1.1001 × 2².

exp field = 2 + 127 = 129 = 10000001₂signexp. (8 bits)fraction (23 bits)110000001

The exponent field stores the true exponent plus 127 (the bias): 2 + 127 = 129. This lets negative exponents be stored unsigned.

fraction = bits after the point of 1.1001 → 1001 and 19 zerossignexp. (8 bits)fraction (23 bits)11000000110010000000000000000000

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.

−6.25 = C0C80000₁₆signexp. (8 bits)fraction (23 bits)11000000110010000000000000000000C0C80000

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.

A

65

01000001
a

97

01100001
0

48

00110000
Line feed (LF)

Control character: 10

00001010
“A” = 6501000001“a” = 9701100001the 32s placeUpper and lower casediffer in a single bit:97 − 65 = 32

Text Representation · Step by step

Bitwise Operations: Changing a Letter's Case

76543210'G'01000111= 71'g'01100111= 103

“G” = 71, “g” = 103: the difference is 32 = 2⁵. Upper and lower case differ only in bit 5 (counting from 0 on the right).

76543210c01000111'G'mOR00100000k01100111= 103 → 'g'

OR: a bit that is 1 in the mask becomes 1; the other bits are unchanged. Uppercase became lowercase.

76543210k01100111'g'NOT mAND11011111b01000111= 71 → 'G'

AND with the inverted mask: the masked bit is cleared to 0, the rest are kept. Lowercase became uppercase again.

76543210b01000111'G'mXOR00100000t01100111= 103 → 'g'

XOR: the masked bit is flipped. XOR with the same mask twice gives back the original value.

76543210c01000111= 71<< 1every bit shifts left; a 0 enters on the rights10001110= 142 = 71 × 2

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.

Code point

The character's universal number.

A → U+0041
UTF-8

Encodes Unicode characters as variable-length byte sequences.

Wide coverage

Turkish letters, other alphabets, math symbols and emoji.

File encoding

The same code point can be stored in different Unicode transformation formats.

charactercode pointUTF-8 bytes (hex)lengthAU+0041411 byteğU+011FC49F2 bytes€U+20ACE282AC3 bytes😀U+1F600F09F98804 bytes

Text Representation · Step by step

UTF-8 Encoding: “ğ”

“ğ” → code point U+011F = 287

Unicode assigns the letter “ğ” the code point U+011F (decimal 287). How does UTF-8 write this number as bytes?

“ğ” → code point U+011F = 28711 bits:00100011111

287 in binary is 00100011111. It doesn't fit in 7 bits (not ASCII) but fits in 11 → 2 bytes are needed.

“ğ” → code point U+011F = 28711 bits:001000111115 bits6 bits

The 2-byte UTF-8 template is 110xxxxx 10xxxxxx: the 11 bits are split as 5 + 6.

“ğ” → code point U+011F = 28711 bits:001000111115 bits6 bitstemplate:11000100= C4₁₆10011111= 9F₁₆

The bits drop into the template: 11000100 10011111 = C4 9F. The leading 110 and 10 tell each byte's role.

“ğ” → code point U+011F = 28711 bits:001000111115 bits6 bitstemplate:11000100= C4₁₆10011111= 9F₁₆More examples:A41 (1 B)ğC4 9F (2 B)€E2 82 AC (3 B)😀F0 9F 98 80 (4 B)

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.

Merhaba␣+DünyaMerhaba␣Dünyalength: 13 characters · 14 bytes in UTF-8 (“ü” is 2 bytes)
Length

The number of characters or code units.

Concatenation

Joining two strings end to end.

Search

Finding a character or substring.

Comparison

Checking contents and ordering.

The number of characters on screen and the number of bytes (code units) in memory are not always the same.

Data Representation

Structured Information

Bits gain meaning when organized into larger structures.

Bit1Byte01000001Character“A”Fieldname = “Ayşe”Recordid · name · gradeFilerecord 1, 2, 3 …123Ayşe
  • 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 mistakeCorrect
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 ten10₁₆ = 16; in hexadecimal, ten is written A.
In two's complement, only inverting the bits11111010 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 numberThe character '7' has code 55; its numeric value is '7' − '0' = 7.
Taking character count for byte countIn UTF-8, “çağ” is 3 characters but 5 bytes (ç 2 + a 1 + ğ 2).
Confusing carry with signed overflowCarry is a problem for the unsigned reading, overflow for the signed one; either can happen without the other.

Computer Architecture

Logic Gates

ABANDORXORNOT A
000001
010111
100110
111100
  • 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.
ABANDABORABXORANOT

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?B?XORANDSUMCARRYA XOR BA AND BThe result in two bits: CARRY SUM

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.

A0B0XORANDSUM = 0CARRY = 0A XOR BA AND B0 + 0 = 00₂ = 0written in the order (CARRY, SUM)

0 + 0: XOR(0, 0) = 0, AND(0, 0) = 0 → 00₂ = 0.

A0B1XORANDSUM = 1CARRY = 0A XOR BA AND B0 + 1 = 01₂ = 1written in the order (CARRY, SUM)

0 + 1: XOR(0, 1) = 1, AND(0, 1) = 0 → 01₂ = 1. No carry.

A1B1XORANDSUM = 0CARRY = 1A XOR BA AND B1 + 1 = 10₂ = 2written in the order (CARRY, SUM)

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

FA0full addera0 = 0b0 = 1s0 = ?FA1full addera1 = 1b1 = 1s1 = ?FA2full addera2 = 1b2 = 1s2 = ?FA3full addera3 = 0b3 = 0s3 = ?c=0c=?c=?c=?c=?

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.

FA0full addera0 = 0b0 = 1s0 = 1FA1full addera1 = 1b1 = 1s1 = ?FA2full addera2 = 1b2 = 1s2 = ?FA3full addera3 = 0b3 = 0s3 = ?c=0c=0c=?c=?c=?

i = 0: a0 = 0, b0 = 1, c = 0 → t = 1, s0 = 1; the inputs are not both 1, so the carry is 0.

FA0full addera0 = 0b0 = 1s0 = 1FA1full addera1 = 1b1 = 1s1 = 0FA2full addera2 = 1b2 = 1s2 = ?FA3full addera3 = 0b3 = 0s3 = ?c=0c=0c=1c=?c=?

i = 1: 1 + 1 + 0 → t = 0, s1 = 0; a1 AND b1 = 1, so the carry is 1 (like 5 + 5 in decimal).

FA0full addera0 = 0b0 = 1s0 = 1FA1full addera1 = 1b1 = 1s1 = 0FA2full addera2 = 1b2 = 1s2 = 1FA3full addera3 = 0b3 = 0s3 = ?c=0c=0c=1c=1c=?

i = 2: 1 + 1 + 1 = 3 = 11₂ → s2 = 1, carry 1 again.

FA0full addera0 = 0b0 = 1s0 = 1FA1full addera1 = 1b1 = 1s1 = 0FA2full addera2 = 1b2 = 1s2 = 1FA3full addera3 = 0b3 = 0s3 = 1c=0c=0c=1c=1c=0

i = 3: 0 + 0 + 1 → s3 = 1, carry 0. The final carry is 0, so the result fits in 4 bits.

FA0full addera0 = 0b0 = 1s0 = 1FA1full addera1 = 1b1 = 1s1 = 0FA2full addera2 = 1b2 = 1s2 = 1FA3full addera3 = 0b3 = 0s3 = 1c=0c=0c=1c=1c=0

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

Memory1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060programdatasame memory, same address space

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.

Flexibility

The same hardware does different jobs with different programs.

Updates

Behavior can change through software, without changing the hardware.

General purpose

One system can handle computing, communication and media.

Computer Architecture

The Fetch–Decode–Execute Cycle

1. Fetchinstruction at PC's address → IRPC ← PC + 12. Decodecontrol unitopcode + operand3. ExecuteALU / memory operationresult to register / memoryrepeat
  • 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

CPUControl unitsequencing and signalsALU+ − AND OR compareRegistersPCIRACCCachefrequently used data and instructionsRAM
Control unit

Manages the order of instructions and the movement of data.

ALU

Performs arithmetic and logical operations.

Registers

Small, fast storage used during an operation.

Cache

Keeps frequently used data and instructions close to the processor.

Computer Architecture · Step by step

Buses: Reading from Memory

CPUMemory[1004] = 12Address busControl busData busI/O devices share the same buses

Three buses link the CPU to memory and I/O. Example: the CPU reads address 1004.

CPUMemory[1004] = 12Address bus1004Control busData busI/O devices share the same buses

Address bus: the CPU sends the address → 1004. One-way.

CPUMemory[1004] = 12Address busControl busREADData busI/O devices share the same buses

Control bus: the CPU sends READ (or WRITE), plus timing signals.

CPUMemory[1004] = 12ACC ← 12Address busControl busData bus12I/O devices share the same buses

Data bus: memory returns the contents (12). It is bidirectional.

BusRole
Data busCarries data between CPU, memory and devices.
Address busSelects the memory or device location.
Control busCarries read, write and timing signals.

Computer Architecture

Memory Addresses

AddressContents
1000LOAD 1004
1001ADD 1005
1002STORE 1006
1003HALT
100412
10058
10060
  • 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.
1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060program (instructions)dataPC advances in order: fetch → decode → executewithout HALT: 12 would run as an instruction

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).

address 100address 101address 102address 103Big-endian12345678Little-endian785634120x12345678 =12 34 56 78(12 most significant)
Big-endian

The most significant byte at the lowest address. Network protocols use this order (network byte order).

Little-endian

The least significant byte at the lowest address. x86-64 and most ARM systems work this way.

The problem

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

FETCHDECODEEXECUTECPUPC1000IR—ACC0ALUaddress busdata bus1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Start: PC = 1000 (address of the first instruction), ACC = 0. The CPU starts the cycle.

FETCHDECODEEXECUTECPUPC1001IRLOAD 1004ACC0ALUaddress busdata bus1000LOAD 10041000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Fetch: PC's 1000 goes out on the address bus; the instruction LOAD 1004 arrives in IR over the data bus. PC increments → 1001.

FETCHDECODEEXECUTECPUPC1001IRLOAD 1004ACC0ALUaddress busdata bus1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Decode: the control unit splits IR: opcode LOAD, address 1004 → mem[1004] will be read into ACC.

FETCHDECODEEXECUTECPUPC1001IRLOAD 1004ACC12ALUaddress busdata bus1004121000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Execute: address 1004 is read and 12 is loaded into ACC.

FETCHDECODEEXECUTECPUPC1002IRADD 1005ACC12ALUaddress busdata bus1001ADD 10051000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Fetch: PC's 1001 goes out on the address bus; the instruction ADD 1005 arrives in IR over the data bus. PC increments → 1002.

FETCHDECODEEXECUTECPUPC1002IRADD 1005ACC12ALUaddress busdata bus1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Decode: the control unit splits IR: opcode ADD, address 1005 → mem[1005] will be added to ACC.

FETCHDECODEEXECUTECPUPC1002IRADD 1005ACC20ALU12 + 8= 20address busdata bus100581000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Execute: the 8 at address 1005 is read, the ALU computes 12 + 8 = 20, and the result goes into ACC.

FETCHDECODEEXECUTECPUPC1003IRSTORE 1006ACC20ALUaddress busdata bus1002STORE 10061000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Fetch: PC's 1002 goes out on the address bus; the instruction STORE 1006 arrives in IR over the data bus. PC increments → 1003.

FETCHDECODEEXECUTECPUPC1003IRSTORE 1006ACC20ALUaddress busdata bus1000LOAD 10041001ADD 10051002STORE 10061003HALT1004121005810060

Decode: the control unit splits IR: opcode STORE, address 1006 → ACC will be written to mem[1006].

FETCHDECODEEXECUTECPUPC1003IRSTORE 1006ACC20ALUaddress busdata bus1006201000LOAD 10041001ADD 10051002STORE 10061003HALT10041210058100620

Execute: the 20 in ACC is written to address 1006 over the data bus.

FETCHDECODEEXECUTECPUPC1004IRHALTACC20ALUaddress busdata bus1003HALT1000LOAD 10041001ADD 10051002STORE 10061003HALT10041210058100620

Fetch: PC's 1003 goes out on the address bus; the instruction HALT arrives in IR over the data bus. PC increments → 1004.

FETCHDECODEEXECUTECPUPC1004IRHALTACC20ALUhaltedaddress busdata bus1000LOAD 10041001ADD 10051002STORE 10061003HALT10041210058100620

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
CPUPC1000ACC0Memory1010 count21011 sum01012 one1

count runs down from 2; when the loop ends, sum = 2 + 1.

CPUPC1002ACC2Memory1010 count21011 sum01012 one1

LOAD → ACC = 2. JUMPZ: ACC ≠ 0, no jump; PC stays at the next address, 1002.

CPUPC1004ACC2Memory1010 count21011 sum21012 one1

ADD: 2 + 0 = 2; STORE → sum = 2.

CPUPC1007ACC1Memory1010 count11011 sum21012 one1

LOAD, SUB, STORE: count 2 − 1 = 1.

CPUPC1000ACC1Memory1010 count11011 sum21012 one1PC ← 1000

JUMP 1000: PC, which the fetch set to 1008, is overwritten with 1000. The loop goes back to the top.

CPUPC1000ACC0Memory1010 count01011 sum31012 one1PC ← 1000

The second pass runs the same 8 instructions: sum = 1 + 2 = 3, count = 0, JUMP again.

CPUPC1008ACC0Memory1010 count01011 sum31012 one1PC ← 1008

LOAD → ACC = 0. This time JUMPZ jumps: PC ← 1008, skipping the loop body.

CPUPC1009ACC0Memory1010 count01011 sum31012 one1

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

CPUControl unitALURegisters (PC, IR, ACC)Memoryprogram instructions + data(shared)single bus: bottleneckInputOutput
  • 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

von NeumannCPUMemoryinstructions + datasingle busHarvardinstruction memoryCPUdata memory
Propertyvon NeumannHarvard
MemoryInstructions and data share one memoryInstructions and data in separate memories
BusOne bus: instructions and data move in turnTwo buses: instructions and data are read at once
FlexibilityA program can be loaded and modified like dataProgram memory is usually fixed (flash)
Typical useGeneral-purpose computersMicrocontrollers, 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.

ADD 1005 →0010001111101101opcode (4 bits) = 2 → ADDaddress (12 bits) = 1005Example (hypothetical) codes: HALT = 0000 · LOAD = 0001 · ADD = 0010 · STORE = 0011
Opcode

Identifies the kind of operation.

ADD
Operand

The register, value or address the operation uses.

R1, R2
Instruction format

The architecture fixes how many bit fields there are and where.

Instruction set

All the instructions the processor understands directly.

Computer Architecture · RISC / CISC

Instruction Set Design: RISC and CISC

PropertyRISCCISC
Instruction setA few simple instructionsMany complex instructions
Instruction lengthFixed (e.g. 32 bits)Variable (1–15 bytes on x86-64)
Memory accessOnly LOAD / STORE instructionsArithmetic instructions can take memory operands directly
ExampleARM, RISC-Vx86-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

Finite memory

Only so much data can be stored at once.

Finite speed

Every instruction costs time and energy.

Representation errors

A fixed number of bits can cause overflow and rounding.

Data bottleneck

The CPU can sit idle while waiting for data from memory.

Limits of parallelism

Not every problem can be parallelized equally well.

Security

Programs and data share system resources, so they must be isolated.

CPU1 cycle ≈ 0.3 ns (3 GHz)RAMaccess ≈ 100 nsnarrow buswaiting for data…100 ns ÷ 0.3 ns ≈ 300 cycles: caches exist to cut this wait

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⁹ instructionsClock rateCPITime
Processor A3 GHz1.52 × 10⁹ × 1.5 ÷ (3 × 10⁹) = 1.0 s
Processor B4 GHz2.52 × 10⁹ × 2.5 ÷ (4 × 10⁹) = 1.25 s
A, with a better compiler (1.6 × 10⁹ instructions)3 GHz1.51.6 × 10⁹ × 1.5 ÷ (3 × 10⁹) = 0.8 s
Instruction count

Depends on the algorithm, compiler and instruction set.

CPI

Depends on the internal design and memory stalls (cache misses).

Clock rate

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

Problem and algorithm“add two numbers”High-level language · Javatotal = a + b;Compiler / virtual machineJava bytecode: iaddAssemblyADD R1, bMachine instructions0010 0011 1110 1101Digital circuitsadder circuit · transistorsabstract to concrete

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

CPUR10ALUMemorya12b8total0

One line of Java becomes three processor instructions. Memory holds a = 12, b = 8.

CPUR112ALUMemorya12b8total0

LOAD: the value of a moves from memory into a register → R1 = 12. The ALU only works on registers.

CPUR120ALU12+8=20Memorya12b8total0

ADD: b is read, the ALU computes 12 + 8 = 20, and the result goes into R1.

CPUR120ALUMemorya12b8total20

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

  1. What is 1101₂ in decimal?
  2. What is the range of an 8-bit signed integer?
  3. What is the key difference between ASCII and Unicode?
  4. What is the advantage of storing program instructions in memory?
  5. What do the data, address and control buses do?
  6. How does an assembly instruction relate to a machine instruction?

Summary

Exercises

  1. In 8-bit two's complement, what number is 11111110?
  2. Convert 3F₁₆ to binary and decimal.
  3. Encode “ç” (U+00E7) in UTF-8; how many bytes does it take?
  4. What is 250 + 10 in an 8-bit unsigned register? Why?
  5. 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?
  6. Write the assembly for int diff = a - b; using LOAD / SUB / STORE.
Show answers
  1. Invert and add 1: 00000001 + 1 = 00000010 = 2 → the number is −2.
  2. 3F₁₆ = 0011 1111₂ = 63.
  3. U+00E7 = 00011100111 → 11000011 10100111 = C3 A7, 2 bytes.
  4. 260 doesn't fit in 8 bits; the 9th bit is dropped → 4 (overflow).
  5. 1006 = 7 + 5 = 12; 4 instructions are fetched (LOAD, ADD, STORE, HALT).
  6. 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.

The End

Chapter 1.3: Computers
Previous: Chapter 1.2: Computer Hardware · Next: Chapter 1.4: The Internet
1 / 1Sercan KÜLCÜ. All rights reserved.