Top Banner
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.11 Pomon a Day!
148

CS 5 Today

Jan 08, 2016

Download

Documents

galena

CS 5 Today. Homework #6. 4 Hmmm problems due Mon. 10/24. How does Python function ?. Python!. Hmmm. RAM. registers. 1-bit memory: flip-flops. Pomona Day!. arithmetic. 10.11.11. bitwise functions. logic gates. transistors / switches. 10-10-10 ?. Jotto !. our guesses. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 5 Today

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!

Page 2: CS 5 Today

10-10-10 ?

Page 3: CS 5 Today

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

Page 4: CS 5 Today

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

Page 5: CS 5 Today

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

Page 6: CS 5 Today

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

Page 7: CS 5 Today

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

Page 8: CS 5 Today

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

Page 9: CS 5 Today

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!

Page 10: CS 5 Today

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

Page 11: CS 5 Today

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

Page 12: CS 5 Today

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

Page 13: CS 5 Today

Jon

Page 14: CS 5 Today

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

Page 15: CS 5 Today

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!

Page 16: CS 5 Today

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

Page 17: CS 5 Today

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

Page 18: CS 5 Today

Demo

of friendly Hmmm assembly-language programming…

Page 19: CS 5 Today

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

Page 20: CS 5 Today

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

Page 21: CS 5 Today

Could you write a Hmmm program to compute

x + 3x – 4

?

2

Page 22: CS 5 Today

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)

Page 23: CS 5 Today

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?

Page 24: CS 5 Today

For systems, a face-lift is to add an edge that

creates a cycle, not just an additional node.

NORSinput

output

feedback cycle

Q

Page 25: CS 5 Today

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

Page 26: CS 5 Today

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

Page 27: CS 5 Today

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!

Page 28: CS 5 Today

Hmmm the complete reference

Page 29: CS 5 Today

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?

Page 30: CS 5 Today

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

Page 31: CS 5 Today

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

Page 32: CS 5 Today

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

Page 33: CS 5 Today

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

Page 34: CS 5 Today

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

Page 35: CS 5 Today

Randohmmm Numbers…

how computers generate "random" numbers

See you there!

This week's lab:

Page 36: CS 5 Today

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

Page 37: CS 5 Today

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

Page 38: CS 5 Today

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

Page 39: CS 5 Today

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

Page 40: CS 5 Today

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

Page 41: CS 5 Today

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

Page 42: CS 5 Today

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?

Page 43: CS 5 Today

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

Page 44: CS 5 Today

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

Page 45: CS 5 Today

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

Page 46: CS 5 Today

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

Page 47: CS 5 Today

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

Page 48: CS 5 Today

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?

Page 49: CS 5 Today

Bah Hmmmbug!

Randohmmm Numbers…

debugging in Hmmm...

how computers generate "random" numbers

See you there!

This week's lab:

Page 50: CS 5 Today

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

Page 51: CS 5 Today

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

Page 52: CS 5 Today

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.

Page 53: CS 5 Today

Gallery

Page 54: CS 5 Today

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"

Page 55: CS 5 Today

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?

Page 56: CS 5 Today

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

Page 57: CS 5 Today

callloadstore

Page 58: CS 5 Today

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

.

.

Page 59: CS 5 Today

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

Page 60: CS 5 Today

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 ?!!

Page 61: CS 5 Today

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

Page 62: CS 5 Today

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

Page 63: CS 5 Today

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!

Page 64: CS 5 Today

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!

Page 65: CS 5 Today

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!

Page 66: CS 5 Today

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

Page 67: CS 5 Today

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!

Page 68: CS 5 Today

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!

Page 69: CS 5 Today

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

Page 70: CS 5 Today

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

Page 71: CS 5 Today

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!

Page 72: CS 5 Today

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!

Page 73: CS 5 Today

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)

Page 74: CS 5 Today

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!

Page 75: CS 5 Today

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

Page 76: CS 5 Today

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!

Page 77: CS 5 Today

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

Page 78: CS 5 Today

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

Page 79: CS 5 Today

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

Page 80: CS 5 Today

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

Page 81: CS 5 Today

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

Page 82: CS 5 Today

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

Page 83: CS 5 Today

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

Page 84: CS 5 Today

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

Page 85: CS 5 Today

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

Page 86: CS 5 Today

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

Page 87: CS 5 Today

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

Page 88: CS 5 Today

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

Page 89: CS 5 Today

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

Page 90: CS 5 Today

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

Page 91: CS 5 Today

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

Page 92: CS 5 Today

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

Page 93: CS 5 Today

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?

Page 94: CS 5 Today

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

Page 95: CS 5 Today

Examples

each processor has its own endearing idiosyncrasies...

Power PC

Core 2 Duo

x = 6y = 7z = x*yprint z

the code

Page 96: CS 5 Today

the "compiler"

Functions (and recursion) took about 20 years to understand well computationally.

Don Gillies, compilerVon Neumann & friend

Page 97: CS 5 Today

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

Page 98: CS 5 Today

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!

Page 99: CS 5 Today

Intel 4004

1971

108 KHz clock

4-bit processor

2300 transistors

10,000 nm wires

hand-designed!

Page 100: CS 5 Today

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!

Page 101: CS 5 Today

iPhone, geohot, and Von Neumann

George Hotz's iPhone

In red: one memory-access bit

After

Before

soldered to be +1.8v (1)

Page 102: CS 5 Today

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

Page 103: CS 5 Today

iPhone, geohot, and Von Neumann

George Hotz's iPhone

the trade

Page 104: CS 5 Today

Have a relaxing fall break!

Page 105: CS 5 Today

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!

Page 106: CS 5 Today

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

Page 107: CS 5 Today

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

Page 108: CS 5 Today

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

Page 109: CS 5 Today

Towers of Hanoi

This puzzle can get Hanoi'ing!

Page 110: CS 5 Today

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…

Page 111: CS 5 Today

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

Page 112: CS 5 Today
Page 113: CS 5 Today

Towers of Hanoi

This puzzle can get Hanoi'ing!

Page 114: CS 5 Today

Assembly Mnemonics

Page 115: CS 5 Today

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?

Page 116: CS 5 Today

# 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

Page 117: CS 5 Today

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

Page 118: CS 5 Today

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

Page 119: CS 5 Today

Friday: YouTube

Fiber Bundles, Animusic 2

Page 120: CS 5 Today

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

Page 121: CS 5 Today

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!

Page 122: CS 5 Today

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

Page 123: CS 5 Today

A Real Crash

Page 124: CS 5 Today

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

Page 125: CS 5 Today

Have a relaxing fall break!

Page 126: CS 5 Today

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

Page 127: CS 5 Today

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

Page 128: CS 5 Today

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!

Page 129: CS 5 Today

10-11-10 ?

Page 130: CS 5 Today

Jon

Page 131: CS 5 Today

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

Page 132: CS 5 Today

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

Page 133: CS 5 Today

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!

Page 134: CS 5 Today

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

Page 135: CS 5 Today

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

Page 136: CS 5 Today

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

Page 137: CS 5 Today

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

Page 138: CS 5 Today

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)

Page 139: CS 5 Today

For systems, a face-lift is to add an edge that

creates a cycle, not just an additional node.

NORSinput

output

feedback cycle

Q

Page 140: CS 5 Today

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

Page 141: CS 5 Today

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

Page 142: CS 5 Today

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!

Page 143: CS 5 Today

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

Page 144: CS 5 Today

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

Page 145: CS 5 Today

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

Page 146: CS 5 Today

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

Page 147: CS 5 Today

Randohmmm Numbers…how computers generate "random"

numbers

See you there!

This week's lab:

Page 148: CS 5 Today