Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hmmm Assembly Language

Similar presentations


Presentation on theme: "Hmmm Assembly Language"— Presentation transcript:

1 Hmmm Assembly Language

2 A Quick review of CPU

3 RAM CU ripple8 05 addn r3, 4 1 7+4 4 strobe for next instruction PC
IR Reg3 1 The instruction Argument 1 Argument 2 the same Reg3 the register the constant 4 old value of Reg3 = 7 new value of Reg3 = 11 CU read enable RAM 8 data bits 8 address bits strobe for all IR bits D in Q out decode instruction strobe to finish instruction (to all bits) ripple8 7+4 8 output bits flip-flop strobe for next instruction Instruction Decoding Guide 2 3 5 05 addn r3, 4 Just explain the pieces. This is a SIMPLE model! Direct their attention to the bottom right table. It's a simple 4 instruction machine! More to come.... 00 = LOAD from memory 01 = STORE to memory 10 = add a constant to a reg. (addn) 11 = ADD a reg. to a reg.

4 Hmmm CPU RAM … … Harvey Mudd Miniature Machine read r1 1 mul r2,r1,r1
Von Neumann bottleneck central processing unit random access memory Program Counter read r1 Holds address of the next instruction 1 mul r2,r1,r1 Instruction Register Holds the current instruction 2 add r2,r2,r1 3 write r2 Stress Levels of abstraction! We want to think at a higher level. Assembly language! Hmmm the machine in text starting on page 76. r0 4 halt 16 registers, each 16 bits 5 r1 they can hold values from upto 32767 6 r2 255 r15 255 memory locations of 16 bits

5 Assembly Language add r2 r2 r2 sub r2 r1 r4 mul r7 r6 r2 div r1 r1 r2
register-level programming add r2 r2 r2 reg2 = reg2 + reg2 crazy, perhaps, but used ALL the time which is why it is written this way in python! sub r2 r1 r4 reg2 = reg1 - reg4 reg7 = reg6 * reg2 mul r7 r6 r2 div r1 r1 r2 reg1 = reg1 / reg2 INTEGER division - no remainders reg1 = 42 you can replace 42 with anything from -128 to 127 setn r1 42 I changed the div line from Brian's slide. Note first argument is the destination register. Consistency in design. Each of these instructions (and many more) get implemented for a particular processor and particular machine… . assembly code actual meaning reg1 = reg1 - 1 a shortcut addn r1 -1 read r1 write r1 read from keyboard and write to screen

6 the assembler a program that translates from human-readable assembly language into machine language (binary) setn r1,6 setn r2,7 mul r3,r1,r2 write r3 executable machine code assembly code We use hmmmAssembler.py to assemble We use hmmmSimulator.py to execute the machine code

7 two of the Intel instructions (SSE4, 2008)
Real Assembly Language Hmmm has a subset common to all real assembly languages. A few of the many basic processor instructions (Intel) two of the Intel instructions (SSE4, 2008)

8 What will this program output?
Suppose your input is 42 when line 0 is executed read r1 r1 General-purpose register r1 setn r2 9 1 r2 (x-9) 2 sub r3 r1 r2 General-purpose register r2 ex2.hmmm Point out that we needed to load 9 into a register because there are no divide instructions that allow a constant operand Python: ((x-9)//9)-1 (x-9) // 9 3 div r3 r3 r2 r3 ((x-9) // 9) - 1 addn r3 -1 4 General-purpose register r3 What is the equivalent expression in Python? 5 write r3 halt 6

9 Write an Hmmm program to compute
x + 3x – 4 HINT: Use the previous program as a model For your reference: ((x – 9) // 9) - 1 0 read r1 1 setn r2, 9 2 sub r3, r1, r2 3 div r3, r3, r2 4 addn r3, -1 write r3 halt 2 ex3.hmmm 0 read r # r1 = read x 1 mul r2, r1, r # r2 = x**2 2 loadn r3, # Need 3 in reg 3 mul r3, r3, r # r3 = 3*x 4 add r2, r2, r # r2 = r2 + r3 5 addn r2, # x**2 + 3*x - 4 6 write r # Output result 7 halt

10 x2 + 3x - 4 0 read r1 # r1 = read x 1 mul r2, r1, r1 # r2 = x**2 2 setn r3, 3 # Need 3 in reg 3 mul r3, r3, r1 # r3 = 3*x 4 add r2, r2, r3 # r2 = r2 + r3 5 addn r2,-4 # x**2 + 3*x write r2 # Output result 7 halt ex3.hmmm

11 Is this enough? What’s missing? read r1 mul r2 r1 r1 add r2 r2 r1
Why couldn't we implement Python using our Hmmm Assembly so far? read r1 mul r2 r1 r1 1 What’s missing? add r2 r2 r1 2 x**2 + x write r2 3 halt 4

12 Loops and ifs read r1 mul r2 r1 r1 add r2 r2 r1 write r2 jump! jumpn 1
We couldn't implement Python using our Hmmm Assembly Language so far... ! "straight-line code" It's too linear! read r1 mul r2 r1 r1 1 ex4.hmmm Computes x = r1**2 + r1 And, outputs x infinitely Discuss how "jump 1" works by placing a 1 in PC register. add r2 r2 r1 2 write r2 3 jump! jumpn 1 4

13 Hmmm, Let's jump ! setn r1 42 write r1 addn r1 1 jumpn 1 halt CPU RAM
central processing unit random access memory setn r1 42 write r1 1 r1 General-purpose register r1 addn r1 1 2 r2 Counts from 42 onward… Take a moment to demo this by typing it in, showing them how "easy" it is to assemble and run. Take a moment to show the –f option for the assembler so they don't need to keep retyping the file name! jumpn 1 3 General-purpose register r2 halt 4 What does this program do? What if we replace 1 with 2?

14 jumps jumpn 42 jeqzn r1 42 jgtzn r1 42 jltzn r1 42 jnezn r1 42
Unconditional jump jumpn 42 "jump to program line number 42" Conditional jumps jeqzn r1 42 IF r1 == 0 THEN jump to line number 42 jgtzn r1 42 IF r1 > 0 THEN jump to line number 42 jltzn r1 42 IF r1 < 0 THEN jump to line number 42 jnezn r1 42 IF r1 != 0 THEN jump to line number 42 Indirect jump jumpr r1 Jump to the line number stored in reg1!

15 jgtzn (A) -42 (B) -6 (C) -1 (D) 6 (E) 42 RAM CPU
screen RAM CPU central processing unit random access memory read r1 r1 1 jgtzn r1 7 2 setn r2 -1 General-purpose register r1 3 mul r1 r1 r2 r2 4 nop General-purpose register r2 ex5.hmmm This computes the absolute value of the input, so the output is 6 5 nop With an input of -6, what does this code write out? 6 nop 7 write r1 8 halt What function is this? (A) (B) (C) (D) (E) 42


Download ppt "Hmmm Assembly Language"

Similar presentations


Ads by Google