Functions in Hmmm Assembly

Slides:



Advertisements
Similar presentations
Instruction Set Design
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
Chapter 6 In introduction to System Software and Virtual Machine ***Assembly Language.
Some thoughts: If it is too good to be true, it isn’t. Success is temporary. It is hard work to make it simple. Knowing you did it right is enough reward.
CS 536 Spring Code generation I Lecture 20.
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Translating high level language into machine code
Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers.
Chap 4 & 5 LC-3 Computer LC-3 Instructions Chap 4 Homework – due Monday October 27 Chap 5 Homework – due Wednesday October 29 Project 2 Designs (Working.
Software Development and Software Loading in Embedded Systems.
1 CS Programming Languages Random Access Machines Jeremy R. Johnson.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Lecture 4: MIPS Instruction Set
Computer Science 101 Computer Systems Organization ALU, Control Unit, Instruction Set.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
More on Pipelining 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
CS61C L20 Datapath © UC Regents 1 Microprocessor James Tan Adapted from D. Patterson’s CS61C Copyright 2000.
More on Pipelining 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Writing Functions in Assembly
Speed up on cycle time Stalls – Optimizing compilers for pipelining
Machine and Assembly Language
Assembly Language Programming of 8085
CHAPTER 6: The Little Man Computer
Assembly Language Assembly Language
COMP2121: Microprocessors and Interfacing
Addendum to Chap 2 and Chap 3
CS5 Stylin' !.
Writing Functions in Assembly
Functions and Procedures
Assembly Programming using MIPS R3000 CPU
Chapter 5 The LC-3.
The CS 5 Times Penguin/Pig Gang Fight Brings Violence to Claremont
About Instructions Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter PHY 201 (Blum)
Hmmm Assembly Language
Instructions - Type and Format
Sports: HMC CS Professor to coach 2020 U. S
Instruction set architectures
CSCE Fall 2013 Prof. Jennifer L. Welch.
Unit IV Code Generation
Computer Science 210 Computer Organization
MIPS Instruction Encoding
ECE232: Hardware Organization and Design
MIPS Instruction Encoding
Computer Architecture and the Fetch-Execute Cycle
MIPS History MIPS is a computer family
von Neumann Architecture CPU
Figure 8.1 of course package
Functions in Hmmm Assembly
Instruction set architectures
MIPS History MIPS is a computer family
CSCE Fall 2012 Prof. Jennifer L. Welch.
3.
The branch instruction
Assembly Programming using MIPS R3000 CPU
Program Execution.
MIPS History MIPS is a computer family
MIPS assembly.
Some Assembly (Part 2) set.html.
CS501 Advanced Computer Architecture
Hmmm Assembly Language
CPSC 171 Introduction to Computer Science
Functions in Hmmm Assembly
Introduction to Pointers
Presentation transcript:

Functions in Hmmm Assembly

Functions in Python vs. assembly r1 = int(input()) r13 = f(r1) print(r13) def f(r1): r13 = r1*(r1-1) return r13 0 read r1 1 calln r14, 4 2 write r13 3 halt 4 copy r13, r1 5 addn r13, -1 6 mul r13,r1,r13 7 jumpr r14 Write a NEW FUNCTION that returns 1 if the input is > 0 and 2 if the input is <= 0

Why Functions? Function is just a block of computation, no real magic. We can use “jumpn” to accomplish the same goal. # computes n*(n-1) without function 0 read r1 jumpn 4 write r13 halt copy r13, r1 addn r13, -1 mul r13,r1,r13 jumpn 2 This program does exactly the same as the function before without function (“calln”). We “hard-coded” the return address “jumpn 2.” But, what if another place in the program needs this part of the computation??? “jumpn 2” will lead to a wrong place! “jumpr r14” (thus function) will be needed!

storer stores TO memory storer rX rY # stores the content of rX into memory address held in rY Hmmm RAM Hmmm CPU setn r1 42 r0 1 setn r15 70 42 stores r1 into r15's MEMORY LOCATION (not into r15 itself!) r1 2 storer r1 r15 3 write r0 70 r15 4 write r1 They have it in the handout. Have them step through the code. points to a location in memory 5 halt storer r1 r15 . 70 4 4

loadr loads FROM memory loadr rX rY # load value into rX from memory address held in rY Hmmm CPU Hmmm RAM r0 nop 1 setn r15 70 r1 loads data into r1 from r15's MEMORY LOCATION (not r15 itself) 2 loadr r1 r15 3 write r0 70 r15 4 write r1 points to a location in memory 5 halt . loadr r1 r15 70 42 5 5

A function example 0 read r1 # Get the "x" for our function 1 setn r15, 70 # Set the stack pointer, (i.e., # load address of stack into r15) 2 storer r1, r15 # Store r1, since f overwrites it 3 calln r14, 7 # Call our function f(x) # Set r14 to be 4, next PC 4 loadr r1, r15 # Load r1 back in place write r13 # Print result halt # Stop the program 7 addn r1, 1 # Compute f(x) = x + 1 8 copy r13,r1 # Save result into r13 9 jumpr r14 # Finished function, jump back # Try 18_fun_example.hmmm with the input of 129, or 1 0000 0001 6

Are there any difference between instructions and values (numbers)? From computers’ point of view, the memory has separate dedicated area for data and instructions. So the computer knows which piece is data, which piece is instruction. But human beings can’t tell data from instructions just from its form. The program on the previous pages are compiled into machine form in red. 0 : 0000 0001 0000 0001 # 0 read r1, assuming input 257 1 : 0001 1111 0100 0110 # 1 setn r15, 70 2 : 0100 0001 1111 0001 # 2 storer r1, r15 3 : 1011 1110 0000 0111 # 3 calln r14, 7 4 : 0100 0001 1111 0000 # 4 loadr r1, r15 5 : 0000 1101 0000 0010 # 5 write r13 6: 0000 0000 0000 0000 # 6 halt ... 70: 0000 0001 0000 0001 # 70: integer 257, same as Line 0!

Jumps in Hmmm Unconditional jump Conditional jumps Indirect jump jumpn n # jump to line n (set PC to n) Conditional jumps jeqzn rx n # if reg x == 0, jump to line n jnezn rx n # if reg x != 0, jump to line n jltzn rx n # if reg x < 0, jump to line n jgtzn # if reg x > 0, jump to line n Indirect jump jumpr rx # jump to the value in reg x 8

Selection in Hmmm Assembly

Jumping for if statements # Python x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) # Hmmm # x in r1 0 read r1 10

Jumping for if statements # x in r1 0 read r1 # jump when ___ __ 0 we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 11

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 12

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later 13

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ # if body 4 addn r1 1 we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) figure out where later x > 2 aka x-2 > 0 14

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ # if body 4 addn r1 1 # jump to avoid else body 5 jumpn __ we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later 15

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ # if body 4 addn r1 1 # jump to avoid else body 5 jumpn __ # else body 6 setn r1 5 we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later 16

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ # if body 4 addn r1 1 # jump to avoid else body 5 jumpn __ # else body 6 setn r1 5 # after the if 7 write r1 8 halt we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later 17

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 __ # if body 4 addn r1 1 # jump to avoid else body 5 jumpn __ # else body 6 setn r1 5 # after the if 7 write r1 8 halt we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later.. 6 figure out where later.. 7 18

Jumping for if statements # x in r1 0 read r1 # jump when x-2 > 0 1 copy r2 r1 2 addn r2 -2 3 jgtzn r2 6 # if body 4 addn r1 1 # jump to avoid else body 5 jumpn 7 # else body 6 setn r1 5 # after the if 7 write r1 8 halt we can only jump using 0... x = int(input()) if x <= 2: x = x + 1 else: x = 5 print(x) we can only test < and > Jump when not (x <= 2) x > 2 aka x-2 > 0 figure out where later.. 6 figure out where later.. 7 DONE! 19

Try It: Write Hmmm for this code x = int(input()) if x == 2: x = 3 else: x = x + 2 print(x) 20

Try It: Write Hmmm for this code x = int(input()) if x == 2: x = 3 else: x = x + 2 print(x) My solution # jump1.hmmm 0 read r1 # x is in r1 1 copy r2 r1 2 addn r2 -2 3 jeqzn r2 6 4 addn r1 2 5 jumpn 7 6 setn r1 3 7 write r1 8 halt 21

Try It: Write Hmmm for this code x = int(input()) y = int(input()) if x > y: x = 3 else: x = y + 2 print(x) 22

Try It: Write Hmmm for this code x = int(input()) y = int(input()) if x > y: x = 3 else: x = y + 2 print(x) My solution # jump2.hmmm 0 read r1 # x is in r1 1 read r4 # y is in r4 2 sub r2 r1 r4 # test = x – y 3 jgtzn r2 7 # jump to 'if' branch 4 addn r4 2 # 'else' branch 5 copy r1 r4 6 jumpn 8 7 setn r1 3 # 'if' branch 8 write r1 9 halt 23