ECE232: Hardware Organization and Design

Similar documents
ECE331: Hardware Organization and Design

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls

Lecture 6: Assembly Programs

ECE331: Hardware Organization and Design

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

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

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

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

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

Course Administration

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

ECE260: Fundamentals of Computer Engineering

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

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

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

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

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

ECE260: Fundamentals of Computer Engineering

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

Control Instructions

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

Functions in MIPS. Functions in MIPS 1

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

ECE 30 Introduction to Computer Engineering

ECE260: Fundamentals of Computer Engineering

CSE Lecture In Class Example Handout

SPIM Procedure Calls

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

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

CA Compiler Construction

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

Chapter 2A Instructions: Language of the Computer

CS64 Week 5 Lecture 1. Kyle Dewey

Machine Instructions - II. Hwansoo Han

Instruction Set Architectures (4)

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

Today. Putting it all together

MIPS Procedure Calls. Lecture 6 CS301

CS 316: Procedure Calls/Pipelining

CS3350B Computer Architecture MIPS Procedures and Compilation

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

MIPS Instruction Set Architecture (2)

Compiling Techniques

COMPUTER ORGANIZATION AND DESIGN

Procedure Call and Return Procedure call

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

Thomas Polzer Institut für Technische Informatik

Implementing Procedure Calls

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

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

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

Computer Architecture

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

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

Procedure Call Instructions

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.

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

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

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

MIPS function continued

Functions and Procedures

ECE 15B Computer Organization Spring 2010

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

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.

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

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

MIPS Assembly (Functions)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

EE 361 University of Hawaii Fall

MIPS Functions and the Runtime Stack

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

Lecture 7: Procedures

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.

MIPS%Assembly% E155%

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

Instructions: Assembly Language

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

Lab 4 Prelab: MIPS Function Calls

Common Problems on Homework

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

Chapter 2. Instructions: Language of the Computer

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

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

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

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Lecture 7: Procedures and Program Execution Preview

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

MIPS Functions and Instruction Formats

Run-time Environment

UCB CS61C : Machine Structures

Compiling Code, Procedures and Stacks

CS 61c: Great Ideas in Computer Architecture

Transcription:

ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB

Overview Procedures have different names in different languages Java: methods C: procedures FORTRAN: subroutines Control flow passes to a new section of code, then returns to after the call MIPs includes registers to support data structures for procedures Goal: Understand what happens when a procedure is called/returns ECE232: Procedures 2

Software Conventions for MIPS Registers Register $0 $1 $2 - $3 $4 - $7 $8 - $15 $16 - $23 $24 - $25 $26 - $27 $28 $29 $30 $31 $hi $lo Names $zero $at $v0 - $v1 $a0 - $a3 $t0 - $t7 $s0 - $s7 $t8 - $t9 $k0 - $k1 $gp $sp $fp $ra $hi $lo Hardwired to zero Reserved by assembler Function return result registers Function passing argument value registers Temporary registers, caller saved Saved registers, callee saved Temporary registers, caller saved Reserved for OS kernel Global pointer Stack pointer Frame pointer Usage by Software Convention Return address (pushed by call instruction) High result register (remainder/div, high word/mult) Low result register (quotient/div, low word/mult) ECE232: Procedures 3

Leaf Procedure Example C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } MIPs assumes that arguments g,, j are in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 ECE232: Procedures 4

Procedure Calling Steps required 1. Place parameters in registers 2. Transfer control to procedure 3. Acquire storage for procedure 4. Perform procedure s operations 5. Place result in register for caller 6. Return to place of call ECE232: Procedures 5

Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return ECE232: Procedures 6

Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call ECE232: Procedures 7

Procedure Call Instructions Procedure call: jump and link jal ProcedureLabel Address of following instruction put in $ra Jumps to target address Procedure return: jump register jr $ra Copies $ra to program counter Can also be used for computed jumps e.g., for case/switch statements ECE232: Procedures 8

Jump-and-Link A special register (storage not part of the register file) maintains the address of the instruction currently being executed this is the program counter (PC) The procedure call is executed by invoking the jump-and-link (jal) instruction the current PC (actually, PC+4) is saved in the register $ra and we jump to the procedure s address (the PC is accordingly set to this address) jal NewProcedureAddress Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction How do we return control back to the caller after completing the callee procedure? ECE232: Procedures 9

The Stack The register scratchpad for a procedure seems volatile It seems to disappear every time we switch procedures A procedure s values are therefore backed up in memory on a stack Proc A s values High address Proc A Proc B s values Proc C s values Stack grows this way Low address return call Proc B call Proc C return return ECE232: Procedures 10

What values are saved? Preserved Saved registers: $s0-$s7 Stack Pointer: $sp Return Address Register; $ra Stack above the stack pointer Not Preserved Temporary registers: $t0-$t9 Argument registers: $a0-$a3 Return registers: $v0-$v1 Stack below the pointer $sp 7fff fffc Stack Dynamic Data pc 0040 0000 0 Static Data Text Reserved ECE232: Procedures 11

Storage Management on a Call/Return A new procedure must create space for all its variables on the stack Arguments are copied into $a0-$a3; the jal is executed After the callee creates stack space, it updates the value of $sp Once the callee finishes, it copies the return value into $v0, frees up stack space, and $sp is incremented The responsibility for copies between stack and registers may fall upon either the caller or the callee ECE232: Procedures 12

Example string copy Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i])!= `\0 ) i += 1; } Base addresses for x and y in $a0 and $a1, respectively $s0 is used for i ECE232: Procedures 13 strcpy: addi $sp, $sp, -8 sw $s0, 0($sp) sw $ra, 4($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra

Customs for function calls in MIPS Steps for the code that is calling a function: Caller Before Entry: Step 1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack Step 2: Execute a jal instruction Jump and Link will save your return address in the $ra register, so that you can return to where you started After Return: Step 1: Get result from $v0, $v1 ECE232: Procedures 14

Customs for function calls in MIPS Steps for the code that is called : Callee On Entry: Step 1: Save callee-saved registers on stack Save registers $s0-$s7 if they are used in the callee procedure Save $ra register Before Exit: Step 1: Save return values in $v0-$v1 registers Step 2: Restore $s0-$s7 registers, if they were saved Step 3: Restore $ra register Step 4: Execute jr $ra ECE232: Procedures 15

Summary Important to understand what happens in MIPs when a procedure is called Information stored on stack, control jumps made MIPs supports recursion All microprocessors support similar actions ECE232: Procedures 16