MIPS Assembly (Functions)

Similar documents
MIPS Assembly (FuncDons)

MIPS Assembly (FuncDons)

Instruction Set Architectures (4)

Computer Systems and Networks

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

Functions in MIPS. Functions in MIPS 1

CSE Lecture In Class Example Handout

COMP2611: Computer Organization MIPS function and recursion

Today. Putting it all together

ECE 250 / CS 250 Computer Architecture. Procedures

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

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

MIPS Procedure Calls. Lecture 6 CS301

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

MIPS function continued

Lab 4 : MIPS Function Calls

ECE232: Hardware Organization and Design

Course Administration

Procedure Call and Return Procedure call

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

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

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

Control Instructions

Lecture 5: Procedure Calls

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

Common Problems on Homework

ECE331: Hardware Organization and Design

EE 361 University of Hawaii Fall

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

MIPS Procedure Calls - Review

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.

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

MIPS Functions and Instruction Formats

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

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

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

ECE 30 Introduction to Computer Engineering

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

Lecture 7: Examples, MARS, Arithmetic

4.2: MIPS function calls

CS64 Week 5 Lecture 1. Kyle Dewey

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

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

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

EE 109 Unit 15 Subroutines and Stacks

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

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

Assignment 1: Pipelining Implementation at SPIM Simulator

CS 316: Procedure Calls/Pipelining

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.

A crash course in MIPS assembly programming

Compiling Techniques

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

ECE 30 Introduction to Computer Engineering

We can emit stack-machine-style code for expressions via recursion

Computer Architecture

Implementing Procedure Calls

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

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

Shift and Rotate Instructions

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

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

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Compiling Code, Procedures and Stacks

MIPS Assembly Language Guide

COMPUTER ORGANIZATION AND DESIGN

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

Lecture 5: Procedure Calls

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

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Assembler. Lecture 8 CS301

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

Lec 10: Assembler. Announcements

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Chapter 2A Instructions: Language of the Computer

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

SPIM Procedure Calls

Lecture 7: Procedures and Program Execution Preview

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.

Anne Bracy CS 3410 Computer Science Cornell University

MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

CS61C : Machine Structures

ECE260: Fundamentals of Computer Engineering

Compilers and computer architecture: A realistic compiler to MIPS

QtSPIM and MARS : MIPS Simulators

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

CENG3420 Lecture 03 Review

SPIM Instruction Set

MIPS Functions and the Runtime Stack

Thomas Polzer Institut für Technische Informatik

Lecture 6: Assembly Programs

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Lab 4 Prelab: MIPS Function Calls

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

Transcription:

ECPE 170 Jeff Shafer University of the Pacific MIPS Assembly (Functions)

2 Lab Schedule This Week Activities Lab work time MIPS functions MIPS Random Number Generator Lab 11 Assignments Due Due by Apr 19 th 5:00am Lab 12 Due by May 3 rd 5:00am

3 MIPS Functions

4 Function Requirements? What happens when we call a function? 1. Place function arguments in standard location where function can find them 2. Save current program location to return to later (the Program Counter register) 3. Jump to the function location 4. Function runs using provided arguments 5. Function produces output (return value) and saves it in standard location 6. Jump to original program location (return) 1. Technically, +1 instruction

5 Function Requirements Can a function change local variables of its calling function? No! The function operates in its own bubble What happens if the function changes $s0 which was also used by the calling function? Problem! Your function has corrupted the calling function

6 Functions in Assembly In assembly, you must do all the background work for functions that the compiler did automatically in a higher level language Functions still allow for code re-use (good!), but they re more complicated than in C or C++

7 Registers Name $zero $s0-$s7 $t0-$t9 Constant value: ZERO Use Local variables (Convention: These are saved if a function needs to re-use them) Temporary results (Convention: These are not saved if a function needs to re-use them) $a0-$a3 Arguments to pass to function (max of 4) $v0-$v1 Return value to obtain from function (max of 2) $ra $sp Return address of function Stack pointer (current top of stack)

8 More Jumps Jump and Link (side effect: $ra stores address of next instruction) jal <destination> Use this to call a function! Jump Register (destination address is stored in <reg1> jr <reg1> Use this to return from a function!

9 Task : Write Code #include <stdio.h> int function(int a); int main() { int x=5; int y; } y = function(x); printf("y=%i\n", y); return 0; int function(int a) { return 3*a+5; } Place arguments in $a0-$a3 Place return values in $v0-$v1 Return address saved automatically in $ra Ignore the stack for this example. (Thus, the function will destroy registers used by calling function)

10 # Simple routine to demo functions # NOT using a stack in this example. # Thus, the function does not preserve values # of calling function! # ------------------------------------------------------------------ # ------------------------------------------------------------------ main:.text.globl main # Register assignments # $s0 = x # $s1 = y # Initialize registers lw $s0, x # Reg $s0 = x lw $s1, y # Reg $s1 = y # Call function move $a0, $s0 # Argument 1: x ($s0) jal fun # Save current PC in $ra, and jump to fun move $s1,$v0 # Return value saved in $v0. This is y ($s1) fun: # FUNCTION: int fun(int a) # Arguments are stored in $a0 # Return value is stored in $v0 # Return address is stored in $ra (put there by jal instruction) # Typical function operation is: # Do the function math li $s0, 3 mul $s1,$s0,$a0# s1 = 3*$a0 (i.e. 3*a) addi $s1,$s1,5 # 3*a+5 # Save the return value in $v0 move $v0,$s1 # Return from function jr $ra # Jump to addr stored in $ra # Print msg1 li $v0, 4 # print_string syscall code = 4 la $a0, msg1 syscall # Print result (y) li $v0,1 # print_int syscall code = 1 move $a0, $s1 # Load integer to print in $a0 syscall # ------------------------------------------------------------------ # Start.data segment (data!).data x:.word 5 y:.word 0 msg1:.asciiz "y=" lf:.asciiz"\n" # Print newline li $v0,4 # print_string syscall code = 4 la $a0, lf syscall # Exit li $v0,10 # exit syscall

11 Preserving Registers What if we don t want to destroy registers used by the calling function? Need to save those registers somewhere while our function runs (like memory!) A stack is a good structure for this

12 The Stack Stack is a data structure stored in memory $sp Memory $sp ( Stack Pointer ) points to top of stack But stack grows down in memory! Example Push 4 to stack Push 5 to stack Pop (5 from stack) Pop (4 from stack)

13 The Stack Stack is a data structure stored in memory $sp ( Stack Pointer ) points to top of stack But stack grows down in memory! Example Push 4 to stack Push 5 to stack Pop (5 from stack) Pop (4 from stack) Memory $sp 4

14 The Stack Stack is a data structure stored in memory Memory 4 $sp ( Stack Pointer ) points to top of stack But stack grows down in memory! $sp 5 Example Push 4 to stack Push 5 to stack Pop (5 from stack) Pop (4 from stack)

15 The Stack Stack is a data structure stored in memory $sp ( Stack Pointer ) points to top of stack But stack grows down in memory! Example Push 4 to stack Push 5 to stack Pop (5 from stack) Pop (4 from stack) Memory $sp 4

16 The Stack Stack is a data structure stored in memory $sp Memory $sp ( Stack Pointer ) points to top of stack But stack grows down in memory! Example Add 4 to stack Add 5 to stack Pop Pop

17 The Stack How would we modify previous solution to use a stack?

18 # Simple routine to demo functions # NOT using a stack in this example. # Thus, the function does not preserve values # of calling function! # ------------------------------------------------------------------ # ------------------------------------------------------------------ main:.text.globl main # Register assignments # $s0 = x # $s1 = y # Initialize registers lw $s0, x # Reg $s0 = x lw $s1, y # Reg $s1 = y # Call function move $a0, $s0 # Argument 1: x ($s0) jal fun # Save current PC in $ra, and jump to fun move $s1,$v0 # Return value saved in $v0. This is y ($s1) # Print msg1 li $v0, 4 # print_string syscall code = 4 la $a0, msg1 syscall # Print result (y) li $v0,1 # print_int syscall code = 1 move $a0, $s1 # Load integer to print in $a0 syscall # Print newline li $v0,4 # print_string syscall code = 4 la $a0, lf syscall # FUNCTION: int fun(int a) # Arguments are stored in $a0 # Return value is stored in $v0 # Return address is stored in $ra (put there by jal instruction) # Typical function operation is: fun: # This function overwrites $s0 and $s1 # We should save those on the stack # This is PUSH ing onto the stack addi $sp,$sp,-4# Adjust stack pointer sw $s0,0($sp) # Save $s0 addi $sp,$sp,-4# Adjust stack pointer sw $s1,0($sp) # Save $s1 # Do the function math li $s0, 3 mul $s1,$s0,$a0# s1 = 3*$a0 (i.e. 3*a) addi $s1,$s1,5 # 3*a+5 # Save the return value in $v0 move $v0,$s1 # Restore saved register values from stack in opposite order # This is POP ing from stack lw $s1,0($sp) # Restore $s1 addi $sp,$sp,4 # Adjust stack pointer lw $s0,0($sp) # Restore $s0 addi $sp,$sp,4 # Adjust stack pointer # Return from function jr $ra # Jump to addr stored in $ra # Exit li $v0,10 # exit syscall # ------------------------------------------------------------------ # Start.data segment (data!).data x:.word 5 y:.word 0 msg1:.asciiz "y=" lf:.asciiz"\n"

19 Random Number Generator

20 In-Class Discussion