SPIM Procedure Calls

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

Implementing Procedure Calls

ECE232: Hardware Organization and Design

CSE Lecture In Class Example Handout

CS64 Week 5 Lecture 1. Kyle Dewey

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

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Functions in MIPS. Functions in MIPS 1

MIPS Functions and Instruction Formats

CA Compiler Construction

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

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

Today. Putting it all together

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

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

Course Administration

Procedure Call and Return Procedure call

CS 61c: Great Ideas in Computer Architecture

MIPS Procedure Calls. Lecture 6 CS301

EE 361 University of Hawaii Fall

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

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

Lecture 5: Procedure Calls

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

ECE 30 Introduction to Computer Engineering

ECE331: Hardware Organization and Design

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

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

Common Problems on Homework

CSE Lecture In Class Example Handout

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.

Code Generation. Lecture 19

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

Instruction Set Architectures (4)

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Lecture 7: Procedures

Lecture 7: Procedures and Program Execution Preview

MIPS function continued

CS 316: Procedure Calls/Pipelining

Lab 4 : MIPS Function Calls

COMP2611: Computer Organization MIPS function and recursion

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

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

ECE 15B Computer Organization Spring 2010

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

Storage in Programs. largest. address. address

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

MIPS Assembly (Functions)

6.004 Tutorial Problems L3 Procedures and Stacks

Stacks and Procedures

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

CS61C : Machine Structures

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

Lecture 5: Procedure Calls

CS153: Compilers Lecture 8: Compiling Calls

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

UCB CS61C : Machine Structures

LAB C Translating Utility Classes

MIPS Functions and the Runtime Stack

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

Anne Bracy CS 3410 Computer Science Cornell University

ECE260: Fundamentals of Computer Engineering

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

Control Instructions

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

Compilers and computer architecture: A realistic compiler to MIPS

Topic 3-a. Calling Convention 2/29/2008 1

Code Generation. Lecture 12

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

Project Compiler. CS031 TA Help Session November 28, 2011

Lecture 6: Assembly Programs

Anne Bracy CS 3410 Computer Science Cornell University

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

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

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

Lectures 3-4: MIPS instructions

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

Lab 4 Prelab: MIPS Function Calls

Compilers CS S-08 Code Generation

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.

CS61C : Machine Structures

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

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

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

ECE260: Fundamentals of Computer Engineering

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

CS 110 Computer Architecture MIPS Instruction Formats

Stacks and Procedures

Shift and Rotate Instructions

Compiling Code, Procedures and Stacks

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Procedures and Stacks

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

CS61C : Machine Structures

4.2: MIPS function calls

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

Programming Languages

Transcription:

SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates to the stack and registers. We will compare JAVA procedure calls and see how they relate to a SPIM procedure and how to enforce similarities. Most of this is gleaned from the February 7 lecture notes and Patterson 79-90. 2 Conventions We would like to establish certain conventions so that we need not know anything about the procedure that we are calling. This is equivalent to asking how we use a procedure, and we would like every procedure to be used the same for them to have the same interface. Of course this is not possible in practice, but we can come close. The conventions are that $a0-$a3 are the arguments of a procedure and $v0- $v1 are the return values of a procedure. So this could look something like this in JAVA: word[2] procedure(word $a0, word $a1, word $a2, word $a3) word v[2] = $v0, $v1}; return v; } So to call a procedure in SPIM, we put the arguments in $a0-$a3 and jump to it. The procedure puts the return values in $v0 and/or $v1 where the caller can then read the values. In the case that the procedure has more than four arguments or more then two return values, we can use the stack to transfer data. 3 Preserving calling state We would like to ensure that whenever a procedure is called that the caller has certain guaranties about what to expect what has changed and what has not after a procedure call. This is equivalent to making certain registers local to the procedure. These guaranties are as follows: 1

What should be preserved after a call: $s0-$s7, the save registers $ra, the return address register $sp, the stack pointer register $fp, the frame pointer register Every thing that was on the stack previous to the procedure call What may change after a call: $t0-$t9, the temporary registers may be different $a0-$a3, the argument registers may be different $v0-$v1, the return value registers may be different Anything above the stack may be different These requirements may be met by the called procedure saving the necessary information on the stack. 4 Managing the stack As discussed before, a procedure has certain obligations to its caller and to the whole program. To meet these requirements we use the stack. We would like, as programmers, management of the stack to be as unobtrusive and readable as possible. There are two cases we need to consider: a static stack frame and a dynamic one. 4.1 Static stack frame This is the case where the maximum size of the stack frame is known by the procedure. For the most part this will be the common case (and easier case). In this case we only need to store a subset of $s0-$s7, $ra, and any local information (including $a0-$a3). Of these, we store the ones that will be written over by our procedure, and any local information that we would like to keep before calling another procedure. We do not need to save $sp since we know the maximum size that it will be incremented, and we don t need to save $fp since we do not use it. We would like to get all the stack stuff done as soon as possible so we can focus on our code and not managing the stack. In general, we store everything that we will need to save right upon entering the procedure, making space for anything we do not know yet. This would look like this: procedure: 2

addi $sp, $sp, -MaxStackFrameSize #push the frame (save everything that needs to be saved using sw) (your procedure goes here) (using lw, load everything that was saved) addi $sp, $sp, MaxStackFrameSize #pop the frame Here we have got all the pushing and popping of the stack out of the way. We may still need to read and write from the stack, but no managing the stack. The only thing we need to remember is where we put everything. Even this could be taken care of by declaring some symbols (i.e. PROC1VAR1 = Offset of variable 1). 4.2 Dynamic stack frame Our maximum stack frame size may not be known when we enter a procedure (e.g. putting an array on the stack). In this case we need to do more stack management and introduce another register to handle it. We still need to save everything as in the previous case with the addition of two more things: the frame pointer register $fp and the stack pointer register $sp. We store $fp on the stack and $sp in $fp. Now it looks something like this: procedure: addi $sp, $sp, -NewStackFrameSize #push the frame (save $fp and everything that needs to be saved using sw) add $fp, $sp,newstackframesize #store old $sp in $fp (your procedure goes here) (using lw, load everything except $fp that was saved) move $sp, $fp #pop the frame lw $fp, FPOFFSET($sp) #restore old frame pointer We may still need to push new things to the stack in our procedure, but with the addition of the frame pointer we do not need to worry too much about indexing into the stack. This is because instead of using the changing $sp as a pointer into the stack, we can use $fp that stays the same during the procedure. So when we read or write to the stack we do so by offsetting from the frame pointer (i.e. lw $s0, 8($fp)). 3

5 JAVA to SPIM examples 5.1 Static stack frame: JAVA: void main() } foo(777); int foo(int n) } SPIM: return n*bar(); int bar() return 1337; } ###### void main() ####### main: # foo(777) li $a0, 777 jal foo 4

###### end main ###### ###### int foo(int n) ##### # $a0 = n # $v0 = return value # stack frame = $ra, maxsize = 4 foo: #push stack frame addi $sp, $sp, -4 sw $ra, 0($sp) #$v0 = bar() jal bar # return n*bar() mult $v0, $a0, $v0 #pop stack frame lw $ra, 0($sp) addi $sp, $sp, 4 ###### end foo ###### ###### int bar() ###### # $v0 = 1337 bar: # no use of $s0-$s7 and no procedure calls so # no stack frame li $v0, 1337 ###### end bar ###### 5.2 Too many arguments Here we will use five arguments, too many to store in $a0-$a3, so we put the fifth on the stack. It may seem redundant to not use another register (i.e. $t0) but what if we had 33 arguments? JAVA: void main() foo(1,2,3,4,5); 5

} int foo(int a, int b, int c, int d, int e) } SPIM: return a+b+c+d+e; ###### void main() ###### main: # foo(1,2,3,4,5) li $a0, 1 li $a1, 2 li $a2, 3 li $a3, 4 # the fifth argument goes on top of the stack li $t0, 5 sw $t0, -4($sp) jal foo ###### end main ###### ###### int foo(int a, int b, int c, int d, int e) ###### # $a0 = a, $a1 = b, $a2 = c, $a3 = d # arg[4] = -4($sp) = e # $v0 = a+b+c+d+e foo: # push stack frame including the fifth arg addi $sp, $sp, -8 sw $ra, 0($sp) # Note: now 4($sp) = d. # In general arg[i] = framesize - 4*(i-3) for i > 3,2,1,0 # $t0 = 4($sp) = arg[4] lw $t0, 4($sp) # put $a0+$a1+$a2+$a3+$t0 in $v0 # return $v0 #pop stack frame lw $ra, 0($sp) addi $sp, $sp, 8 ###### end foo ###### 6