Common Problems on Homework

Similar documents
MIPS function continued

CDA3100 Midterm Exam, Summer 2013

MIPS Coding Continued

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

ECE331: Hardware Organization and Design

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Lecture 7: Examples, MARS, Arithmetic

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

Lecture 6: Assembly Programs

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

Instruction Set Architectures (4)

Lecture 5. Announcements: Today: Finish up functions in MIPS

Functions in MIPS. Functions in MIPS 1

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Lecture 5: Procedure Calls

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

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

Today. Putting it all together

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

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

CS 61c: Great Ideas in Computer Architecture

MIPS Functions and the Runtime Stack

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

MIPS Assembly (Functions)

ECE232: Hardware Organization and Design

CSE Lecture In Class Example Handout

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Lecture 5: Procedure Calls

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Compiling Techniques

Chapter 2A Instructions: Language of the Computer

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Thomas Polzer Institut für Technische Informatik

MIPS Assembly (FuncDons)

CS64 Week 5 Lecture 1. Kyle Dewey

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

SPIM Instruction Set

SPIM Procedure Calls

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

COMP MIPS instructions 2 Feb. 8, f = g + h i;

Computer Systems and Networks

CS 316: Procedure Calls/Pipelining

CENG3420 Lecture 03 Review

Lec 10: Assembler. Announcements

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

ECE 30 Introduction to Computer Engineering

COMP2611: Computer Organization MIPS function and recursion

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

ECE260: Fundamentals of Computer Engineering

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

Compiling Code, Procedures and Stacks

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

We will study the MIPS assembly language as an exemplar of the concept.

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

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

Storage in Programs. largest. address. address

Implementing Procedure Calls

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

QtSPIM and MARS : MIPS Simulators

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

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

Shift and Rotate Instructions

Course Administration

NAME: 1. (2) What is a transistor?

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

MIPS Procedure Calls. Lecture 6 CS301

COMPUTER ORGANIZATION AND DESIGN

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Review of Activation Frames. FP of caller Y X Return value A B C

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

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Solutions for Chapter 2 Exercises

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

MIPS%Assembly% E155%

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

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

Lecture 6 Decision + Shift + I/O

Functions and Procedures

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Introduction to MIPS Processor

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CA Compiler Construction

MIPS Assembly Language Programming

Architecture II. Computer Systems Laboratory Sungkyunkwan University

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

MIPS Assembly Language Programming

MIPS Functions and Instruction Formats

Transcription:

MIPS Functions

Common Problems on Homework 1.3: Convert -3000 ten to binary in 8bit, 16bit, and 32bit Even though it overflows with 8bits, there is plenty of room with 16 and 32 bit.

Common Problems on Homework 2.3: Convert 01111111111111111111111110111111 two to decimal It says that it is in two s complement but that is just a standard to distinguish positive numbers from negative. You don t have to do two s complement conversion. That s used to see what the decimal equivalent is or to convert an unsigned number to a signed one

Common Problems on Homework 3.3: Convert -0.3 ten to a floating-point binary number In single precision, you would get 0xBE999999 and not 0xBE99999A because when you run out of hardware, instead of rounding, you truncate the number.

In Class Exercise 2 Write the MIPS assembly code to do insertion sort (shown by the following C code segment). You may use any MIPS instructions that you ve learned inside or outside of class. int main() { int i, j, v; int A[10] = {6, 3, 7, 2, 0, 9, 1, 8, 4, 5}; for (i = 1; i < 10; ++i) { v = A[i]; for (j = i - 1; j >= 0 && A[j] >= v; --j) { A[j+1] = A[j]; } A[j+ 1] = v; } return 0; }

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Set up program Initialize variables Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: Set up outer loop addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: addi $s3, $s2, -1 # j = i - 1 Set up Inner Loop Loop2: addi $s3, $s3, -1 # --j bge $s3, $0, Loop2 # Loop if j >= 0 addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: sll $t0, $s2, 2 # Convert i to memory access add $t0, $t0, $s0 # Add offset i to base A lw $s4, 0($t0) # v = A[i] addi $s3, $s2, -1 # j = i - 1 Load A[i] into v Loop2: addi $s3, $s3, -1 # --j bge $s3, $0, Loop2 # Loop if j >= 0 addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: sll $t0, $s2, 2 # Convert i to memory access add $t0, $t0, $s0 # Add offset i to base A lw $s4, 0($t0) # v = A[i] addi $s3, $s2, -1 # j = i - 1 sll $t1, $s3, 2 # Convert j to memory access add $t1, $t1, $s0 # Add offset j to base A Loop2: lw $t3, 0($t1) # load A[j] Load A[j] addi $s3, $s3, -1 # --j addi $t1, $t1, -4 # Keep memory access consistent with j bge $s3, $0, Loop2 # Loop if j >= 0 addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: sll $t0, $s2, 2 # Convert i to memory access add $t0, $t0, $s0 # Add offset i to base A lw $s4, 0($t0) # v = A[i] addi $s3, $s2, -1 # j = i - 1 sll $t1, $s3, 2 # Convert j to memory access add $t1, $t1, $s0 # Add offset j to base A Loop2: lw $t3, 0($t1) # load A[j] blt $t3, $s4, Break # Continue looping if A[j] >= v Add second condition to inner loop Break: addi $s3, $s3, -1 # --j addi $t1, $t1, -4 # Keep memory access consistent with j bge $s3, $0, Loop2 # Loop if j >= 0 addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit

In Class Exercise 2 Solution.data A:.word 6, 3, 7, 2, 0, 9, 1, 8, 4, 5.text.globl main main: la $s0, A # Array A li $s1, 10 # Length of A li $s2, 1 # i li $s3, 0 # j li $s4, 0 # v li $t0, 0 # Address of A[i] li $t1, 0 # Address of A[j] li $t2, 0 # Value of A[i] li $t3, 0 # Value of A[j] Loop1: sll $t0, $s2, 2 # Convert i to memory access add $t0, $t0, $s0 # Add offset i to base A lw $s4, 0($t0) # v = A[i] addi $s3, $s2, -1 # j = i - 1 sll $t1, $s3, 2 # Convert j to memory access add $t1, $t1, $s0 # Add offset j to base A Loop2: lw $t3, 0($t1) # load A[j] blt $t3, $s4, Break # Continue looping if A[j] >= v sw $t3, 4($t1) # A[j+1] = A[j] addi $s3, $s3, -1 # --j addi $t1, $t1, -4 # Keep memory access consistent with j bge $s3, $0, Loop2 # Loop if j >= 0 Break: sw $s4, 4($t1) # A[j+1] = v addi $s2, $s2, 1 # ++i blt $s2, $s1, Loop1 # Loop while i < 10 Exit: li $v0, 10 # Load exit operation syscall # Exit Update A[j+1]

Procedures and Functions We programmers use procedures and functions to structure and organize programs To make them easier to understand To allow code to be reused 6/4/2013 week04-3.ppt 13

Function A function carries out a well-defined functionality, that can be called and produce the result to be used by the caller.

Functions A function is a consecutive piece of code stored in the memory. To invoke (or call) a function, we must go to that piece of code. Then it does certain things, and get the result we need. What do we know about going to a piece of code?

Functions So, we can call a function by j Function And, the function code will do something we need. Problem: how to come back to the caller?

Two Interesting Instructions and One Interesting Register jal: jump and link jal L1: does TWO things 1. Goto L1. (the next instruction to be executed is at address L1) 2. Save the address of the next instruction in $ra. $ra is the interesting register that stores the return address jr $ra Does ONE thing. Goto the instruction whose address is the value stored in $ra. This is ALL we need to support function calls in MIPS!

Functions The procedure add $t0, $t1, a $t0 a a a sub $t2, $t1, $t0 jal Function sll $t0, $t0, 2... Function: a a a add $t0, $t1, $t0 sub $t2, $t1, $t0 jr $ra

A simple function

.data A:.word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 main: loop: done:.text.globl main la $s7, A li $s0, 0 #i li $s1, 0 #res li $s6, 9 sll $t0, $s0, 2 add $t0, $t0, $s7 lw $a0, 0($t0) lw $a1, 4($t0) jal addfun add $s1, $s1, $v0 addi $s0, $s0, 2 bgt $s0, $s6, done j loop li $v0,10 syscall addfun: add $v0, $a0, $a1 jr $ra

Key things to keep in mind 1. A function is just a segment of code stored sequentially in the memory. To call a function is just to go there. 2. The name of a function in MIPS is JUST A LABEL or JUST AN ADDRESS. 3. We cannot simply use j addfun to go to addfun, because we do not know where come back. Therefore, we need to store the address of the instruction that should be executed after going to the function somewhere, and in MIPS, it is $ra. 4. At the end of a function, we write jr $ra, to go back.

MIPS Calling Conventions MIPS assembly follows the following convention in using registers $a0 - $a3: four argument registers in which to pass parameters $v0 - $v1: two value registers in which to return values $ra: one return address register to return to the point of origin 6/4/2013 week04-3.ppt 22

MIPS Conventions Quite often, our function needs to use some registers to do dome calculation. So we will modify the values of them. We can use $t0-$t9 freely inside a function, because the caller does not expect the values inside $t0-$t9 to stay the same after the function call. But, the caller do expect the values in $s0 to $s7 to be the same after a function call.

MIPS Conventions So, just try to avoid using $s0 and $s7 inside a function whenever possible. But what if do need it? Such occasions will arise

So, if we do have to use $s0 - $s7, we MUST save it somewhere before entering the main part of the function, and restore it before you return (before we execute jr $ra ). In MIPS, we save them in the stack. Stack is a part in the memory allocated for functions. It starts at 0x7ffffffc and grows down as we add more stuffs to it. Stack is first in last out. Stack

$sp The top address of the stack, the address of the first word that is storing value, is (should be) always be stored in $sp. So, adding a word into the stack (pushing a word onto the stack) is a two-step thing, because you have to maintain the correctness of $sp: addi $sp, $sp, -4 sw $s0, 0($sp)

Suppose we want to

Stack and $sp Suppose we want to store a/2 in $s0. How do we get a/2? At the beginning, we do addi $sp, $sp, -4 sw $s0, 0($sp) At the end, we do lw $s0, 0($sp) addi $sp, $sp, 4

.data A:.word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 main: loop: done:.text.globl main la $s7, A li $s0, 0 #i li $s1, 0 #res li $s6, 9 sll $t0, $s0, 2 add $t0, $t0, $s7 lw $a0, 0($t0) lw $a1, 4($t0) jal weirdfun add $s1, $s1, $v0 addi $s0, $s0, 2 bgt $s0, $s6, done j loop li $v0,10 syscall weirdfun: addi $sp, $sp, -4 sw $s0, 0($sp) srl $s0, $a0, 1 add $t0, $a0, $a0 add $t0, $t0, $a1 sub $t0, $t0, $s0 ori $v0, $t0, 0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Function calls inside a function What if we need to call another function inside a function? Will this work? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

Function calls inside a function The problem is that the value of $ra is changed whenever you use jal somelabel. How to deal with it? twofun: addi $sp, $sp, -4 sw $s0, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra

The working versions twofun1: addi $sp, $sp, -4 sw $s0, 0($sp) addi $sp, $sp, -4 sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0, lw $ra, 0($sp) addi $sp, $sp, 4 lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra twofun2: addi $sp, $sp, -8 sw $s0, 4($sp) sw $ra, 0($sp) jal addfun srl $s0, $a0, 1 sub $v0, $v0, $s0, lw $ra, 0($sp) lw $s0, 4($sp) addi $sp, $sp, 8 jr $ra

Saving registers In case of nested function calls, before calling a function, to be safe, the caller should save $t0-$t9 save $a0-$a3 If such registers are needed later. and save $ra Because $ra is going to be needed later and it will be changed The callee should save $s0-s7