MIPS Assembly (FuncDons)

Similar documents
MIPS Assembly (Functions)

MIPS Assembly (FuncDons)

Computer Systems and Networks

Instruction Set Architectures (4)

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

COMP2611: Computer Organization MIPS function and recursion

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

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

Functions in MIPS. Functions in MIPS 1

ECE 250 / CS 250 Computer Architecture. Procedures

CSE Lecture In Class Example Handout

Today. Putting it all together

Lab 4 : MIPS Function Calls

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

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

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

ECE232: Hardware Organization and Design

Procedure Call and Return Procedure call

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

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

Lecture 5: Procedure Calls

ECE 30 Introduction to Computer Engineering

ECE331: Hardware Organization and Design

Course Administration

EE 361 University of Hawaii Fall

CS 316: Procedure Calls/Pipelining

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

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

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

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

Control Instructions

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

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.

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

Common Problems on Homework

Lecture 7: Examples, MARS, Arithmetic

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

MIPS Functions and Instruction Formats

MIPS Procedure Calls - Review

4.2: MIPS function calls

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

ECE 30 Introduction to Computer Engineering

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

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

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

Assignment 1: Pipelining Implementation at SPIM Simulator

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

Computer Architecture

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

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

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

A crash course in MIPS assembly programming

EE 109 Unit 15 Subroutines and Stacks

Compiling Techniques

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

Implementing Procedure Calls

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

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

MIPS Assembly Language Guide

Lec 10: Assembler. Announcements

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

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

Assembler. Lecture 8 CS301

CS64 Week 5 Lecture 1. Kyle Dewey

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

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.

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

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.

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

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS 61c: Great Ideas in Computer Architecture

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

Please put your cellphone on vibrate

CENG3420 Lab 1-1: MIPS assembly language programing

CENG3420 Lecture 03 Review

QtSPIM and MARS : MIPS Simulators

Compiling Code, Procedures and Stacks

SPIM Procedure Calls

Introduction to Assembler Language Programming

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

ECE260: Fundamentals of Computer Engineering

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

Lecture 5: Procedure Calls

Shift and Rotate Instructions

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

MIPS Functions and the Runtime Stack

COMPUTER ORGANIZATION AND DESIGN

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

Anne Bracy CS 3410 Computer Science Cornell University

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

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

Transcription:

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

2 Lab Schedule AcDviDes Monday Discuss: MIPS FuncDons Lab 11 Assignments Due Sunday Apr 14 th Lab 11 due by 11:59pm Wednesday Discuss: MIPS random numbers MIPS floadng- point sqrt() Lab 11 Wednesday Apr 24 th Lab 12 due by 11:59pm Friday Lab 11

3 MIPS Functions

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

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

6 Functions in Assembly In assembly, you must do all the background work for funcdons that the compiler did automadcally in a higher level language FuncDons sdll allow for code re- use (good!), but they re not as trivial as in C or C++

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

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

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 automadcally in $ra Ignore the stack for this example. (Thus, the funcdon will destroy registers used by calling funcdon)

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) # 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 # Exit li $v0,10 # exit 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: # 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 # ------------------------------------------------------------------ # Start.data segment (data!).data x:.word 5 y:.word 0 msg1:.asciiz "y=" lf:.asciiz "\n"

11 Preserving Registers What if we don t want to destroy registers used by the calling funcgon? Need to save those registers somewhere while our funcdon 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 solugon 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"