Information Processing 2 - Liverpool Hope University

Download Report

Transcript Information Processing 2 - Liverpool Hope University

Information Processing 10
At the Processor Level
Objectives for Weeks 10 & 11
After studying this week’s work, you should:
have begun to understand some of the
internal workings of the CPU
have understood the connections between
the CPU and the CPU Simulator
have understood the instruction set for the
CPU Simulator
be able to program the CPU Simulator to
perform simple tasks
The CPU Components
The CPU
Central Processing Unit
Lies at the heart of the computer
Fastest element, millions of operations
per second
Highly complex, but built to simple
overall design
conforms to Von Neumann’s principles
The Von Neumann Principles
Stored Program Concept

memory hold both programs and data
Memory addressed linearly

single numeric address for each memory cell
Independent Memory Addresses

location numbers are not affected by the data
contained at that location
Instructions Executed Sequentially

Instructions given carry on, one after the other
unless an instruction causes a ‘jump’ to occur.
“Simple” Diagram of a
Microprocessor
Overview of elements
ALU (Arithmetic/Logical Unit)
I/O Interface
CU (Control Unit)
ALU
Memory
Control
Unit
Program
Counter
Memory
I/O
Interface
The Components in brief
RAM
Memory
ALU
(calculator)
Command
Unit
Program
Counter
24
35
Input
58
Output
The Components in Brief
RAM Memory:
Addressable memory
cells 00,01,02 etc
Each contains a single
item, which can be:



an instruction
a reference to another
memory location
a piece of data.
The contents of memory
is called a program
The Program
RAM
Memory
00
IN
01 STO
02
03
20
IN
04 ADD
05 20
06 OUT
07 HLT
The program is stored in
consecutive RAM Memory
locations
The program will be executed
step-by-step in a logical manner.
The Components in Brief
The ALU (Calculator)
A simple calculator
It can only add,
subtract or compare
This is the ONLY
place where
calculations can be
done.
The Components in Brief
The Program Counter
Counts upwards from 00,
01, 02 etc.
Points to where the next
instruction is to come from
(e.g. 23 means the next
instruction will come from
memory cell 23)
Increments by 1 after
every instruction
Can be altered
The Components in Brief
Input & Output Cells
Contents show:


Last Number
Input
Last Number
Output
The Components in Brief
The Command Unit
It controls and performs
all tasks
Fetches and interprets
instructions from RAM
Each instruction is
specified by an
operation code:
e.g. STO 21 means
“store the value in
memory location 21”
The CPU Simulator
The CPU Simulator
Load this now
from the website.
The CPU Simulator
This part of the CPU is the
memory, where we store
program. This is a set of
instructions which will be
carried out later.
Type the program into your
CPU Simulator.
Make sure you do it exactly.
That means spelling, upper
case lettering ... everything
must be the same.
We will begin
with a small
demonstration
The CPU Simulator - Program
Now click on
Start
This data
entry box
appears.
Enter the
number
23 Now click
on OK
The CPU Simulator - Program
A second
data entry
box appears.
This time
enter the
number
35
Now click
on OK
The CPU Simulator - Program
An output
box
appears
with the
answer
58
Click on
OK
Finally
Click on
OK
What Happened?
The program:
Asked for an input
 we typed in 23
Asked for another input
 We typed in 35
Told us the output
 This was 58
What Happened?
The program is just a
simple routine which
inputs two numbers,
adds them together
and tells you the
answer.
The program:
Asked for an input
 we typed in 23
Asked for another input
 We typed in 35
Told us the output
 This was 58
In this
case
23 + 35 =
58
The Final State of the CPU
An Explanation
First
number
entered
Final
answer
here
Second
number
entered
Challenge
Now use the CPU
Simulation to add
together 17 and 49
First
number
entered
Final
answer
here
Second
number
entered
How does it work?
The next few slides tell you what the
elements of the CPU simulator are, and
what they do.
You will be introduced to important new
terms and ideas.
These will be related to the component
parts of a real CPU.
The CPU Simulator -elements
On-board RAM
On-Board RAM
High-Speed memory chip (normally
SDRAM) which contains the program
which will be processed by the CPU
Memory cells in the simulator will be
identified by the two-digit address
codes: 00, 01, 02,… 37, 38.
The CPU Simulator -elements
The CU
The Control Unit
Manages and controls the processing of
instructions
moves data around the processor
Responsible for incrementing the
program counter (this counter tells the
CPU which instruction is next to be
processed)
The CPU Simulator -elements
The ALU
The Arithmetic/Logical Unit
In a real CPU this carries out all

Arithmetical Calculations
 integer and floating point
 binary and decimal arithmetic

Logical Calculations
 Not, And, or etc.
 Boolean Logic
In the simulator, the ALU can only add
and subtract decimal numbers.
The CPU Simulator -elements
The Interface
Unit
The Interface Unit
Inputs external data and instructions
into the CPU for processing
Outputs the results of processing to
external hardware components
Contains two single registers


Input Register – showing the last value
input into the CPU from outside
Output Register – showing the last value
output from the CPU to the outside
The CPU Simulator -elements
Other Single
Registers
Registers used in
Performing Calculations
Accumulator


This register holds the data used for
calculations and the results of calculations
Currently = 58 (final result)
Arithmetic/Logic Unit Buffer


This holds the partial results of calculations
prior to being copied back to the
Accumulator
Also Currently = 58
Registers used in carrying
out instructions
Program Counter


This stores the memory location where the
next instruction is to be fetched from.
Currently = 8 (final location used was 7)
Instruction Register


This register holds the instruction currently
being executed by the CPU
Currently = HLT (final instruction in cell 7)
Single Registers –
Memory Access Information
Memory Address Register


This stores the address of a RAM memory location
being accessed.
Currently = 7 (last memory cell to be accessed)
Memory Read Buffer


This holds the contents of a RAM memory location
that have been read.
Currently = HLT (contents of cell 7 …last cell)
Memory Write Buffer


This holds the information that is being written to a
RAM Memory location.
Currently this is 23 (note that this data has been
written to memory location 20)
The Current Status
Programming the Simulator
The Program
The program written in the RAM is a
sequence of instructions, memory location
references and data.
These program instructions will consist of


either of a single mnemonic instruction, for
example HLT (“stop”)
or a two-part mnemonic instruction, for example
STO 15 (“ store the number in memory cell 15”)
The mnemonic shorthand instructions are
called Operation Codes
The Operation Codes
Op Code
Action
IN
Allows user to input a number into the Accumulator
OUT
Outputs the number currently in the Accumulator
STO
Stores the number currently in the Accumulator in the
specified location
Load the number stored in the specified location into the
Accumulator.
Adds the number the specified location to the number
currently in the Accumulator.
Subtracts the number the specified location from the number
currently in the Accumulator.
Halt
LDA
ADD
SUB
HLT
The CPU Simulator
Program Code
IN, OUT and HLT
When these commands are fetched
from the memory location, they are
executed immediately
They do one single task
They do not change or affect any
memory location
The CPU Simulator
Program Code
LDA, STO, ADD, SUB
With these commands, we also need to read
the next memory location
This makes up a single instruction, e.g.
Mnemonic
instruction
(op code)
LDA 25
RAM
Memory
address
We read this as “Load the number in
memory location 25 into the Accumulator”
Our First Program
RAM
00
01
02
03
04
05
Code
IN
STO
20
IN
ADD
20
06
07
OUT
HLT
Description
This is the program, and
the description explains
what the program was
doing.
However, In order to
understand it, we need
to see it working step-bystep.
Input Data
Store…
…the Input Data at RAM address 20
Input Data
Add…
… the contents of RAM address 20 to
whatever is currently in the accumulator
Output the Answer
Stop the Program
The Program Step-by-Step
RAM
Memory
00
Command
Unit
00
Program
Counter
00
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
00
Input
00
Output
Step 00
The Number 23
is input into the
CPU.
IN
RAM
Memory
23
Command
It also Unit
appears
here automatically
in the accumulator
display.
00
Program
Counter
00
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
Input
00
Output
Steps 01 & 02 STO 20
RAM
Memory
23
Command
Unit
The number 23 is
copied from the
Accumulator to
RAM Cell 20
00
Program
Counter
02
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
23
Input
00
Output
Step 03
The Number 35
is input into the
CPU.
IN
RAM
Memory
35
Command
Unit
The number
automatically
appears in the
accumulator
00
Program
Counter
03
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
35
Input
00
Output
Steps 04 & 05 ADD 20
RAM
Memory
58
00
23+ 35= 58
Command
Unit
The contents of RAM
cell 20 (the number
23) are added to the
accumulator
Program
Counter
05
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
35
Input
00
Output
Step 06
OUT
RAM
Memory
58
Command
Unit
The contents of
the accumulator
are sent to the
output box
00
Program
Counter
06
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
35
Input
58
Output
Step 07
The CPU Halts
Execution
HLT
RAM
Memory
58
Command
Unit
00
Program
Counter
07
IN
01 STO
18
02
03
19
20
20
IN
04 ADD
05 20
21
22
06 OUT
07 HLT
23
23
35
Input
58
Output
The Fetch-Execute Cycle
The process which carries out a single
instruction is called the instruction cycle.
This instruction cycle is similar for all
instructions, and can be broken down into
two parts:


The fetch cycle, where the instructions are
fetched from memory locations
The execute cycle, where the instructions are
interpreted and executed.
The Fetch-Execute Cycle
The Fetch Cycle




Read the program counter
read the instruction in that memory
location
Increment the program counter
interpret the instruction
The Execute Cycle

Carry out the task
Fetch-Execute
Cycle
Start
Fetch the Instruction from the
location indicated by the
program counter
Control Unit Decodes
the Instruction
Fetch the Operand
Yes
Increment the Program
counter
Does
Instruction
require an
Operand?
No
Execute the Instruction
Op Code Instructions
Storing and Loading
LDA xx




Find RAM address xx
Read the two-digit number there
Enter it into the accumulator register
(Lose any previous data in the accumulator)
STO xx



Load
Store
Read the accumulator register
Enter it into RAM address xx
(lose any previous data in address xx)
Op Code Instructions
Add and Subtract
ADD xx



Find RAM address xx
Read the number there
Add that number to what is in the Accumulator
SUB xx



Add
Subtract
Find RAM address xx
Read the number there
Subtract that number from what is in the
Accumulator
Op Code Instructions
Input and Output
IN
Input





Display an input box
Read the number entered into the box
Place in the ‘Last Value Input’ box
Enter this number into the Accumulator
(lose all previous data in the Accumulator)
OUT Output



Read the number in the Accumulator
Put up an output box
Enter the Number in the ’Last Value Output’ box.
Op Code Instructions:
Halt
HLT


Halt
Stop Working
Ignore all further commands
Programming Activities (1)
Task A
What does this program
do?
First of all type the
program in to the CPU
Simulator and run it.
Then work out exactly
what is happening in each
line of the program.
RAM Code
00
IN
01
STO
02
20
03
ADD
04
20
05
OUT
06
HLT
Task B
What does this program
do? Work out what is
happening on each line of
the program.
Predict the output if:



the first input is 12
the second input is 25
the third input is 23
Type in the program into
the simulator to see if you
were right.
RAM Code
00
IN
01
STO
02
20
03
IN
04
STO
05
21
06
IN
07
ADD
08
20
09
ADD
10
21
11
OUT
12
HLT
Task C
What does this program
do?
1. Predict the output for:


first input = 27
second input = 13
2. Predict the output for:


first input = 50
second input = 12
Type the program in to the
CPU Simulator and run it
to see if you were right.
RAM
00
01
02
03
04
05
06
07
08
09
10
11
12
13
Code
IN
STO
20
IN
STO
21
LDA
20
SUB
21
SUB
21
OUT
HLT
Some More Tasks
Write Short Programs to:
1. Input two numbers and subtract the
first number from the second.
2. Input two numbers and subtract the
second number from the first.
3. Input three numbers, add the first and
third, and subtract the second.
Simple Branching Commands
Op Code Instructions:
Branch
BR xx


Jump
Go to the counter
Change the counter to read xx
(Branch unconditionally)
Note: after a Branch command, the
processing starts immediately at that
new number
A Mystery Program
What does this
program do?
RAM Code
00
ADD
01
05
02
OUT
03
BR
04
00
05
01
WARNING…
This program severely
disrupts the CPU simulator.
In order to exit, You will need
to press CTRL, ALT, DELETE
all together, then select End
Task.
Programs with Loops
How does the “Count” program work?
Box
00
01
02
03
04
05
Code
ADD
05
OUT
BR
00
01
Description
Adds data from RAM cell 05…
…to the contents of the accumulator
Outputs the current accumulator contents
Branch unconditionally…
…to RAM Cell 00
Data used by the program
Infinite Loops
The “Count” program
will run forever
It is a non-terminating
program, and is stuck in
an infinite loop
To get out of this we
need some different op
codes (see next week’s
exciting episode)
Even More Tasks
4. Write a program that counts up in tens.
5. Write a program that counts down in ones
starting at 99
WARNING…
These programs too will
contain infinite loops and
disrupt the CPU simulator.
In order to exit, You will need
to press CTRL, ALT, DELETE
all together, then select End
Task.
Some Harder Tasks
6. Write a program that counts upwards in
multiples specified by an input

i.e. if the input is 5, the output will be:
5, 10, 15, 20 . . . etc.
7. Write a program which counts down in
multiples from a given number, both of which
are specified by inputs.

i.e. if the input is 5 and 900 the output will be :
900, 895, 890 . . . etc.)
Challenge Problem
Devise a program which
outputs the “triangle
numbers”

1,
3,
6,
10, etc.
Alternatively, these are:




1
1+2 =
1+2+3 =
1+2+3+4 =
1
3
6
10
Branching with Decisions
More Branching Commands
BRZ xx



Read the display in the accumulator
If it is Zero change program counter to xx
(that becomes the next line to be executed)
BRN xx



Branch on Zero
Branch on Negative (or Zero)
Read the display in the accumulator
If it is Negative change program counter to xx
(that becomes the next line to be executed)
CPU Simulator
The full instruction set
ADD xx
SUB xx
STO xx
LDA xx
BR xx
BRZ xx
BRN xx
IN
OUT
HLT
Add the value in xx to accumulator
Subtract value in xx from accumulator
Store accumulator value in xx
Load value from xx into accumulator
Branch Unconditional (i.e. Jump) to xx
Branch if zero to xx
Branch if negative to xx
Input a value
Output a value
Halt
Example of a Program with
Branching on Zero
Countdown from 99 to 1, then stop
Cell Code
Description
00
LDA
Load from …
01
10
… cell 10
02
OUT
Output
03
SUB
Subtract …
04
11
… cell 11 from the accumulator
05
BRZ
Test the accumulator; if zero, …
06
09
…branch to cell 09
07
BR
Branch to…
08
02
…cell 02
09
HLT
Stop Processing
10
99
Data: Starting Value
11
01
Data: Counting down in 1’s
Example of a Program with
Branching on Negative
Find the difference between two input values
Cell
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
Code
IN
STO
20
IN
STO
21
SUB
20
BRN
12
BR
16
LDA
20
SUB
21
OUT
HLT
Description
Inputs a value
Store in …
…cell 20
Input a value
Store in …
…cell 21
Subtract …
…cell 20
Test: if Negative, branch to…
…cell 12
Branch to…
… cell 16
Load …
…cell 20
Subtract…
…cell 21
Output
Halt
Counting Programs
1. Input a number, and count down in ones
from it to one, then stop.
2. Input any number, and count down in tens
until the last positive (or zero) number is
reached, then stop.
3. Count up in ones and stop at 10
4. Write a program which totals a column of
figures. The first input specifies how many
inputs will follow. The total of these inputs
will then be outputted.
Sorting Programs
5. Input two numbers then print out the
larger one.
6. Input a series of numbers ending in
00. Print out the largest of these.
7. Input three numbers and print them
out: smallest, middle, largest.
Multiplication and Division
8. Write a program which Input two
numbers, and multiplies one by the
other
9. Write a program which inputs two
numbers and divides the first number
by the second number, then outputs
the answer, followed by the remainder.
Programming Structures
Programming Structures
Programming
languages often
have “common”
ways of carrying out
tasks
Some of these are:



while... do loop
repeat.. until loop
for... next loop
While…do
Loop
A condition is
checked before the
start of the loop.
On the basis of that
decision,


either the loop is
executed
or it is skipped.
While
Do
Repeat… Until
A Loop is entered and
executed at least once.
At the end of the loop,
a condition is checked.
On the basis of that
decision,


either the loop is reexecuted
Or it is exited.
Loop
Repeat
Until
For… next LOOP
A loop variable has a
start value and a
target value.
The loop is entered,
and repeated a
specified number of
times until the loop
variable reaches its
target value
Enter
Loop
repeated
a set
number
of times
Exit
Summary
The Simulator is a simplified version of the
CPU.
The Op Codes, memory cells and counter all
have counterparts within a real CPU.
Although the simulator is a simple device, it is
flexible, adaptive and has the capability to
perform highly complex tasks.