04 Cpu
04 Cpu
• operates by performing
sequences of very simple
operations very fast
John von Neumann
1903-1957
A simple "toy" computer (a "paper" design)
Memory (RAM)
A program to print a number
• questions:
– how would you extend this to adding three numbers?
– how would you extend this to adding 1000 numbers?
– how would you extend this to adding as many numbers as there were?
Looping and testing and branching
• we need a way to re-use instructions
• add a new instruction to CPU's repertoire:
GOTO take next instruction from a specified RAM location
instead of just using next location
• this lets us repeat a sequence of instructions indefinitely
• each CPU architecture has its own instruction format and one
(or more) assemblers
A simulator for the toy computer (toysim.html)
• simulator (a program) reads a program written for the toy computer
• simulates what the toy computer would do
• toy machine's instruction repertoire:
get read a number from the keyboard into accumulator
print print contents of accumulator
load Val load accumulator with Val (which is unchanged)
store Lab store contents of accumulator into location labeled Lab
add Val add Val to accumulator
sub Val subtract Val from accumulator
goto Lab go to instruction labeled Lab
ifpos Lab go to instruction labeled Lab if accumulator positive (>= 0)
ifzero Lab go to instruction labeled Lab if accumulator is zero
stop stop execution
if Val is a name like Sum, it refers to a memory location with that label;
if Val is a number like 17, that value is used literally
Summary of how CPU operates
• each memory location holds an instruction or a data value (or part)
• instructions are encoded numerically (so they look the same as data)
e.g., GET = 1, PRINT = 2, LOAD = 3, STORE = 4, …