THE CPU
How it works
Introduction
• Processing device
Data is entered into a computer using an INPUT device like
a keyboard and mouse. At the end the data will then be
output via monitor or speakers
However, for the data to get from the input stage to the
output , something important needs to take place
Introduction...
The data needs to be processed
There is a processing device inside all computers called
Central Processing Unit (CPU)
The CPU is responsible for taking data inputted by the user
and then performing any necessary processing, for example
Introduction...
• Sorting the data
• Performing calculations on it
• Saving it to the computer’s memory etc
• Please NOTE, the more powerful the processing device, the
more powerful the computer will be
Introduction...System unit
Introduction...
Introduction...
Introduction...
• Please note that in most computer, processing devices
(processors/CPU) is attached to the MOTHERBOARD inside
a CPU Box
Central Processing Unit (CPU)
Central Processing Unit (CPU)
RAM (cont.)
20 bits of
address
Address
20
Data input 2 Data Output
bytes of
RAM
Write
(1 Mega-byte)
8 bits (1 byte)
of data
RAM (cont.)
• When you talk about the memory of a computer, most often
you’re talking about its RAM.
• If a program is stored in RAM, that means
that a sequence of instructions are stored in consecutively
addressed bytes in the RAM.
• Data values (variables) are stored anywhere in RAM, not
necessarily sequentially
• Both instructions and data are accessed from RAM using
addresses
• RAM is one (crucial) part of the computer’s
overall architecture
Computer Architecture
Central Processing
Unit
CPU Input/
Output
Bus Devices
RAM
Computer ORGANISATION
CPU Keyboard Display
Bus
Hard
CD-ROM
RAM Disk
The Bus
• What is a bus?
• It is a simplified way for many devices to
communicate to each other.
• Looks like a “highway” for information.
• Actually, more like a “basket” that they all share.
CPU Keyboard Display
Bus
The Bus
CPU Keyboard Display
Bus
The Bus
• Suppose CPU needs to check to see if the user typed
anything.
CPU Keyboard Display
Bus
The Bus
• CPU puts “Keyboard, did the user type anything?”
(represented in some way) on the Bus.
CPU Keyboard Display
Bus
“Keyboard, did the user type anything?”
The Bus
• Each device (except CPU) is a State Machine that constantly
checks to see what’s on the Bus.
CPU Keyboard Display
Bus
“Keyboard, did the user type anything?”
The Bus
• Keyboard notices that its name is on the Bus,
and reads info. Other devices ignore the info.
CPU Keyboard Display
Bus
“Keyboard, did the user type anything?”
The Bus
• Keyboard then writes “CPU: Yes, user typed ‘a’.” to the Bus.
CPU Keyboard Display
Bus
“CPU: Yes, user typed ‘a’.”
The Bus
• At some point, CPU reads the Bus, and gets
the Keyboard’s response.
CPU Keyboard Display
Bus
“CPU: Yes, user typed ‘a’.”
Computer Architecture
CPU Keyboard Display
Bus
Hard
CD-ROM
RAM Disk
Inside the CPU
• The CPU is the brain of the computer.
• It is the part that actually executes the instructions.
• Let’s take a look inside.
Inside the CPU (cont.)
Memory Registers Temporary Memory.
Computer “Loads” data from
Register 0
RAM to registers, performs
Register 1 operations on data in registers,
and “stores” results from
Register 2 registers back to RAM
Register 3
Remember our initial example: “read value of A from memory; read value of B from memory; add
values of A and B; put result in memory in variable C.” The reads are done to registers, the
addition is done in registers, and the result is written to memory from a register.
Inside the CPU (cont.)
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
For doing basic
Arithmetic / Logic
Operations on Values stored
in the Registers
Inside the CPU (cont.)
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
To hold the current
instruction
Instruction Register
Inside the CPU (cont.)
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
To hold the
Instruction Register address of the
current instruction
Instr. Pointer (IP)
in RAM
Inside the CPU (cont.)
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
Control Unit
Instruction Register (State Machine)
Instr. Pointer (IP)
The Control Unit
• It all comes down to the Control Unit.
• This is just a State Machine.
• How does it work?
The Control Unit
Control Unit State Machine has very simple structure:
• 1) Fetch: Ask the RAM for the instruction
whose address is stored in IP.
• 2) DECODE -INTERPRETE the instructions
• 3) Execute: There are only a small number
of possible instructions.
Depending on which it is, do
what is necessary to execute it.
• 4) Repeat: Add 1 to the address stored in
IP, and go back to Step 1 !
The Control Unit is a State Machine
Fetch
Add Store
Load … Goto
Exec Exec Exec Exec Exec
… … … …
…
Add 1
to IP
A Simple Program
Running the Program
2 a
1 c
3 b
r1 2
r2
r3
Logic Memory
r4
IR Load a into r1 Load a into r1 2005
Load b into r3 2006
IP 2005
r2 r1 + r3 2007
Store r2 into c 2008
CPU
Running the Program
2 a
1 c
3 b
r1 2
r2
r3 3
Logic Memory
r4
IR Load b into r3 Load a into r1 2005
Load b into r3 2006
IP 2006
r2 r1 + r3 2007
Store r2 into c 2008
CPU
Running the Program
2 a
1 c
3 b
r1 2
r2 5
r3 3
Logic Memory
r4
IR r2 r1 + r3 Load a into r1 2005
Load b into r3 2006
IP 2007
r2 r1 + r3 2007
Store r2 into c 2008
CPU
Running the Program
2 a
1 c
3 b
r1 2
r2 5
r3 3
Logic Memory
r4
IR Store r2 into c Load a into r1 2005
Load b into r3 2006
IP 2008
r2 r1 + r3 2007
Store r2 into c 2008
CPU
Running the Program
2 a
5 c
3 b
r1 2
r2 5
r3 3
Logic Memory
r4
IR Store r2 into c Load a into r1 2005
Load b into r3 2006
IP 2008
r2 r1 + r3 2007
Store r2 into c 2008
CPU
Putting it all together
• Computer has many parts, connected by a Bus:
CPU Keyboard Display
Bus
Hard
CD-ROM
RAM Disk
Putting it all together
• The RAM is the computer’s main memory.
• This is where programs and data are stored.
CPU Keyboard Display
Bus
Hard
CD-ROM
RAM Disk
Putting it all together
• The CPU goes in a never-ending cycle, reading
instructions from RAM and executing them.
CPU Keyboard Display
Bus
Hard
CD-ROM
RAM Disk
Putting it all together
• This cycle is orchestrated by the Control Unit in the CPU.
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
Control Unit
Instruction Register (State Machine)
Instr. Pointer (IP)
Back to the Control Unit
• It simply looks at where IP is pointing, reads the instruction
there from RAM, and executes it.
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
Control Unit
Instruction Register (State Machine)
Instr. Pointer (IP)
Putting it all together
• To execute an instruction, the Control Unit uses the ALU plus
Memory and/or the Registers.
Memory Registers
Register 0 Arithmetic
Register 1 / Logic
Unit
Register 2
Register 3
Control Unit
Instruction Register (State Machine)
Instr. Pointer (IP)
Computer Programming
Where we are
• Examined the hardware for a computer
– Truth tables
– Logic gates
– States and transitions in a state machine
– The workings of a CPU and Memory
• Now, want to program the hardware
Programs and Instructions
• Programs are made up of instructions
• CPU executes one instruction every clock cycle
• Modern CPUS do more, but we ignore that
• Specifying a program and its instructions:
• Lowest level: Machine language
• Intermediate level: Assembly language
• Typically today: High-level programming language
Specifying a Program and its
Instructions
• High-level programs: each statement translates to many
instructions Load a into r1
• E.g. c a + b to: Load b into r3
r2 r1 + r3
Store r2 into c
• Assembly language: specify each machine instruction,
using mnemonic form
• E.g. Load r1, A
• Machine language: specify each machine instruction, using
bit patterns
• E.g. 1101101000001110011
Translators
• Language translators are a component of systems software
• They translate source codes(human readable) to object
codes(machine executable)
• Three translators
– Machine code ….machine readable codes(0’s and 1’s) e.g 100=4
no special translator is required
– Assembly –provided by manufacturers/ it was computer specific
– High level Interpreters an compilers
Machine/Assembly Language
• We have a machine that can execute instructions
• Basic Questions:
• What instructions?
• How are these instructions represented to the computer
hardware?
Machine Languages
• Different types of CPU’s understand different
instructions
• Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel
x86 family)
• PowerPC (Mac)
• DragonBall (Palm Pilot)
• StrongARM/MIPS (WinCE)
• Many Others (specialized or general-purpose)
• They represent instructions differently in their
assembly/machine languages (even common ones)
• Let’s look instructions for a simple example CPU
What are
• Programming Programming Languages?
languages are algorithms
designed for specific performers.
• Each programming language has its own
•.
command system executed by a computer
• Commands are written in machine language
or machine code.
• Assembly language is used to convert
machine code into symbolic form for easier
understanding.
• High-level programming languages use
commands that combine sequences of
machine instructions
Photo by Pexels
Machine Language and Assembly Language
• Machine language commands are
written in binary code.
• Assembly language is a low-level
language that reflects machine code.
• High-level programming languages use
English-like commands for easier human
understanding.
• Fortran, Pascal, C++, and Python are
examples of high-level programming
languages.
Photo by Pexels
Compilers and Interpreters
• Compilers translate source code into
executable files directly run by the
operating system.
• Interpreters read and execute code
line-by-line or transform code into
bytecode.
• Languages like Pascal and BC have
built-in compilers in their development
environments.
• Languages like JavaScript, Perl, and
Python use interpreters to execute code.
Photo by Pexels
High-Level Programming Languages
• High-level programming languages
abstract from hardware implementation.
• They allow algorithms to be
independent of specific computer
hardware.
• Functional programming languages, like
Haskell and Lisp, focus on evaluating
function values.
• Object-oriented programming
languages, like Java and Python, use
objects and classes for code organization.
• Structured programming languages,
like Pascal and C, emphasize block
structures.
• Multi-paradigm languages combine
multiple programming paradigms.
• Most modern programming languages support
multiple paradigms.
Programming Language Classifications
• Programming languages can be classified based
on their semantic differences from machine code.
• Low-level languages are closer to machine code,
like assembly languages.
• High-level languages are more abstract
and easier for humans to understand.
• Languages can also be classified as compiled
or interpreted, although many languages blur
this distinction.
• Educational languages focus on
simplicity and ease of understanding,
while esoteric languages are created for
experimental or entertainment purposes.
• Pseudocode is a language used to describe
algorithms using natural-language-like syntax. Photo by Pexels
Syntax, Semantics, and Operators
• Syntax and grammar define the rules
for writing programs in a language.
• Semantics determine the meaning and
interpretation of language constructs.
• Syntax and semantics are checked
during program analysis.
• Operators are fundamental concepts in
programming languages and allow for data
manipulation.
• Operators can vary between languages.
Photo by Pexels
Variables, Functions, and Procedures
• Variables can be constants or changeable values
e.G principle =2000, Interest=0.5 ,time =3 years
Intrest= [rinciple*interest*time
• Functions represent processes that
compute a specific value based on input
arguments.
• Procedures
Example are sets of commands
area(l,w)
executed in sequence without returning a
value.
• High-level languages provide libraries
of standard functions.
• Objects and classes are key concepts in
object-oriented programming.
Photo by Pexels
Operators and Expressions
• Expressions combine variables,
constants, and operators to produce a
value.
• Expressions can be evaluated and
assigned to variables. E.g area
=1/2(a+b)*h
• Programming languages have standard
operators for arithmetic, logic,
comparison, and more.
• Operator precedence determines the
order of evaluation in complex
expressions.
Photo by Pexels
Conclusion
• Programming languages are
fundamental tools for writing computer
programs.
• They provide the means to translate
high-level algorithms into executable code.
• Understanding different programming
languages and their features is essential
for effective coding.
• Continuous learning and exploration of
new languages is crucial in the dynamic
field of programming.
Photo by Pexels