Procedure Call and Return Procedure call

Similar documents
COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Lecture 5: Procedure Calls

ECE232: Hardware Organization and Design

CSE Lecture In Class Example Handout

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

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

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

Implementing Procedure Calls

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

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

ECE260: Fundamentals of Computer Engineering

MIPS Assembly (Functions)

MIPS Functions and Instruction Formats

ECE331: Hardware Organization and Design

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.

CS 316: Procedure Calls/Pipelining

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

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

ECE260: Fundamentals of Computer Engineering

SPIM Procedure Calls

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

Lecture 7: Procedures

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

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

Functions in MIPS. Functions in MIPS 1

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

Anne Bracy CS 3410 Computer Science Cornell University

MIPS Functions and the Runtime Stack

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

Control Instructions

EE 109 Unit 15 Subroutines and Stacks

Course Administration

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

EE 361 University of Hawaii Fall

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

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

MIPS Procedure Calls. Lecture 6 CS301

MIPS Assembly (FuncDons)

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

CS 61c: Great Ideas in Computer Architecture

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

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

Anne Bracy CS 3410 Computer Science Cornell University

CS153: Compilers Lecture 8: Compiling Calls

Lecture 5: Procedure Calls

COMP2611: Computer Organization MIPS function and recursion

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

CSE Lecture In Class Example Handout

CA Compiler Construction

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.

Anne Bracy CS 3410 Computer Science Cornell University

Stacks and Procedures

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Lecture 7: Procedures and Program Execution Preview

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

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

CS61C : Machine Structures

Patterson PII. Solutions

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Lec 10: Assembler. Announcements

More C functions and Big Picture [MIPSc Notes]

Today. Putting it all together

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

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

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

ECE260: Fundamentals of Computer Engineering

Instruction Set Architectures (4)

MIPS function continued

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

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Compiling Code, Procedures and Stacks

Lecture 6: Assembly Programs

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

Agenda. Strings: C vs. Java. Loading, Storing bytes. Support for Characters and Strings 6/29/2011

ECE 30 Introduction to Computer Engineering

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

ECE/CS 314 Fall 2003 Homework 2 Solutions. Question 1

Stacks and Procedures

COMPUTER ORGANIZATION AND DESIGN

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

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

CS61C : Machine Structures

MIPS Assembly (FuncDons)

The Activation Record (AR)

Stacks and Procedures

Lectures 3-4: MIPS instructions

MIPS Procedure Calls - Review

UCB CS61C : Machine Structures

Stacks and Procedures

ECE 30 Introduction to Computer Engineering

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

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

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

Thomas Polzer Institut für Technische Informatik

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

ECE369. Chapter 2 ECE369

Compiling Techniques

Transcription:

Procedures int len(char *s) { for (int l=0; *s!= \0 ; s++) l++; main return l; } void reverse(char *s, char *r) { char *p, *t; int l = len(s); reverse(s,r) N/A *(r+l) = \0 ; reverse l--; for (p=s+l t=r; l>=0; l--) { *t++ = *p--; } len(s) 12 } void main(int) { char *s = Hello World! ; len } char r[100]; reverse(s,r); How can we do this with assembly? * Need a way to call / return procedures * Need a way to pass arguments * Need a way to return a value 64 Procedure Call and Return Procedure call Jump to the procedure The return goes back to the point immediately after the call Need to pass return address (instruction after call) jal Label " $ra = PC+4 # set return address to next PC " PC = PC[31:28] Label << 2 # jump to procedure Procedure return Need return address (address of instruction after the jal Label) Need to jump back to the return point jr $ra " PC = $ra # jump back to return address 65 1

In Class Quick Example! Write a procedure hello that prints Hello Write a procedure world that prints World Write code to print HelloWorld using the procedures 66 In-Class Quick Example jal world jal hello jal world li $v0,10 hello: la $a0,h_msg # assume h_msg declared Hello li $v0,4 jr $ra world: la $a0,w_msg # assume w_msg declared World li $v0,4 jr $ra 67 2

Arguments and Return Value Register conventions specified in PRM $a0-$a3: four arguments for passing values to called procedure $v0-$v1: two values returned from called procedure $ra: return address register (set by call, used by return) Call chains One procedure calls another, which calls another one E.g., main reverse len What happens to $ra??? (e.g., when reverse calls len) You must save $ra someplace! Simple approach: A free register (can t be used by caller) Leaf procedure: Doesn t make any calls. Doesn t need to save $ra. 68 In-class Example Write a procedure that replaces a character with a new one in a string e.g., findreplace(char* string, char old, char new) Shows passing values with arguments $a0 is the string $a1 is the old character $a2 is the new character 69 3

In-class Example _replace: # $a0 is string address # $a1 is old character # $a2 is new character move $t0,$a0 # save contents of $a0 _replace_loop: lbu $t1,0($t0) addi $t0,$t0,1 beq $t1,$0,_replace_end sub $t1,$t1,$a1 # check for old character bne $t1,$0,_replace_loop # continue sb $a2,-1($t0) # save new char, -1 offset (add above) j _replace_loop _replace_end: jr $ra 70 In-class Example # uses _replace to change * to spaces.data my_str:.asciiz "Hello*World*and*CS*447!\n".text la $a0,my_str # address of the string li $a1,'* # old character to be replaced li $a2,' # the new character, a space jal _replace # call procedure li $v0,4 # << procedure returns here print string >> li $a1, # $a0 not changed - let s replace space with + li $a2,+ jal _replace # $v0 wasn t changed, so can just li $v0,10 71 4

In-class Example #2 Write a procedure print that prints string Hello! Write a procedure print_n that calls print n times 72 In-class Example #2 _print: # $a0 holds string address to print li $v0,4 jr $ra _print_n: # $a0 is string address, $a1 is number times to print move $s0,$ra # save return address move $s1,$a1 # we will modify $s1 _print_n_loop: beq $s1,$0,_print_n_cont jal _print addi $s1,$s1,-1 j _print_n_loop _print_n_cont: move $ra,$s0 # restore $ra jr $ra # could return as jr $s0 73 5

In-class Example #2 # calls print_n.data my_str:.asciiz "Hello!".text la $a0,my_str # address of string to print li $a1,10 # number of times to print it jal _print_n # call function print_n(string, n) li $v0,10 # exit service # terminate 74 In-class Example Change the WorldHelloWorld example to use one procedure that prints a string. 75 6

In-Class Example.data w_msg:.asciiz World h_msg:.asciiz Hello.data la $a0,w_msg jal print la $a0,h_msg jal print la $a0,w_msg jal print li $v0,10 print: li $v0,4 jr $ra 76 Another example! Write two procedures Procedure #1: print(str): prints the string pointed to by str Procedure #2: hello(n): print Hello World! n times Newline between each print Shouldn t print anything when n=0 What argument register to use? See inclass5.asm 77 7

More Procedure Call/Return Caller: The procedure that calls another one Callee: The procedure that is called by the caller What if callee wants to use registers? Caller is also using registers!!! If callee wants to use same registers, it must save them Consider what happened with $ra in a call chain Register usage conventions specified by PRM $t0-$t9: Temp. registers; if caller wants them, must save before call $s0-$s7: Saved registers; saved by callee prior to using them 78 Where to save? Need memory space to hold saved ( spilled ) registers Caller spills $t0-$t9 that be must saved to memory Callee spills $s0-$s7 to memory, when these regs are used Other registers (e.g., $v0, $v1 might also need to be saved) Non-leaf caller saves $ra when making another call Each procedure needs locations to save registers In general, call-chain depth (number of called procs) is unknown, so we need to support undetermined length Suggestion: Use a stack, located in memory. Add stack element onto stack for each call. The stack element has the locations to hold values. 79 8

Program Stack Program stack: Memory locations used by running program Has space for saved registers Has space for local variables, when can t all fit in registers " E.g., local arrays are allocated on the stack Has space for return address Each procedure allocates space for these items So-called activation frame (a.k.a., activation record ) Purpose of locations in activation frame are known Location of activation frame isn t known until procedure call made Prologue (entry point into the procedure): Allocates an activation frame on the stack Epilogue (exit point from procedure): De-allocates the activation frame, does actual return 80 Procedure Structure and Stack foo: # prologue - entry to function addi $sp, $sp, -8 # push, adjust size(ar) sw $s0,0($sp) # save needed temp reg sw $ra,4($sp) # save ra, non-leaf. procedure body.. (1) Before call (2) Prologue 32 <$sp 32 28 24 20 16 28 24 20 16 $ra saved $s0 saved <$sp foo_exit: # epilogue - exit from function lw $ra,4($sp) # restore RA lw $s0,0($sp) # restore $s0 addi $sp,$sp,8 # pop AR jr $ra (3) Proc body (4) Epilogue 32 28 24 20 16 $ra saved $s0 saved <$sp 32 28 24 20 16 <$sp 81 9

Calling convention Caller saves needed registers, sets up args, makes call Argument registers $a0-$a3 When not enough arg regs: put arguments onto the stack Callee procedure prologue Adjust stack pointer for activation frame size to hold enough space to hold saved registers, locals, return address (non-leaf) Save any saved registers to the stack Save return address to the stack Callee procedure body Access stack items as needed Including loading arguments from the stack Callee procedure epilogue Restore return address from the stack (non-leaf) Restore any saved registers from the stack Return to caller Return value in $v0, $v1 82 In-class Example Return to print_n procedure It called print to display the string print_n was a non-leaf procedure Thus, we must save $ra to stack We also used $s1, so we need to save this too! 83 10

In-class Example _print_n: # $a0 is string address, $a1 is number times to print addi $sp,$sp,-8 # push frame: storing 8 bytes sw $ra,0($sp) # save return address -- not a leaf sw $s1,4($sp) # save $s1 by convention (caller, this proc, uses it) move $s1,$a1 # use $s1 to hold $a1 across call _print_n_loop: beq $s1,$0,_print_n_cont jal _print addi $s1,$s1,-1 j _print_n_loop _print_n_cont: lw $ra,0($sp) # restore $ra lw $s1,4($sp) # restore $s1 addi $sp,$sp,8 # pop stack frame jr $ra # return 84 Example: Factorial /* factorial */ int fac(int f) { if (f == 1) // end of recursion return 1; else // go to bottom return (fac(f-1) * f); } int main(void) { a = fac(3); print(a); } 85 11

Example: Factorial fact(3) returns 6 fact(3-1) * 3 returns 2 * 3 fact(2-1) * 2 returns 1 * 2 fact(1) * 1 returns 1 * 1 call factorial again, when not at end of recursion (f==1) on each call, we need to pass a new argument to next one on return, we do the actual computation and pass value back need the return address & possibly temporary storage set up a stack to make space See factorial.asm 86 12