Post on 08-Jan-2016
description
CS 5 Today
logic gates
transistors / switches
bitwise functions
arithmetic
1-bit memory: flip-flops
Homework #6
registers
RAM
Hmmm
How does Python function ?4 Hmmm problems due Mon. 10/24
Python!
10.11.11Pomona
Day!
10-10-10 ?
Jotto !morning class's our guesses
938 words remaining 630 words remaining
2749 words remaining2047 words remaining
tower 1 trump 1
462 words remaining 285 words remaining
afternoon class's our guesses
784 words remaining 303 words remaining
1361 words remaining1844 words remaining
339 words remaining
206 words remaining
smile 2 bacon 1happy 1 shyly 2
hairy 2 wasps 2plane 1
credo 2gumbo 1
thumb 0front 1154 words remaining
deify 1bunch 1
lying 1wicks 1
sorry 3bagel 2knife 3
scale 4ridge 3
147 words remaining
15 words remaining
218 words remaining
68 words remaining
20 words remaining
117 words remaining
11 words remaining91 words remaining
13 words remaining
Options…aeein - ainee 1aefns - fanes 1aeinn - inane 1aeinv - naevi naive 2aeinx - xenia 1aeinz - azine 1aekns - kanes skean snake sneak 4eginp - genip 1eginy - eying 1
LETTERS ANAGRAMS
my word morning's
aarsy - rayas 1agrsy - grays 1ahrry - harry 1akrsy - kyars 1dlors - lords 1ehors - heros hoers horse shoer shore 5elors - lores loser orles roles sorel 5fhors - frosh 1glory - glory 1gosty - stogy 1ilors - loris roils 2kmosy - smoky 1mossy - mossy 1oosty - sooty toyos 2oppsy - soppy 1
adgrs - drags grads 2bbeir - bribe 1beijr - jiber 1beirr - brier 1dginy - dingy dying 2dilru - lurid 1eghit - eight 1egrsy - greys gyres 2eimmr - mimer 1eimrr - rimer 1eimrx - mirex mixer remix 3eiqru - quire 1gillr - grill 1
LETTERS ANAGRAMS
my word
acdls - clads scald 2acegs - cages 1aceis - saice 1acelp - place 1acens - acnes canes scena 3aclos - coals colas 2aclrs - carls 1adels - dales deals lades lased leads 5aelos - aloes 1aelrs - arles earls lares laser lears rales reals seral 8celsw - clews 1
afternoon's
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
programs stored hereinstructions executed here
Von Neumann bottleneck
A few, fast registers + arithmetic
Slow memory - no computation
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
the programthe processing
0
1
2
3
4
5
…6
Programs are stored in memory in
machine language(bits!)
Von Neumann bottleneck
0000 0001 0000 0001
1000 0010 0001 0001
0110 0010 0010 0001
0000 0010 0000 0010
0000 0000 0000 0000
the fetch - execute cycle
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
0000 0001 0000 0001
1000 0010 0001 0001
0110 0010 0010 0001
0000 0010 0000 0010
0000 0000 0000 0000
PC
IR
Reg3
1 10
1 0 0 1 1 1 0 0
The instruction Argument 1 Argument 2 the same Reg3the register the constant
1 1 10
0 1 11
4
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
decode instruction
strobe to finish instruction
(to all bits)
7+4
8 output bits
flip-flop
strobe for next instruction
Instruction Decoding Guide a Von Neumann machine
12
34
5
Reg3
10 = add a constant (addn)
How can circuits run
05: 10 011 100
? RippleAdd
add to reg3 4
PC 1 10
clock
00000
read enable
RAM
8 bit instruction
8 address bits
D in
Q out
strobe
flip-flop
strobe for next instruction
12
34
5
The program
counter knows
we're on line 05 .The clock keeps things ticking!
PC
IR
1 10
1 0 0 1 1 1 0 0
The instruction
10 = add a constant (addn)
Argument 1 Argument 2
the register the constant
4
clock
00000
read enable
RAMIR
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
flip-flop
strobe for next instruction
add to reg. 3 the number 4
12
34
5
Reg3
8 bit instruction
Line 05 is loaded
into the instruction
register. Its bits are10 011 100
add 4 to reg3
PC
Reg3
1 10
1 0 0 1 1 1 0 0
1 1 10
old value of Reg3 = 7
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
D in
Q out
strobe
decode instruction
7+4
4 output bits
flip-flop
strobe for next instruction
12
34
5
IR
The instruction
10 = add a constant (addn)
Argument 1 Argument 2
the register the constant
4
1 0 0 1 1 1 0 0
Reg3
r34
Doesn't this ruin add r3 r4 ?
Where do they go?
RippleAdd
add 4 to reg3
PC
Reg3
1 10
1 0 0 1 1 1 0 0
the same Reg3
1 1 10
0 1 11
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
D in
Q out
strobe
decode instruction
strobe to finish instruction
(to all bits)
RippleAdd
7+4
8 output bits
flip-flop
strobe for next instruction
12
34
5
IR
The instruction
10 = add a constant (addn)
Argument 1 Argument 2
the register the constant
4
1 0 0 1 1 1 0 0
Reg3
In circuits, if is done via AND gates...
add 4 to reg3
Jon
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
the programthe processing
0
1
2
3
4
5
…6
Programs are stored in memory in
machine language(bits!)
Von Neumann bottleneck
0000 0001 0000 0001
1000 0010 0001 0001
0110 0010 0010 0001
0000 0010 0000 0010
0000 0000 0000 0000
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
read r10
1
2
3
4
5
…6
halt
Assembly language is human-readable machine language
mul r2 r1 r1 add r2 r2 r1
write r2
Von Neumann bottleneck
the programthe processing
substituting words for bits
Human readable? I doubt it!
Running a program on the processor
halt
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
Our assembly language
halt
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
16 registers256 memory
locations
Demo
of friendly Hmmm assembly-language programming…
Assembly Language
read r1
div r1 r1 r1
add r2 r2 r2
register-level programming
reg2 = reg2 + reg2
which is why it is written this way in python!sub r2 r1 r4 reg2 = reg1 - reg4
reg7 = reg6 * reg2
reg1 = reg1 / reg1INTEGER division - no remainders
mul r7 r6 r2
Each of these instructions (and many more) get implemented for a particular processor and particular machine… .write r1
read from keyboard and write to screen
setn r1 42 you can replace 42 with anything from -128 to 127
addn r1 -1 a shortcut
crazy, perhaps, but used ALL the time
reg1 = 42
reg1 = reg1 - 1
What will this program output?
halt
setn r2 9
read r1
sub r3 r1 r2
div r3 r3 r2
central processing unit registers random access memory locations
0
1
2
3
4
r1 General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
r3 General-purpose register r3
addn r3 -1
write r35
6
Could you write a Hmmm program to compute
x + 3x – 4
?
2
Real Assembly Language
Hmmm has a subset common to all real assembly languages.
two of the latest intel instructions (SSE4, 2008)
A few of the many basic processor instructions (Intel)
Is this enough?
0
1
2
3
4
Why couldn't we implement Python using our Hmmm
Assembly so far?
halt
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
What’s missing?
For systems, a face-lift is to add an edge that
creates a cycle, not just an additional node.
NORSinput
output
feedback cycle
Q
Loops and ifs
It's too linear! "straight-line code"
jump!
We couldn't implement Python using our Hmmm Assembly Language so far... !
0
1
2
3
4 jumpn 1
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
Hmmm, Let's jump !
halt
write r1
setn r1 42
addn r1 1
jumpn 1
RAMCPU
0
1
2
3
4
r1 General-purpose register r1
r2 General-purpose register r2
What if we replace 1 with 2?
screen
random access memorycentral processing unit
jumps
Unconditional jump
Conditional jumps
jumpn 42
jeqz r1 42 jgtz r1 42 jltz r1 42 jnez r1 42
"jump to program line number 42"
IF r1 == 0 THEN jump to line number 42
IF r1 > 0 THEN jump to line number 42
IF r1 < 0 THEN jump to line number 42
IF r1 != 0 THEN jump to line number 42
Indirect jump
jump r1 Jump to the line number stored in reg1!
This IS making me jumpy!
Hmmm the complete reference
jgtz
RAMrandom access memory
read r10
1
2
3
4
5
6
7
8
jgtz r1 7
setn r2 -1
mul r1 r1 r2
nop
nop
nop
Gesundheit!
write r1
halt
CPUcentral processing unit
r1 General-purpose register r1
r2 General-purpose register r2
screen
With an input of -6, what does this code write out?
What function is this?
jgtz
RAMrandom access memory
read r10
1
2
3
4
5
6
7
8
jgtz r1 7
setn r2 -1
mul r1 r1 r2
nop
nop
nop
Gesundheit!
write r1
halt
CPUcentral processing unit
r1 General-purpose register r1
r2 General-purpose register r2
screen
With an input of -6, what does this code write out?
What function is this?
(A) -42 (B) -6 (C) -1 (D) 6 (E) 42
Quiz Feeling Jumpy?Name(s)
1 Follow this assembly-language program from top to bottom. Use r1 = 42 and r2 = 5.
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
Hint: Take in an input. Next, set up a “result” register (r2) starting with 1 in it. Then modify the “result” until it’s right!
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
read r1
read r2
sub r3 r2 r1
nop
jltz r3 7
write r1
jumpn 8
(1) What function does this program compute in general?
(2) How could you change this program so that, if the original two inputs were equal, it asked the user for new inputs?
Memory - RAM
0
Memory - RAM
Registers - CPU
1
2
3
4
5
6
7
8
9
10
r0
r1
r2
Registers - CPU
0
1
2
3
4
5
6
write r2
halt
7
8
r3
9
0
0r0
r1
r2
r3
r4
1
(1) What does this program compute in general?
(2) How could you change this program so that, if the original two inputs were equal, it asked the user for new inputs?
Follow this assembly-language program from top to bottom. Use r1 = 42 and r2 = 5.
read r1
read r2
sub r3 r2 r1
nop
jltz r3 7
write r1
jumpn 8
Memory - RAM
r0
Registers - CPU0
1
2
3
4
5
6
write r2
halt
7
8
9
0
r1
r2
r3
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
factorial
Plan?
r2r1let r1 be the input (and counter) let r2 become the output
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
0
Memory - RAM
1
2
3
4
5
6
7
8
9
10
r0
Registers - CPU
0
r1
r2
r3
factorial
read r1
setn r2 1
jeqz r1 9
mul r2 r2 r1
addn r1 -1
write r2
halt
jumpn 2
nop
nop
nop
Randohmmm Numbers…
how computers generate "random" numbers
See you there!
This week's lab:
Hmmm, Let's jump !
halt
write r1
setn r1 42
addn r1 1
jumpn 1
RAMCPU
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
What if we replace 1 with 2?
screen
random access memorycentral processing unit
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
factorial
plan
1
(1) What does this program compute in general?
(2) How could you change this program so that, if the original two inputs were equal, it asked the user for new inputs?
Follow this assembly-language program from top to bottom. Use r1 = 42 and r2 = 5.
read r1
read r2
sub r3 r2 r1
nop
jltz r3 7
write r1
jumpn 8
Memory - RAM
r0
Registers - CPU0
1
2
3
4
5
6
write r2
halt
7
8
9
0
r1
r2
r3
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
0
Memory - RAM
1
2
3
4
5
6
7
8
9
10
r0
Registers - CPU
0
r1
r2
r3
factorialNOTE: HANDWRITING REFLECTS OLD HMMM
Jotto ! morning class's guesses
alien 2 diner 1seven 0 ghost 0389 words remaining 363 words remaining
2320 words remaining2285 words remaining
leans 1 fluff 1213 words remaining 190 words remaining
afternoon class's guesses
faces 2 diner 1
mount 1 ghost 11061 words remaining 1010 words remaining
2320 words remaining2199 words remaining
shier 2 fluff 1467 words remaining 458 words remaining
choke 1, shake 1 badge 1
gypsy 0 wacky 0
132 words remaining 97 words remaining
48 left 27 words remaining
quipu 0 1? words remainingmagic 227 words remaining
squib 2 41 words remaininglearn 410 words remaining
Options…abiot - biota 1adiio - oidia 1adiop - podia 1adior - aroid radio 2adiou - audio 1adioz - azido diazo 2aiopt - patio 1aiort - ratio 1ghilt - light 1hilmu - hilum 1iiklm - kilim 1iklmy - milky 1
LETTERS ANAGRAMS
my word
morning's
cikl - click 1ciilv - civil 1ciily - icily 1cijuy - juicy 1cikqu - quick 1cilxy - cylix 1cmpru - crump 1crruy - curry 1cruvy - curvy 1fiyzz - fizzy 1iiklm - kilim 1iillv - villi 1ijkmu - mujik 1iklmy - milky 1iklxy - kylix 1illwy - willy 1ilmpy - imply 1ilppy - lippy 1impux - mixup 1ipquu - quipu 1jknuy - junky 1kmruy - murky 1knpuy - punky 1lrwyy - wryly 1mmruy - rummy 1mrruy - murry 1nnpuy - punny 1
aaenr - anear arena 2adenr - redan 1aeenr - ranee 1aeiln - alien aline anile elain liane 5aelmr - lamer realm 2aelrt - alert alter artel later ratel taler 6aelru - ureal 1aenrr - reran 1aenrv - raven 1aenrw - rewan 1
LETTERS ANAGRAMS
my word
bbels - blebs 1bbilo - bilbo 1beefs - beefs 1begmu - begum 1bells - bells 1belps - plebs 1belss - bless 1bettu - butte 1bhmru - rhumb 1bilmo - limbo 1biloo - oboli 1biltz - blitz 1bnoux - unbox 1borru - burro 1ddssu - sudds 1dmpsu - dumps 1dpssu - spuds 1eemsu - emeus 1ejpsu - jupes 1empsu - spume 1
emssu - muses 1epssu - puses spues supes 3eqtuu - tuque 1iilps - pilis 1ijlls - jills 1illms - mills 1illps - pills spill 2illss - sills 1illsv - vills 1ilmps - limps 1ilmss - slims 1ilpss - lisps slips 2imopu - opium 1itttu - tutti 1mprsu - rumps 1mrrsu - murrs 1nnssu - sunns 1npsuu - sunup 1prrsu - purrs 1prssu - spurs 1prsuu - usurp 1afternoon's
PC
IR
Reg3
1 10
1 0 0 1 1 1 0 0
The instruction Argument 1 Argument 2 the same Reg3the register the constant
1 1 10
0 1 11
4
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
decode instruction
strobe to finish instruction
(to all bits)
ripple8 7+4
8 output bits
flip-flop
strobe for next instruction
Instruction Decoding Guide a Von Neumann machine
12
34
5
Reg3
10 = add a constant to a reg. (AIM)
How does it all
work together?
PC 1 10
clock
00000
read enable
RAM
8 bit instruction
8 address bits
D in
Q out
strobe
flip-flop
strobe for next instruction
a Von Neumann machine
12
34
5
PC
IR
1 10
1 0 0 1 1 1 0 0
The instruction
10 = add a constant to a reg. (AIM)
Argument 1 Argument 2
the register the constant
4
clock
00000
read enable
RAMIR
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
flip-flop
strobe for next instruction
Instruction Decoding Guide a Von Neumann machine
12
34
5
Reg3
8 bit instruction
PC
Reg3
1 10
1 0 0 1 1 1 0 0
1 1 10
old value of Reg3 = 7
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
D in
Q out
strobe
decode instruction
7+4
flip-flop
strobe for next instruction
a Von Neumann machine
12
34
5
IR
The instruction
10 = add a constant to a reg. (AIM)
Argument 1 Argument 2
the register the constant
4
1 0 0 1 1 1 0 0
Instruction Decoding Guide
Reg3
Adder
Output bits
PC
Reg3
1 10
1 0 0 1 1 1 0 0
the same Reg3
1 1 10
0 1 11
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 bit instruction
8 address bits
strobe for all IR bits
D in
Q out
strobe
decode instruction
strobe to finish instruction
(to all bits)
7+4
flip-flop
strobe for next instruction
a Von Neumann machine
12
34
5
IR
The instruction
10 = add a constant to a reg. (AIM)
Argument 1 Argument 2
the register the constant
4
1 0 0 1 1 1 0 0
Instruction Decoding Guide
Reg3
Adder
Output bits
PC
IR
Reg3
1 10
1 0 0 1 1 1 0 0
The instruction
00 = LOAD from memory 01 = STORE to memory 10 = add a constant to a reg. (AIM) 11 = ADD a reg. to a reg.
Argument 1 Argument 2 the same Reg3the register the constant
1 1 10
0 1 11
4
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 data bits
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
decode instruction
strobe to finish instruction
(to all bits)
Adder 7+4
Output bits
flip-flop
strobe for next instruction
Instruction Decoding Guide a Von Neumann machine
12
34
5
Reg3
Why Assembly Language ?
It’s only the foolish who never climb Mt. Fuji -- or who climb it again.
Who writes most of the assembly language used?
Bah Hmmmbug!
Randohmmm Numbers…
debugging in Hmmm...
how computers generate "random" numbers
See you there!
This week's lab:
Assembly Language
halt
div r6 r7 r12
add r2 r1 r0
setn r0 42
register-level programming
sub r5 r4 r3
mul r11 r9 r8
Assembly Language
halt
div r6 r7 r12
add r2 r1 r0
setn r0 42
register-level programming
sub r5 r4 r3
mul r11 r9 r8
Machine Language
0011000000101010
0111001000010000
1000010101000011
1001101110011000
1010011001111100
0000000000000000
bit-level programming
Machine Language
0011000000101010
0111001000010000
1000010101000011
1001101110011000
1010011001111100
0000000000000000
bit-level programming
Problem #2
Figure out what a machine-language
program is doing and describe it in English.
Gallery
Random Access Memory
We can use data latches to create a 12 nG bit RAM !
3 data output bits
Inputs
3 data input bits
SimplifiedPrototype forAccessing Memory
Outputs
write enable line
read enable line
2 data address bits
3 bits stored at location 00 3 bits stored at location 01 3 bits stored at location 10 3 bits stored at location 11
12 bits of RAM
"on-chip"
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
STORE
5 into memory location #1
3 data input bits
write enable line
read enable line
IN: data address, in binary
0
1
Binary Decoder
two other memory lines and their flip-flops are not drawn 3 data output bits
2
3
OR
OR
OR
0. Make data input bits 101
4. How do the * AND gates help?
*
*
Ex CR
memory location
1. Give 01 to the decoder (the 1 goes on)
2. Make the "Write Enable" high
3. How does it all work?
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
strobe
D Q
LOAD
data from mem. location #1
3 data input bits
write enable line
read enable line
IN: data address, in binary
0
1
Binary Decoder
two other memory lines and their flip-flops are not drawn 3 data output bits
2
3
OR
OR
OR
0. Suppose 101 is in Location #1
3. Which gates ensure data from memory location #1 is read?
Ex Cr
4. Which gates ensure data from memory location #0 is not read?
memory location
1. Give 01 to the decoder (the 1 goes on)
2. Make the "Read Enable" high
callloadstore
store goes TO memory
r1
Hmmm RAMHmmm CPU
read r1
r15
0
1
2
3
4
5
…42
setn r15 42
store r1 r15
43…
store r1 r15 42aliens
addn r15 1
the "stack pointer"
stores contents of r1 into r15's LOCATION
(not r15 itself!)
.
.
load retrieves FROM memory
r1
Hmmm RAMHmmm CPU
.
r15
0
1
2
3
4
5
…
6
7
8
42
.
.
.
.
43…
42load r1 r15
aliens
.
.
addn r15 -1
load r1 r15loads data from r15's LOCATION (not r15)
into register r1the stack pointer
call
A function call in python:
def main(): r1 = input() result = factorial(r1) print result
def factorial( r1 ): # do factorial work return result
When this function is called… what will the processeor need
to remember ?!!
call
A function call in python:
def main(): r1 = input() result = factorial(r1) print result
def factorial( r1 ): # do factorial work return result
Hmmm's call operation:
call r14 4
puts NEXT line # into r14, then jumps to line 4
where, presumably, the function we want will start!
r14 is being used as a RETURN ADDRESS REGISTER
Homework #6
hw6pr1.py
hw6pr2.py
hw6pr3.py
hw6pr4.py
Ex Cr.
Countdown Lab!
Hmmm power!
Fibonacci1, 1, 2, 3, 5, 8, …
Recursive power
Recursive Fibonacci
start from the factorial example from last class
ditto, just a bit "more"
start from the recursive factorial example we'll look at today
ditto
problems
extra
RandoHmmm #s
Factorial as function call
Hmmm RAMHmmm CPU
read r10
1
2
3
4
5
6
7
8
9
call r14 4r1
r13
write r13
halt
setn r13 1
jeqz r1 9
mul r13 r13 r1
addn r1 -1
jumpn 5
jump r14
Input value: x
Final result - return value - in progress
loop
return
what if we wanted x + fac(x) ?
inputfunction call
r14return address register
the function!
output
destructive!
The problem
but potentially LOTS of function calls!
There is only ONE set of registers!
read r10
1
2
3
4
5
6
7
8
9
call r14 4r1
r13
write r13
halt
setn r13 1
jeqz r1 9
mul r13 r13 r1
addn r1 -1
jumpn 5
jump r14
Input value: x
Final result - return value - in progress
loop
return
what if we wanted x + fac(x) ?
inputfunction call
r14return address register
the function!
output
destructive!
The solution: the stack
A function call in python:
def main(): r1 = input() result = factorial(r1) print result
def factorial( r1 ): # do work return result
main's stack frame
the stack
r1 gets stored here for retrieval later…
in general, each function's "valuables" gets stored for future retrieval
Then, this function does not have to be careful about using registers… it can
pretend it has a new processor in which to work!
Recursion?def fac(N):
if N <= 1: return 1
else: return N * fac(N-1)
fac(5)
5 * fac(4)
4 * fac(3)
3 * fac(2)
2 * fac(1)
1
"The Stack"
Remembers all of the
individual calls to fac
Slide taken from Recursion, Day 1
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
Save valuable possessions!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
ret. value
3?
A Stack Frame represents one function call
It stores the input and return address!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
Another Stack Frame
A DIFFERENT input and return address!
THE STACK
growing downward
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
x, the input
ret. address
ret. value
1?
fac(1)
Yet another Stack Frame
Storing each function call's "private" info!
THE STACK
growing downward
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
x, the input
ret. address
ret. value
1?
x, the input
ret. address
ret. value
01
fac(1)
fac(0)
Aha!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
x, the input
ret. address
ret. value
1?
fac(1)1
Stack Frame gone!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
x, the input
ret. address
ret. value
11
fac(1)
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
2?
1
Another Stack Frame gone!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
fac(2)
ret. value
3?
x, the input
ret. address
ret. value
22
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
ret. value
3?
2
Yet another Stack Frame gone!
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
x, the input
ret. address
fac(3)
ret. value
36
Factorial via Recursion…RAMPython
x = input()y = fac(x)print y
def fac(x):
if x==0: return 1
else: REC = fac(x-1) return x*REC
6 The Stack is Empty.
prints out 6
Implementing functions
Reserve r15 as the stack pointer.
Reserve r14 as the return address.
Reserve r13 as the return value (result).
r15
r14
r13
Reserve some registers…
Store and load "function valuables" (data) using the stack
More detail…
(1) Before a function call,Store all valuable data to the stack
(2) Get r1, (r2), (r3), … ready as function "inputs."
When done: The result will be in r13.(3) Make the function call.
(0) Use r15 as the stack pointer.
setn r15 42
or some other large-enough
value
store r1 r15addn r15 1
repeat for all old values: r1, (r2), (r3) and the return address, r14
There may or may not be some lines of code
necessary to do this.
(4) After the function call,Load valuable data back from the stack
addn r15 -1load r1 r15
call r14 # line # of the function
for each item stored
(0) Use r14 as the return address.
(0) Use r13 as the return value (result).
x = input()
y = fac(x)
print y
def fac(x): if x == 0: return 1
else: REC = fac(x-1) return x*REC
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Python Hmmm
Base Case
Recursion
input
function call
output
Quiz: base case only!Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
The input is 0
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
always-0 register
recu
rsiv
e st
epb
ase
case
0
Recursive case…Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
always-0 register
recu
rsiv
e st
epb
ase
case
0
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
3
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
3
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
3
3Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
3
3
43
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Name(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
Are the first two stored values the same? How deep does the stack get? What are the possible values of r14?
recu
rsiv
e st
epb
ase
case
3
42
3
3
43
3
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
always-0 register
0
00 read r1 01 setn r15 4202 call r14 503 jumpn 2104 nop05 jnez r1 806 setn r13 107 jump r1408 store r1 r1509 addn r15 110 store r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 load r14 r1516 addn r15 -117 load r1 r1518 mul r13 r13 r119 jump r1420 nop21 write r1322 halt
Implementing functions
(1) Before the function call,
non-destructively!
Store all valuable data to the stack
(2) Get r1, (r2), (r3), … ready as function "inputs."
The result, if any, will be in r13.(3) Make the function call.
(0) Use r15 as the stack pointer. setn r15 42
or some other large-enough
value
store r1 r15addn r15 1
store the return address r14 and the inputs: r1, (r2), (r3)
There may or may not be some lines of code
necessary to do this.
(4) After the function call,Load valuable data back from the stack
addn r15 -1load r1 r15
call r14 #line # of the
function
for each item stored
Hmmm Notes and Example Code
r1,r2,…
r13
r14
r15
function inputs
return value
return address
stack pointer
Recursion and functions:
factorial
Loops and jumps: factorial
Please use these conventions for your registers -- so the graders know what
you're writing in your code!!
IMAGE NOT UPDATED
Why Assembly Language ?
It’s only the foolish who never climb Mt. Fuji -- or who climb it again.
Who writes most of the assembly language that is actually used?
the compiler
a program that translates from human-usable language into assembly language and machine langauge
x = 6y = 7z = x*yprint z
the code assembly or byte-code
executable machine code
loadn r1 6loadn r2 7mul r3 r1 r2write r3
0000 0001 0000 00011000 0010 0001 00010110 0010 0010 00010000 0010 0000 00100000 0000 0000 0000
Compiler
Examples
each processor has its own endearing idiosyncrasies...
Power PC
Core 2 Duo
x = 6y = 7z = x*yprint z
the code
the "compiler"
Functions (and recursion) took about 20 years to understand well computationally.
Don Gillies, compilerVon Neumann & friend
Inside gates: Transistors
1947: Bell Labs
seeking cheaper, smaller amplifiers for phone lines
team of scientists: Walter Brattain, William Shockley, and John Bardeen
1948: junction transistor
1956: Shockley semiconductor
1957: Fairchild semiconductor
much more robust, solid state
the "traitorous eight" - ICs
point contact transistor
onward to microprocessors
1968: IntelBob Noyce and Gordon Moore go off on their own again, leaving Fairchild to found Intel (Moore-Noyce didn't sound as good.)
1971: the first microprocessorthe Intel 4004 is created by the team of Federico Faggin, Ted Hoff and Stan Mazur.
the Intel 4004
The 8008, 8080, 8086, 80386, 80486, Pentium, P6, and now Intel Core Duo microproccessors are its direct descendants
The rest is history…
The base case for complex designs' recursive dependence.
Intel 4004
1971
108 KHz clock
4-bit processor
2300 transistors
10,000 nm wires
hand-designed!
Intel 4004
1971
108 KHz clock
4-bit processor
2300 transistors
10,000 nm wires
hand-designed!
2009
Intel Core i7 965
3.3 GHz clock
64-bit processor
731 million transistors
45 nm wires
computer-assisted design
I see these designers had excellent taste!
iPhone, geohot, and Von Neumann
George Hotz's iPhone
In red: one memory-access bit
After
Before
soldered to be +1.8v (1)
iPhone, geohot, and Von Neumann
George Hotz's iPhone
When starting up, the phone checks 4 locations in memory to see if its software is already there. Only if it sees the software is NOT there, does the phone use software from general-purpose memory to start. Otherwise, it loads its software from read-only memory.
There's not much you can do to change what is in those areas of read-only memory. Instead, the idea was to change one of the address lines to be high while it checked these four locations.
All of these locations are in a read/write (accessible) portion of the phone's memory -- so, they can be written to the "reset" signal. This reloads the phone's start-up software from read/write memory -- allowing arbitrary network access, not just AT&T.
The 4 32-bit memory locations it checks are
10100000000000000000000000110000101000000000000010100101101000001010000000000001010111000101100010100000000000010111001101110000
0xA0000030
0xA000A5A0
0xA0015C58
0xA0017370
hex binary
1
10100000000001000000000000110000101000000000010010100101101000001010000000000101010111000101100010100000000001010111001101110000
The memory locations it actually checks are thus
iPhone, geohot, and Von Neumann
George Hotz's iPhone
the trade
Have a relaxing fall break!
2006
Intel Core 2 Duo
3 GHz clock
64-bit processor
291 million transistors
65 nm wires
computer-assisted design
I see these designers had excellent taste!
Jotto
diner 2 diner 1
ghost 1 ghoul 1
Jessica's word my word
952 words remaining 1129 words remaining
2319 words remaining1916 words remaining
quack 0 brown 3
363 words remaining 56 words remaining
badge 1 wacky 0
190 words remaining 9 words remaining
errorxenonrhymemoxie…
possibilities
Jotto
diner 2 diner 1
ghost 1 ghoul 1
Jessica's word my word
952 words remaining 1129 words remaining
2319 words remaining1916 words remaining
quack 0 brown 3
363 words remaining 56 words remaining
badge 1 wacky 0
190 words remaining 9 words remaining
CS 5 Wed.
5 divides 0 evenly,
too!
memory extra credit!
random numbers
no stack!
The Collider, the Particle and a Theory About Fate
GOTOflier
Towers of Hanoi
This puzzle can get Hanoi'ing!
Python HmmmHmmmPython
x = input()y = fac(x)print y
def fac(x): """ recursive factorial! """ if x==0: return 1
else: REC = fac(x-1) return x*REC
00 read r1 01 loadn r15 4202 call r14 503 jump 2104 nop05 jnez r1 806 loadn r13 107 jumpi r1408 storei r1 r1509 addn r15 110 storei r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 loadi r14 r1516 addn r15 -117 loadi r1 r1518 mul r13 r13 r119 jumpi r1420 nop21 write r1322 halt
match each piece of Python code with the Hmmm assembly code that implements it.
what if…
x + fac(x) Saved by the stack
r0
Hmmm RAM
Hmmm CPU
read r10
loadn r15 42
r1
r13
storei r1 r15
addn r15 1
loadn r13 1
jeqz r1 15mul r13 r13 r1addn r1 -1jump 11jumpi r14
Input value: x
Final result - return value - in progress
0
inputstack pointer
r14location / line to return TO
STORE valuable data to stack
r15the "stack pointer"
call r14 10
addn r15 -1
loadi r1 r15
add r13 r13 r1
write r13
halt
1
2
10
11
12
13
14
15
42
43
3
4
5
6
7
8
9
call the function
LOAD valuable data from stack
do more stuff
output
the stack
factorial function
Towers of Hanoi
This puzzle can get Hanoi'ing!
Assembly Mnemonics
QuizName(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r2
r3
r4
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152r5
Program (low RAM)
the stack
0 read r0 1 loadn r2 62 loadn r3 03 loadn r4 84 loadn r5 425 jump r46 write r37 halt8 loadn r3 19 jzero r0 r210 addn r5 111 store r0 r512 addn r5 1 13 store r2 r514 addn r0 -115 loadn r2 1816 loadn r4 817 jump r418 load r2 r519 addn r5 -120 load r0 r521 addn r5 -1 22 mul r3 r0 r323 jump r2
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
The input is 3.
Input value: x
Return address
Result value
Next Function's Address
Stack Pointer
Aaaargh!
with labels
fact
ori
al f
un
ctio
n
Why 18? How low could we start the stack? How to call a different function? Stack == 'safe' No base case…? Why is r1 unused?
# r0 is x (first input)# r2 is the return address# r3 is the result (return value!)# r4 holds the function's address (destination)# r5 holds the STACK POINTER!0 read r0 1 loadn r2 62 loadn r3 03 loadn r4 84 loadn r5 425 jump r46 write r37 halt8 loadn r3 19 jzero r0 r210 addn r5 111 store r0 r512 addn r5 1 13 store r2 r514 addn r0 -115 loadn r2 1816 loadn r4 817 jump r418 load r2 r519 addn r5 -120 load r0 r521 addn r5 -1 22 mul r3 r0 r323 jump r2
fac
input, setup, print
input x
Recursive Factorial!
y = fac(x)print y
if x == 0: return 1else:
REC = fac(x-1)
return x*REC
setup for call to fac(x)
setup for call to fac(x-1)
unpack after call to fac(x-1)
REC is now in r3
1 is now in r3
Bits of the implementation…
From real to really real…
sD QAn 8-bit register:
sD Q
sD Q
sD Q
sD Q
sD Q
sD Q
sD Q
The PC register holds the memory address of the next instruction.Program Counter
The IR register holds the next instruction to be executed.Instruction Register
Data registers can be used as the programmer wishes.
But who is this hypothetical programmer?
A clock keeps all of the logic in sync andkeeps track of what needs to be done next… clock
PC
IR
Reg3
1 0 0 0 0 10 1 0 0 0 00 0 1 0 0 00 0 0 1 0 00 0 0 0 1 0
Reg3
12
34
5
PC
IR
Reg3
1 10
1 0 0 1 1 1 0 0
The instruction
10 = (addn)
Argument 1 Argument 2 the same Reg3the register the constant
1 1 10
0 1 11
4
old value of Reg3 = 7
new value of Reg3 = 11
clock
00000
read enable
RAMIR
8 data bits
8 address bits
strobe for all IR bits
1 0 0 1 1 1 0 0
D in
Q out
strobe
decode instruction
strobe to finish
instruction (to
all bits)ripple8 7+4
8 output bits
flip-flop
strobe for next instruction
Instruction Decoding Guidea Von Neumann machine
12
34
5
Reg3
cycles through 1 2 3 4 5
Friday: YouTube
Fiber Bundles, Animusic 2
Computers process numbers - not symbols. We measure our understanding (and control)
by the extent to which we can arithmetize an activity.
The Neo-classical view:
- Alan Perlis
No magic left ?
logic gates
transistors / switches
bitwise functions
arithmetic
1-bit memory: flip-flops
registers
RAM
IR: Instruction Register
PC: Program Counter
Hmmm
How does Python function ?
Python!
QuizName(s)
Write down what happens in the registers and memory (the stack) as this program runs…
r0
r1
r13
r14
42
Memory (high RAM)
CPU Registers
43
44
45
46
47
48
49
50
5152
r15
Program (low RAM)
"the stack"00 read r1 01 loadn r15 4202 call r14 503 jump 2104 nop05 jnez r1 806 loadn r13 107 jumpi r1408 storei r1 r1509 addn r15 110 storei r14 r1511 addn r15 112 addn r1 -113 call r14 514 addn r15 -115 loadi r14 r1516 addn r15 -117 loadi r1 r1518 mul r13 r13 r119 jumpi r1420 nop21 write r1322 halt
Yesterday I couldn't spell "Recursive Assembler," but now I r1.
The input is 3.
Input: x
result, return value
return address (line #)
Stack Pointer
with labels
fact
ori
al f
un
ctio
n
How low could we start the stack? Where is the BASE CASE? Where is the recursive call?
always-0 register
A Real Crash
Hmmm
CPU RAMcentral processing unit random access memory
Von Neumann bottleneck
read r10
1
2
3
4
5
…6
255
halt
255 memory locations of 16 bits
r0
…
16 registers,
each 16 bits
r15
they can hold values
from -32768 upto
32767
r1
r2
Program
CounterInstruction
RegisterHolds the current instruction
Holds address of the next instruction
The Harvey Mudd Miniature Machine
mul r2 r1 r1
add r2 r2 r1
write r20
Have a relaxing fall break!
Jotto ! morning class's guesses
alien 2 diner 1seven 0 ghost 0389 words remaining 363 words remaining
2320 words remaining2285 words remaining
leans 1 fluff 1213 words remaining 190 words remaining
afternoon class's guesses
faces 2 diner 1
mount 1 ghost 11061 words remaining 1010 words remaining
2320 words remaining2199 words remaining
shier 2 fluff 1467 words remaining 458 words remaining
choke 1, shake 1 badge 1
gypsy 0 wacky 0
132 words remaining 97 words remaining
48 left 27 words remaining
quipu 0 1? words remainingmagic 227 words remaining
squib 2 41 words remaininglearn 410 words remaining
Options…abiot - biota 1adiio - oidia 1adiop - podia 1adior - aroid radio 2adiou - audio 1adioz - azido diazo 2aiopt - patio 1aiort - ratio 1ghilt - light 1hilmu - hilum 1iiklm - kilim 1iklmy - milky 1
LETTERS ANAGRAMS
my word
morning's
cikl - click 1ciilv - civil 1ciily - icily 1cijuy - juicy 1cikqu - quick 1cilxy - cylix 1cmpru - crump 1crruy - curry 1cruvy - curvy 1fiyzz - fizzy 1iiklm - kilim 1iillv - villi 1ijkmu - mujik 1iklmy - milky 1iklxy - kylix 1illwy - willy 1ilmpy - imply 1ilppy - lippy 1impux - mixup 1ipquu - quipu 1jknuy - junky 1kmruy - murky 1knpuy - punky 1lrwyy - wryly 1mmruy - rummy 1mrruy - murry 1nnpuy - punny 1
aaenr - anear arena 2adenr - redan 1aeenr - ranee 1aeiln - alien aline anile elain liane 5aelmr - lamer realm 2aelrt - alert alter artel later ratel taler 6aelru - ureal 1aenrr - reran 1aenrv - raven 1aenrw - rewan 1
LETTERS ANAGRAMS
my word
bbels - blebs 1bbilo - bilbo 1beefs - beefs 1begmu - begum 1bells - bells 1belps - plebs 1belss - bless 1bettu - butte 1bhmru - rhumb 1bilmo - limbo 1biloo - oboli 1biltz - blitz 1bnoux - unbox 1borru - burro 1ddssu - sudds 1dmpsu - dumps 1dpssu - spuds 1eemsu - emeus 1ejpsu - jupes 1empsu - spume 1
emssu - muses 1epssu - puses spues supes 3eqtuu - tuque 1iilps - pilis 1ijlls - jills 1illms - mills 1illps - pills spill 2illss - sills 1illsv - vills 1ilmps - limps 1ilmss - slims 1ilpss - lisps slips 2imopu - opium 1itttu - tutti 1mprsu - rumps 1mrrsu - murrs 1nnssu - sunns 1npsuu - sunup 1prrsu - purrs 1prssu - spurs 1prsuu - usurp 1afternoon's
CS 5 Today
logic gates
transistors / switches
bitwise functions
arithmetic
1-bit memory: flip-flops
Homework #6
registers
RAM
IR: Instruction Register
PC: Program Counter
Hmmm
How does Python function ?4 Hmmm problems due Mon. 10/25
Python!
10-11-10 ?
Jon
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
programs stored hereinstructions executed here
Von Neumann bottleneck
A few, fast registers + arithmetic
Slow memory - no computation
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
the programthe processing
00001001111010000
1
2
3
4
5
…6
Programs are stored in memory in
machine language(bits!)
Von Neumann bottleneck
1111110100100001
0001011011111001
1010100111000010
0000000000000000
Von Neumann Architecture
CPU RAMcentral processing unit random access memory
read r10
1
2
3
4
5
…6
halt
Assembly language is human-readable machine language
mul r2 r1 r1
add r2 r2 r1
write r2
Von Neumann bottleneck
the programthe processing
substituting words for bits
Human readable? I doubt it!
the fetch - execute cycle
halt
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
the fetch - execute cycle
halt
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
CPU RAMVon Neumann bottleneck
16 registers256 memory
locations
Assembly Language
read r1
div r1 r1 r1
add r2 r2 r2
register-level programming
reg2 = reg2 + reg2
which is why it is written this way in python!sub r2 r1 r4 reg2 = reg1 - reg4
reg7 = reg6 * reg2
reg1 = reg1 / reg1INTEGER division - no remainders
mul r7 r6 r2
Each of these instructions (and many more) get implemented for a particular processor and particular machine… .write r1
read from keyboard and write to screen
loadn r1 42 you can replace 42 with anything from -128 to 127
addn r1 -1 a shortcut
crazy, perhaps, but used ALL the time
reg1 = 42
reg1 = reg1 - 1
halt
loadn r2 9
read r1
sub r3 r1 r2
div r3 r3 r2
central processing unit registers random access memory locations
0
1
2
3
4
r1
Program
Counter
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
Answers
r3 General-purpose register r3
addn r3 -1
write r35
6
(A) -1 (B) 0 (C) 1 (D) 2 (E) 3
Real Assembly Language
Hmmm has a subset common to all real assembly languages.
two of the latest intel instructions (SSE4, 2008)
A few of the many basic processor instructions (Intel)
For systems, a face-lift is to add an edge that
creates a cycle, not just an additional node.
NORSinput
output
feedback cycle
Q
Loops and ifs
It's too linear! "straight-line code"
jump!
We couldn't implement Python using our Hmmm Assembly Language so far... !
0
1
2
3
4 jump 1
mul r2 r1 r1
read r1
add r2 r2 r1
write r2
Hmmm, Let's jump !
halt
write r1
loadn r1 42
addn r1 1
jump 1
RAMCPU
0
1
2
3
4
r1
Program
Counter
Instruction Register
Holds the current instruction
Holds address of the next instruction
General-purpose register r1
r2 General-purpose register r2
What if we replace 1 with 2?
screen
random access memorycentral processing unit
jumps
Unconditional jump
Conditional jumps
jump 42
jeqz r1 # jgtz r1 # jltz r1 # jnez r1 #
replaces the PC (program counter) with 42."jump to program line number 42"
IF r1 == 0 THEN jump to line number #
IF r1 > 0 THEN jump to the location in #
IF r1 < 0 THEN jump to the location in #
IF r1 != 0 THEN jump to the location in #
Indirect jump
jumpi r1 Jump to the line # stored in reg1!
This IS making me jumpy!
jgtz
RAMrandom access memory
read r10
1
2
3
4
5
6
7
8
jgtz r1 7
loadn r2 -1
mul r1 r1 r2
nop
nop
nopWith an input of 6, what does this code write out?
Gesundheit!
write r1
halt
CPUcentral processing unit
r1 General-purpose register r1
r2 General-purpose register r2
screen
(A) -1 (B) 1 (C) 6 (D) 7 (E) 42
1
(1) What does this program compute in general?
(2) How could you change this program so that, if the original two inputs were equal, it asked the user for new inputs?
Follow this assembly-language program from top to bottom. Use r1 = 42 and r2 = 5.
read r1
read r2
sub r3 r2 r1
nop
jltz r3 7
write r1
jump 8
Memory - RAM
r0
Registers - CPU0
1
2
3
4
5
6
write r2
halt
7
8
9
0
r1
r2
r3
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
factorial
plan
Write an assembly-language program that reads one integer as keyboard input. Then, the program should
2
compute the factorial of that input and write it out. You may assume without checking that the input will be a positive integer.
0
Memory - RAM
1
2
3
4
5
6
7
8
9
10
r0
Registers - CPU
0
r1
r2
r3
factorial
Randohmmm Numbers…how computers generate "random"
numbers
See you there!
This week's lab: