Hmmm Assembly Language A QUICK OVERVIEW OF CPU
Hmmm Assembly Language
A QUICK OVERVIEW OF CPU
Hmmm Harvey Mudd Miniature Machine CPU central processing unit Program Counter Holds address of the next instruction Instruction Register Holds the current instruction 0 r 1 r 2 r 15 … RAM Von Neumann bottleneck 16 registers, each 16 bits they can hold values from -32768 upto 32767 random access memory 0 read r 1 1 mul r 2, r 1 2 add r 2, r 1 3 write r 2 4 halt 5 6 … 255 memory locations of 16 bits
Fetch-Execution Cycle CPU execution repeats the Fetch-Execution Cycle, • Fetch a instruction from memory • Decode to determine what the instruction intends to do • Execution the instruction as specified during which the PC is incremented properly.
Assembly Language register-level programming add r 2 r 2 reg 2 = reg 2 + reg 2 sub r 2 r 1 r 4 reg 2 = reg 1 - reg 4 mul r 7 r 6 r 2 reg 7 = reg 6 * reg 2 div r 1 r 2 reg 1 = reg 1 / reg 2 crazy, perhaps, but used ALL the time which is why it is written this way in python! INTEGER division - no remainders setn r 1 42 reg 1 addn r 1 -1 assembly code read r 1 read from keyboard write r 1 and write to screen reg 1 = 42 you can replace 42 with anything from -128 to 127 = reg 1 - 1 a shortcut actual meaning Each of these instructions (and many more) get implemented for a particular processor and particular machine….
the assembler a program that translates from human-readable assembly language into machine language (binary) setn r 1, 6 setn r 2, 7 mul r 3, r 1, r 2 write r 3 assembly code We use hmmm. Assembler. py to assemble 0000 1000 0110 0000 0001 0010 0000 0001 0010 0000 executable machine code We use hmmm. Simulator. py to execute the machine code
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 (SSE 4, 2008)
What will this program output? (1) Suppose your input is 42 when line 0 is executed r 1 42 General-purpose register r 1 r 2 0 read r 1 1 setn r 2 9 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 General-purpose register r 3
What will this program output? (2) Suppose your input is 42 when line 1 is executed r 1 42 General-purpose register r 1 r 2 9 0 read r 1 1 setn r 2 9 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 General-purpose register r 3
What will this program output? (3) Suppose your input is 42 when line 2 is executed r 1 42 General-purpose register r 1 r 2 9 0 read r 1 1 setn r 2 9 (x-9) 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 33 General-purpose register r 3
What will this program output? (4) Suppose your input is 42 when line 3 is executed r 1 42 General-purpose register r 1 r 2 9 0 read r 1 1 setn r 2 9 (x-9) 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 3 General-purpose register r 3 (x-9) // 9
What will this program output? (5) Suppose your input is 42 when line 4 is executed r 1 42 General-purpose register r 1 r 2 9 0 read r 1 1 setn r 2 9 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 2 General-purpose register r 3
What will this program output? (6) Suppose your input is 42 when line 5 is executed r 1 42 General-purpose register r 1 r 2 9 0 read r 1 1 setn r 2 9 2 sub r 3 r 1 r 2 3 div r 3 r 2 4 addn r 3 -1 5 write r 3 6 halt General-purpose register r 2 r 3 2 General-purpose register r 3 What is the equivalent of Python?
Write an Hmmm program to compute 2 x + 3 x – 4 HINT: Use the previous program as a model For your reference: ((x – 9) // 9) - 1 0 1 2 3 4 5 6 read r 1 setn r 2, 9 sub r 3, r 1, r 2 div r 3, r 2 addn r 3, -1 write r 3 halt
x 2 + 3 x - 4 0 1 2 3 4 5 6 7 read mul setn mul addn write halt r 1 r 2, r 1 r 3, 3 r 3, r 1 r 2, r 3 r 2, -4 r 2 # # # # r 1 = read x r 2 = x**2 Need 3 in reg r 3 = 3*x r 2 = r 2 + r 3 x**2 + 3*x - 4 Output result
Is this enough? Why couldn't we implement Python using our Hmmm Assembly so far? What’s missing? 0 read r 1 1 mul r 2 r 1 2 add r 2 r 1 3 write r 2 4 halt
Loops and ifs We couldn't implement Python using our Hmmm Assembly Language so far. . . ! "straight-line code" It's too linear! jump! 0 read r 1 1 mul r 2 r 1 2 add r 2 r 1 3 write r 2 4 jumpn 1
Hmmm, Let's jump ! CPU central processing unit r 1 RAM random access memory 0 setn r 1 42 1 write r 1 2 addn r 1 1 3 jumpn 1 4 halt General-purpose register r 1 r 2 General-purpose register r 2 What does this program do? What if we replace 1 with 2?
jumps Unconditional jumpn 42 "jump to program line number 42" Conditional jumps jeqzn jgtzn jltzn jnezn r 1 r 1 42 42 IF r 1 == 0 THEN jump to line number 42 IF r 1 > 0 THEN jump to line number 42 IF r 1 < 0 THEN jump to line number 42 IF r 1 != 0 THEN jump to line number 42 Indirect jumpr r 1 Jump to the line number stored in reg 1!
jgtzn Poll. Ev. com/xiannongmeng 758 text: xiannongmeng 758 to 37607 CPU central processing unit RAM random access memory 0 read r 1 1 jgtzn r 1 7 2 setn r 2 -1 3 mul r 1 r 2 4 nop 5 nop 6 nop 7 write r 1 8 halt r 1 General-purpose register r 1 r 2 General-purpose register r 2 With an input of -6, what does this code write out? What function is this? (A) -42 screen (B) -6 (C) -1 (D) 6 (E) 42
- Slides: 20