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

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

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

Instruction Set Architectures (4)

ECE232: Hardware Organization and Design

Functions in MIPS. Functions in MIPS 1

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

ECE260: Fundamentals of Computer Engineering

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

CSE Lecture In Class Example Handout

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

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

Functions and Procedures

Today. Putting it all together

Course Administration

Implementing Procedure Calls

Storage in Programs. largest. address. address

EE 361 University of Hawaii Fall

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

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

CS64 Week 5 Lecture 1. Kyle Dewey

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.

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

MIPS Procedure Calls. Lecture 6 CS301

Procedures and Stacks

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

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

Control Instructions

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

COMP2611: Computer Organization MIPS function and recursion

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

MIPS Functions and the Runtime Stack

Lecture 5: Procedure Calls

Compiling Code, Procedures and Stacks

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

CA Compiler Construction

ECE331: Hardware Organization and Design

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

ECE 30 Introduction to Computer Engineering

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

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

CS61C : Machine Structures

Common Problems on Homework

Lecture 7: Procedures and Program Execution Preview

SPIM Procedure Calls

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

Lab 4 : MIPS Function Calls

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.

CS 316: Procedure Calls/Pipelining

EE 109 Unit 15 Subroutines and Stacks

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

CSE Lecture In Class Example Handout

ECE 30 Introduction to Computer Engineering

MIPS Assembly (Functions)

Instructions: Assembly Language

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

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.

Chapter 2A Instructions: Language of the Computer

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

Lecture 7: Procedures

6.004 Tutorial Problems L3 Procedures and Stacks

MIPS Procedure Calls - Review

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

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

CS153: Compilers Lecture 8: Compiling Calls

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

Shift and Rotate Instructions

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

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

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

The Activation Record (AR)

CSCE 5610: Computer Architecture

ECE 15B Computer Organization Spring 2010

UCB CS61C : Machine Structures

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

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

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Systems I. Machine-Level Programming V: Procedures

CS61C : Machine Structures

MIPS Assembly (FuncDons)

Lab 4 Prelab: MIPS Function Calls

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

MIPS Functions and Instruction Formats

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

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

More C functions and Big Picture [MIPSc Notes]

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

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Compiling Techniques

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

LAB C Translating Utility Classes

MIPS%Assembly% E155%

CENG3420 Lecture 03 Review

Computer Systems and Networks

Thomas Polzer Institut für Technische Informatik

Procedure Call and Return Procedure call

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

COE608: Computer Organization and Architecture

Transcription:

Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i + 273);} Arguments/ Parameters Result of the subroutine

Subroutine Important for modularizing programs separation of concerns, smaller coding units Each subroutine should be able to perform independently without worrying about what registers are in use by its callee subroutines. reuse of code call from anywhere CS 230 - Spring 2018 2-2

Subroutines Basic Steps: 1. Place arguments in a place where the subroutine can access them 2. Store the current PC+4 in a place so that the program can return to it at the end 3. Jump from current PC to the PC at the start of subroutine 4. Perform the instructions in the subroutine and store the result in a place so that the main program can access it. 5. Return to the main program at the point saved in step 2. CS 230 - Spring 2018 2-3

Subroutines Special register for supporting subroutines $31: return address register - stores the return address Subroutine Call - Instruction for jumping to the subroutine and saving the current PC: jal L1 ;jump-and-link copy (current PC + 4) to $31 Jump to L1 Subroutine Return - Instruction for jumping back to the saved point jr $31 Jump to address in $31 CS 230 - Spring 2018 2-4

Subroutines Problem! If after jal, $31 stores the return address, what have we lost? the return address of the emulator (to end our program) or another subroutine call s return address This means if a piece of code wants to call a subroutine before doing so it must store its own return address or else it will lose it! Where should we store it? Register No! ; Because, a subroutine might use that register for its work, and we want to do nested subroutines and recursion. We use a Stack basically, an area in the memory CS 230 - Spring 2018 2-5

Nested Subroutines CS 230 - Spring 2018 2-6

Stack LIFO (last in, first out) principle CS 230 - Spring 2018 2-7

Stack For each function call, a block of stack space, called a stack frame, is allocated from the memory. This stack frame will be used by the function Before the function returns, it must pop the stack fully We don't need to remember specific memory addresses for each value, just store address of the last thing we pushed (top of the stack) and remember what order we pushed things in! Normally, the current top of stack address is stored by $29 (Stack Pointer register (SP)), but in our MIPS assembler, stack pointer is $30. MIPS does not provide push and pop instructions. These have to be done by the programmer CS 230 - Spring 2018 2-8

Convention stack grows from higher memory address to lower Low address Higher address Push values onto stack by subtracting from sp Pop values from stack by adding to sp CS 230 - Spring 2018 2-9

Stack Save/Restore Push (save register to memory) decrement stack pointer to make room copy value to stack pointer memory location addi $30, $30, -4 sw $x, 0($30) Pop (restore register from stack) copy value from stack pointer memory location increment stack pointer to free up space lw $x, 0($30) addi $30, $30, 4 CS 230 - Spring 2018 2-10

Arguments Passing and Return values need to pass arguments and return the result(s) can use registers and/or stack There needs to be agreement between caller and callee on the protocol MIPS convention first 4 arguments in registers remainder on the stack CS 230 - Spring 2018 2-11

MIPS Register Conventions! which registers to save? which registers to use for arguments/results? MIPS $at (1) assembler temporary $v0 - $v1 (2-3) return values $a0 - $a3 (4-7) arguments $t0 - $t9 (8-15, 24, 25) temporary $s0 - $s7 (16-23) saved temporary $k0 - $k1 (26-27) OS kernel $sp - $30 Stack Pointer $ra - $31 Return Address CS 230 - Spring 2018 2-12

MIPS Register Conventions $t0 - $t9 (8-15, 24, 25) temporary: the subroutine you called does NOT promise to preserve the values in these registers. $s0 - $s7 (16-23) saved temporary: the subroutine you called promises that when it gives execution back to the caller these registers will be in the same state they are when the call was made. Achieved by either not touching the registers or by saving them on the stack before using them, then popping off before returning. Simply put, unsaved temporaries if required after subroutine call must be saved by caller, saved temporaries are saved by callee. CS 230 - Spring 2018 2-13

Subroutines nt main() int i, j; i = 5; j = celtokel(i); i = j; return 0;} / Celsius to kelvin nt celtokel(int i) return (i + 273);} addi $17, $0, 5 ;i=5 add $4, $17, $0 ;Argument addi $30, $30, -4 ;sp-4 sw $31, 0($30) ;push ra jal ctok ;jump to c2k add $18, $2, $0 ;return val add $17, $18, $0 ;i=j lw $31, 0($30) ;pop ra addi $30, $30, 4 ;sp+4 jr $31 ;jump to $ra c2k:addi $30, $30, -4 ;sp-4 sw $17, 0($30) ;push s1 val addi $17, $4, 273 ;$4+273 add $2, $17, $0 ;ret val lw $17, 0($30) ;pop s1 val addi $17, $17, 4 ;sp+4 jr $31 ;jump to $ra