MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Similar documents
MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

MIPS%Assembly% E155%

Lecture 5: Procedure Calls

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS64 Week 5 Lecture 1. Kyle Dewey

Assembler. Lecture 8 CS301

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Chapter 2A Instructions: Language of the Computer

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Five classic components

Instruction Set Architecture

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CS 61c: Great Ideas in Computer Architecture

Midterm. CS64 Spring Midterm Exam

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

MIPS Functions and Instruction Formats

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

ECE260: Fundamentals of Computer Engineering

Five classic components

Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

ECE260: Fundamentals of Computer Engineering

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Lecture 4: MIPS Instruction Set

CS61C Machine Structures. Lecture 13 - MIPS Instruction Representation I. 9/26/2007 John Wawrzynek. www-inst.eecs.berkeley.

Unsigned Binary Integers

Unsigned Binary Integers

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

8*4 + 4 = 36 each int is 4 bytes

Course Administration

Lecture 6 Decision + Shift + I/O

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CS61C : Machine Structures

CENG3420 Lecture 03 Review

Format. 10 multiple choice 8 points each. 1 short answer 20 points. Same basic principals as the midterm

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

MIPS Instruction Set Architecture (2)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

ECE 15B Computer Organization Spring 2010

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

MIPS Assembly Programming

Levels of Programming. Registers

CSSE 232 Computer Architecture I. I/O and Addressing

ECE331: Hardware Organization and Design

Common Problems on Homework

CS222: MIPS Instruction Set

MIPS Assembly Language. Today s Lecture

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

And in Review. Register Conventions (2/4) saved. Register Conventions (1/4) Register Conventions (4/4) Register Conventions (3/4) volatile

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

ECE232: Hardware Organization and Design

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

syscall takes a service ID (number) in $v0 Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard

Chapter 2: Instructions:

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MIPS Assembly (Functions)

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MIPS Coding Continued

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

CS61C : Machine Structures

COMPUTER ORGANIZATION AND DESIGN

Computer Organization MIPS ISA

Problem maximum score 1 35pts 2 22pts 3 23pts 4 15pts Total 95pts

LAB B Translating Code to Assembly

Chapter 2. Instruction Set Architecture (ISA)

Computer Architecture. MIPS Instruction Set Architecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Lecture 9: Disassembly

Shift and Rotate Instructions

Transcription:

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8 Ziad Matni Dept. of Computer Science, UCSB

CS 64, Spring 18, Midterm#1 Exam Average = 84.3%, Median = 87% 19 14 12 6 8 7 7 2 5/1/18 Matni, CS64, Sp18 2

Reviewing Your Midterm#1 Exam If your FAMILY name is: BAAS to LIANG (inclusive) Then go see T.A.: Fatih Bakir Wednesdays 5-7 PM LIN to ZHOU (inclusive) Bay-Yuan Hsu Fridays 11 AM - 1 PM T.A. office hours are in PHELP 3525 Exams have to stay with T.As at ALL times No copying / no photos of exams allowed 5/1/18 Matni, CS64, Sp18 3

Lecture Outline Constructing Instructions in MIPS Functions in MIPS 5/1/18 Matni, CS64, Sp18 4

Last Week s Exercise Using your MIPS Reference Card, write the 32 bit instruction (using the R-Type format) for the following. Express your final answer in hexadecimal. add $t3, $t2, $s0 0x01505820 op (6b) 0 rs (5b) 10 rt (5b) 16 rd (5b) 11 shamt (5b) 0 funct (6b) 32 000000 0 1010 1 0000 0 1011 0 0000 10 0000 00000001010100000101100000100000 0x01505820 5/1/18 Matni, CS64, Sp18 5

5-Minute Pop Quiz!!! Write the machine code, in Hex, for this MIPS instruction: sub $t1, $t5, $t2 5/1/18 Matni, CS64, Sp18 6

5-Minute Pop Quiz!!! Write the machine code, in Hex, for this MIPS instruction: sub $t1, $t5, $t2 0x01AA4822 5/1/18 Matni, CS64, Sp18 7

Instruction Representation What if you wanted an instruction to add using an immediate value? load/save from/to memory? Why are these instructions unusable with an R-Type format? 5/1/18 Matni, CS64, Sp18 8

A Second Type of Format 32 bits are divided up into 4 fields (the I-Type format) op code 6 bits basic operation rs code 5 bits first register source operand rt code 5 bits second register source operand address/immediate code 16 bits constant or memory address Note: The I-Type format uses the address field to access ±2 15 addresses from whatever value is in the rs field op 6 b 31 26 rs 5 b 25 21 rt 5 b 20 16 address 16 b 15 0 5/1/18 Matni, CS64, Sp18 9

I-Type Format The I-Type address field is a signed number It can be positive or negative The addi instruction is an I-Type, example: addi $t0, $t1, 42 What is the largest, most positive, number you can put as an immediate? Ans: 2 15-1 5/1/18 Matni, CS64, Sp18 10

Instruction Representation in I-Type Example: op 8 Rs 16 addi $t0, $s0, 124 rt 8 address/immediate 124 op = 8 mean addi rs = 16 means $s0 rt = 8 means $t0 address/const = 124 is the immediate value (note 124 is in decimal) A full list of codes can be found in your MIPS Reference Card 5/1/18 Matni, CS64, Sp18 11

Exercises Using your MIPS Reference Card, write the 32 bit instruction (using the I-Type format and decimal numbers for all the fields) for the following: addi $t3, $t2, -42 andi $a0, $a3, 0x1 0x214BFFD6 0x30E40001 5/1/18 Matni, CS64, Sp18 12

srl vs sra Shift-Right Logic vs Arithmetic srl replaces the lost MSBs with 0s sra replaces the lost MSBs with either 0s (if number is +ve) or 1s (if number is ve) IMPLICATIONS: srl should NOT be used with negative numbers That is, unsigned use only sra should be used with signed numbers Can also be used with unsigned, but there s srl for that 5/1/18 Matni, CS64, Sp18 13

sra vs srl Exercise DEMO! srlsra.asm Is sra (-19) >> 2 the same as -(srl (19)) >> 2? 19 = 0000000000000000 0000000000010011-19 = 1111111111111111 1111111111101101 sra(-19) = 1111111111111111 1111111111111011 (01) = -5 -srl(19) = -(0000000000000000 0000000000000100) (11) = -4 goes away goes away 5/1/18 Matni, CS64, Sp18 14

sra vs srl Exercise DEMO! shiftdemo.asm srl replaces the lost MSBs with 0s sra replaces the lost MSBs with either 0s (if number is +ve) or 1s (if number is ve) EXAMPLE: addi $t0, $zero, 12 addi $t1, $zero, -12 srl $s0, $t0, 1 sra $s1, $t0, 1 srl $s0, $t1, 1 sra $s1, $t1, 1 5/1/18 Matni, CS64, Sp18 15

5/1/18 Matni, CS64, Sp18 16

Functions Up until this point, we have not discussed functions Why not? Memory management is a must for the call stack...though we can make some progress without it Think of recursion How many variables are we going to need ahead of time? What memory do we end up using in recursive functions? 5/1/18 Matni, CS64, Sp18 17

Implementing Functions What capabilities do we need for functions? 1. Ability to execute code elsewhere Branches and jumps 2. Way to pass arguments There a way (convention) to do that that we ll learn 3. Way to return values Registers 5/1/18 Matni, CS64, Sp18 18

Jumping to Code We have ways to jump to code (j instruction) But what about jumping back? We ll need a way to save where we were (so we can jump back) Q: What do need so that we can do this on MIPS? A: A way to store the program counter ($PC) (to tell us where the next instruction is so that we know where to return!) 5/1/18 Matni, CS64, Sp18 19

Calling Functions on MIPS Two crucial instructions: jal and jr One specialized register: $ra jal (jump-and-link) Simultaneously jump to an address, and store the location of the next instruction in register $ra jr (jump-register) Jump to the address stored in a register, often $ra 5/1/18 Matni, CS64, Sp18 20

Simple Call Example See program: simple_call.asm # Calls a function (test) which immediately returns.text test: # return to whoever made the call jr $ra main: # do stuff # then call the test function jal test exit: # exit li $v0, 10 syscall Note: SPIM always starts execution at the line labeled main 5/1/18 Matni, CS64, Sp18 21

Passing and Returning Values We want to be able to call arbitrary functions without knowing the implementation details So, we need to know our pre-/post-conditions Q: How might we achieve this in MIPS? A: We designate specific registers for arguments and return values 5/1/18 Matni, CS64, Sp18 22

Passing and Returning Values in MIPS Registers $a0 thru $a3 Argument registers, for passing function arguments Registers $v0 and $v1 Return registers, for passing return values What if we want to pass >4 args? There are ways around that but we won t discuss them in CS64! 5/1/18 Matni, CS64, Sp18 23

Passing and Returning Values in MIPS Demo: print_ints.asm Illustrates the use of a printing sub-routine (i.e. like a simple function) 5/1/18 Matni, CS64, Sp18 24

Passing and Returning Values in MIPS Demo: print_ints.asm Illustrates the use of a printing sub-routine (i.e. like a simple function) How would you write this function in C++? void print_ints(int a0, int a1) { cout << a0 << endl << a1 << endl; } 5/1/18 Matni, CS64, Sp18 25

Passing and Returning Values in MIPS Demo: add_ints.asm Illustrates the use of an adding sub-routine (i.e. like a simple function that returns a value) 5/1/18 Matni, CS64, Sp18 26

Passing and Returning Values in MIPS Demo: add_ints.asm Illustrates the use of an adding sub-routine (i.e. like a simple function that returns a value) How would you write this function in C++? int add_ints(int a0, int a1) { v0 = a0 + a1; return (v0); } 5/1/18 Matni, CS64, Sp18 27

YOUR TO-DOs Finish assignment/lab #4 Assignment due on FRIDAY 5/1/18 Matni, CS64, Sp18 28

5/1/18 Matni, CS64, Sp18 29