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

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

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

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

CSE Lecture In Class Example Handout

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CS64 Week 5 Lecture 1. Kyle Dewey

Functions in MIPS. Functions in MIPS 1

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

Today. Putting it all together

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

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

Implementing Procedure Calls

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

CS 316: Procedure Calls/Pipelining

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

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

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

MIPS Procedure Calls. Lecture 6 CS301

MIPS Assembly (Functions)

Common Problems on Homework

COMP2611: Computer Organization MIPS function and recursion

The Activation Record (AR)

Lab 4 : MIPS Function Calls

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

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

CSE Lecture In Class Example Handout

Course Administration

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

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

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

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

Instruction Set Architectures (4)

MIPS function continued

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.

Compilers and computer architecture: A realistic compiler to MIPS

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Assembly (FuncDons)

ECE 30 Introduction to Computer Engineering

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

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.

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

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

MIPS Assembly (FuncDons)

Code Generation. Lecture 19

Storage in Programs. largest. address. address

SPIM Procedure Calls

ECE260: Fundamentals of Computer Engineering

Lec 10: Assembler. Announcements

MIPS Functions and Instruction Formats

ECE331: Hardware Organization and Design

CS153: Compilers Lecture 8: Compiling Calls

Lecture 5: Procedure Calls

Code Generation. Lecture 12

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

Anne Bracy CS 3410 Computer Science Cornell University

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

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

Computer Systems and Networks

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

MIPS Functions and the Runtime Stack

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

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

Lecture 7: Procedures and Program Execution Preview

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

Shift and Rotate Instructions

Compiling Techniques

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

Stacks and Procedures

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

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

CS61C : Machine Structures

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

Computer Hardware Engineering

Homework 9: Stack Frame

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

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

Lecture 5: Procedure Calls

Compiling Code, Procedures and Stacks

Procedure Call and Return Procedure call

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

CA Compiler Construction

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

EE 109 Unit 15 Subroutines and Stacks

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Lab 4 Prelab: MIPS Function Calls

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

Computer Architecture I Midterm I

Functions and Procedures

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

MIPS Instruction Set

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

MIPS Assembly Programming

Transcription:

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Ziad Matni Dept. of Computer Science, UCSB

Lecture Outline More on MIPS Calling Convention Functions calling functions The role of the convention and how to use it 5/3/18 Matni, CS64, Sp18 2

MIPS Calling Conventions What we are talking about today and the next lecture You *must* read the handout on MIPS Calling Conventions Found on our webpage 5/3/18 Matni, CS64, Sp18 3

Any Questions From Last Lecture? 5/3/18 Matni, CS64, Sp18 4

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/3/18 Matni, CS64, Sp18 5

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 5/3/18 Matni, CS64, Sp18 6

Function Calls Within Functions Given what we ve said so far What about this code makes our previously discussed setup break? You would need multiple copies of $ra You d have to copy the value of $ra to another register (or to mem) before calling another function Danger: You could run out of registers! 5/3/18 Matni, CS64, Sp18 7

Another Example What about this code makes this setup break? Can t fit all variables in registers at the same time! How do I know which registers are even usable without looking at the code? 5/3/18 Matni, CS64, Sp18 8

Solution??!! Store certain information in memory only at certain times Ultimately, this is where the call stack comes from So what (registers/memory) save what??? 5/3/18 Matni, CS64, Sp18 9

What Saves What? By MIPS convention, certain registers are designated to be preserved across a call Preserved registers are saved by the function called (e.g., $s0 - $s7) So these are saved at the start of every function Non-preserved registers are saved by the caller of the function (e.g., $t0 - $t9) So these are saved by the function s caller Or not (they can be ignored under certain circumstances) 5/3/18 Matni, CS64, Sp18 10

And Where is it Saved? Register values are saved on the stack The top of the stack is held in $sp (stackpointer) The stack grows from high addresses to low addresses 5/3/18 Matni, CS64, Sp18 11

The Stack When a program starts executing, a certain contiguous section of memory is set aside for the program called the stack. 5/3/18 Matni, CS64, Sp18 12

The Stack The stack pointer is a register ($sp) that contains the top of the stack. $sp contains the smallest address x such that any address smaller than x is considered garbage, and any address greater than or equal to x is considered valid. 5/3/18 Matni, CS64, Sp18 13

The Stack In this example, $sp contains the value 0x0000 1000. The shaded region of the diagram represents valid parts of the stack. 5/3/18 Matni, CS64, Sp18 14

The Stack Stack Bottom: The largest valid address of a stack. When a stack is initialized, $sp points to the stack bottom. Stack Limit: The smallest valid address of a stack. If $sp gets smaller than this, then there's a stack overflow 5/3/18 Matni, CS64, Sp18 15

STACK (LIFO) PUSH AND POP 5/3/18 Matni, CS64, Sp18 16

Stack Push and Pop PUSH one or more registers Subtract 4 times the number of registers to be pushed on the stack pointer Why???? Copy the registers to the stack (do a sw instruction) Example: addi $sp, $sp, -8 # 2 registers to save sw $s0, 4($sp) sw $s1, 0($sp) 5/3/18 Matni, CS64, Sp18 17

Stack Push and Pop POP one or more registers Reverse process from push Copy the data from the stack to the registers (do a lw instruction) Add 4 times the number of registers to be popped on the stack. Example: lw $s0, 4($sp) lw $s1, 0($sp) addi $sp, $sp, 8 # 2 registers to restore # Note: you cannot do the addi first 5/3/18 Matni, CS64, Sp18 18

save_registers.asm The program will look at 2 integers (a0, a1) and ultimately returns (a0 + a0) + (a1 + a1) via a function call (i.e. jal) The function will first create room for 2 words on the stack It will push $s0 & $s1 onto the stack We ll use $s0 and $s1 b/c we want them to be preserved across a call It will calculate the returned value and put the result in $v0 We will then restore the original registers It will pop 2 words from the stack & place them in $s0 & $s1 5/3/18 Matni, CS64, Sp18 19

.data solution_text:.asciiz "Solution: " saved_text:.asciiz "Saved: newline:.asciiz "\n.text # $a0: first integer # $a1: second integer # Returns ($a0 + $a0) + ($a1 + $a1) in $v0. # Uses $s0 and $s1 as part of this process because these are preserved across a call. # add_ints must therefore save their values internally using the stack. add_ints: # save $s0 and $s1 on the stack (i.e. push) addi $sp, $sp, -8 # make room for two words sw $s0, 4($sp) # note the non-zero offset sw $s1, 0($sp) # calculate the value add $s0, $a0, $a0 add $s1, $a1, $a1 add $v0, $s0, $s1 save_registers.asm # because $t0 is assumed to not be preserved, we can modify it directly (and it will not matter b/c we ll pop the saved $t0 out of the stack later) li $t0, 4242 # restore the registers and return (i.e. pop) lw $s1, 0($sp) lw $s0, 4($sp) addi $sp, $sp, 8 jr $ra Matni, CS64, Sp18 20

main: # We happen to have the value 1 in $t0 and 2 in $s0 in this example # $t0 and $s0 are independent of the function li $t0, 1 li $s0, 2 # We want to call add_ints. Because we want to save the value of $t0, in this case, # and because it's not preserved across a call (we can t assume it will be), it is our # (the caller s) responsibility to store it on the stack and restore it afterwards addi $sp, $sp, -4 sw $t0, 0($sp) # saving $t0 is the caller s responsibility, $s0 is the callee s # setup the function call and make it li $a0, 3 li $a1, 7 jal add_ints # restore $t0 also, we can assume that $s0 still has the value 2 in it # because the CC says the function has to preserve $s registers lw $t0, 0($sp) addi $sp, $sp, 4 # print out the solution prompt move $t1, $v0 li $v0, 4 la $a0, solution_text syscall # print out the solution itself li $v0, 1 move $a0, $t1 syscall save_registers.asm # print out a newline and end (not shown) la $a0, newline li $v0, 4 syscall 5/3/18 21

What is a Calling Convention? It s a protocol about how you call functions and how you are supposed to return from them Every CPU architecture has one They can differ from one arch. to another 3 Reasons why we care: Because it makes programming a lot easier if everyone agrees to the same consistent (i.e. reliable) methods Makes testing a whole lot easier I will ask you to use it in assignments and in exams! And you loose major points (or all of them) if you don t 5/3/18 Matni, CS64, Sp18 22

More on the Why Have a way of implementing functions in assembly But not a clear, easy-to-use way to do complex functions In MIPS, we do not have an inherent way of doing nested/recursive functions Example: Saving an arbitrary amount of variables Example: Jumping back to a place in code recursively There is more than one way to do things But we often need a convention to set working parameters Helps facilitate things like testing and inter-compatibility This is partly why MIPS has different registers for different uses 5/3/18 Matni, CS64, Sp18 23

Instructions to Watch Out For jal <label> and jar $ra always go together Function arguments have to be stored ONLY in $a0 thru $a3 Function return values have to be stored ONLY in $v0 and $v1 If functions need additional registers whose values we don t care about keeping after the call, then they can use $t0 thru $t9 What about $s registers? AKA the preserved registers Hang in there will talk about them in a few slides 5/3/18 Matni, CS64, Sp18 24

MIPS C.C. for CS64: Assumptions We will not utilize $fp and $gp regs $fp: frame pointer $gp: global pointer Assume that functions will not take more than 4 arguments and will not return more than 2 arguments Makes our lives a little simpler Assume that all values on the stack are always 32-bits That is, no overly long data types or complex data structures like C-Structs, Classes, etc 5/3/18 Matni, CS64, Sp18 25

MIPS Call Stack We know what a Stack is A Call Stack is used for storing the return addresses of the various functions which have been called When you call a function (e.g. jal funca), the address that we need to return to is pushed into the call stack. funca does its thing then The function needs to return. So, the address is popped off the call stack 5/3/18 Matni, CS64, Sp18 26

void first() { second() return; } void second() { third (); return; } void third() { fourth (); return; } void forth() { return; } MIPS Call Stack Top of the Stack Address of where second third should return to (i.e. after jal second ) third ) Address of where second should return to (i.e. after jal second ) fourth: jr $ra third: push $ra jal fourth pop $ra jr $ra second: push $ra jal third pop $ra jr $ra first: jal second li $v0, 10 syscal 5/3/18 Matni, CS64, Sp18 27 POP PUSH

Why addiu? Because there is no such thing as a negative memory address AND we want to avoid triggering a processor-level exception on overflow fourth: jr $ra third: addiu $sp, $sp, -4 sw $ra, 0($sp) jal fourth lw $ra, 0($sp) addiu $sp, $sp, 4 jr $ra second: addiu $sp, $sp, -4 sw $ra, 0($sp) jal third lw $ra, 0($sp) addiu $sp, $sp, 4 jr $ra first: jal second fourth: jr $ra third: push $ra jal fourth pop $ra jr $ra second: push $ra jal third pop $ra jr $ra first: jal second li $v0, 10 syscall li $v0, 10 syscal 5/3/18 Matni, CS64, Sp18 28

YOUR TO-DOs Make sure you read the handout on MIPS Calling Conventions Finish assignment/lab #4 Assignment due on FRIDAY Prep for Lab #5 on Monday 5/3/18 Matni, CS64, Sp18 29

5/3/18 Matni, CS64, Sp18 30