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

Similar documents
ECE260: Fundamentals of Computer Engineering

ECE232: Hardware Organization and Design

ECE331: Hardware Organization and Design

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

ECE260: Fundamentals of Computer Engineering

Course Administration

ECE260: Fundamentals of Computer Engineering

Lecture 5: Procedure Calls

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

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

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

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

SPIM Procedure Calls

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

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

Functions in MIPS. Functions in MIPS 1

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Implementing Procedure Calls

Lecture 6: Assembly Programs

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

ECE 30 Introduction to Computer Engineering

Chapter 2A Instructions: Language of the Computer

Storage in Programs. largest. address. address

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

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

Control Instructions

MIPS%Assembly% E155%

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

Functions and Procedures

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

Thomas Polzer Institut für Technische Informatik

EE 361 University of Hawaii Fall

Today. Putting it all together

Instruction Set Architectures (4)

LECTURE 19. Subroutines and Parameter Passing

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

CA Compiler Construction

CSE Lecture In Class Example Handout

MIPS Procedure Calls. Lecture 6 CS301

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

Computer Architecture

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

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

MIPS Instruction Set Architecture (2)

ECE260: Fundamentals of Computer Engineering

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

Machine Instructions - II. Hwansoo Han

Run-time Environment

MIPS Functions and the Runtime Stack

ECE331: Hardware Organization and Design

EE 109 Unit 15 Subroutines and Stacks

Instructions: Assembly Language

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

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

Compiling Code, Procedures and Stacks

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

More C functions and Big Picture [MIPSc Notes]

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

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

CS 61c: Great Ideas in Computer Architecture

ECE260: Fundamentals of Computer Engineering

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

Lecture 7: Procedures and Program Execution Preview

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

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.

CS3350B Computer Architecture MIPS Procedures and Compilation

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

COMPUTER ORGANIZATION AND DESIGN

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

MIPS Functions and Instruction Formats

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

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

M2 Instruction Set Architecture

CS 316: Procedure Calls/Pipelining

Lab 4 Prelab: MIPS Function Calls

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

COE608: Computer Organization and Architecture

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

ECE 30 Introduction to Computer Engineering

Instruction Set Architecture

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

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

Anne Bracy CS 3410 Computer Science Cornell University

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

Lab 4 : MIPS Function Calls

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

CSE Lecture In Class Example Handout

MIPS Assembly (Functions)

CS/COE1541: Introduction to Computer Architecture

instructions aligned is little-endian

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

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

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.

Anne Bracy CS 3410 Computer Science Cornell University

ECE 15B Computer Organization Spring 2010

Transcription:

Supporting Procedures in Computer Hardware James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy

What are Procedures? Come with many different names: Subroutine a repeatable piece of code that you can call by name Procedure a subroutine that doesn t return a value May have side-effects that write to files, print to the screen, modify input values, etc. Function a subroutine that returns one or more output values based on one or more input values Given some input, produces some output (similar to a mathematical function) Method a procedure or function that is executed in the context of an object-oriented class May have side-effects that modify the state of the object 2

Why Use Procedures? Readability Divide up large programs into smaller procedures Reusability Call same procedure from many parts of code Programmers can use each others code Parameterizability Same function can be called with different arguments/parameters at runtime to produce different output or side-effects Polymorphism (in OOP C++/Java/etc.) Behavior can be determined at runtime as opposed to compile time based on object type 3

Why Use Procedures? (continued) Scoping of variable names Use locally scoped variable names that make sense in the context of the procedure No need to keep track of ALL variable names throughout entire program int x = 5; int x1 = 5; int foo(int x) { return x + 1; int foo(int x3) { return x3 + 1; // arg x3 shadows x1 int bar(int y) { int x = 0; return foo(x) + y; int bar(int y) { int x2 = 0; return foo(x2) + y; // local var x2 shadows x1 main() { foo(bar(x)); main() { foo(bar(x1)); // no local x, use x1 4

What s the Procedure? Main idea for procedures Some main code routine M calls a procedure P P does some work, then returns to M Execution in M picks up where it left off prior to calling P i.e., execution continues at the instruction in M after the instruction that called P 5

Using Procedures A calling program (Caller) must: Provide procedure parameters Put the arguments in a place where the procedure can access them Transfer control to the procedure (i.e. jump to the procedure) A called procedure (Callee) must: Acquire the resources (i.e. registers) needed to perform the function Perform the function Place results in a place where the Caller can find them Return control back to the Caller 6

Steps for Calling Procedure on MIPS 1. Place parameters in registers before calling so callee can access them (use $a0 - $a3) 2. Transfer control to procedure using special instruction Use jal instruction it saves a return address more info coming soon 3. Acquire storage resources needed for the procedure Must backup (and later restore) contents of any $sx registers used in procedure $tx registers may be overwritten by subroutines be careful 4. Perform procedure s operations 5. Place result in register where the caller can access it (use $v0 - $v1) 6. Return to place of call Register $ra contains return address 7

MIPS Registers (now with more info!) MIPS architecture has a 32 32-bit register file (e.g. it has 32 32-bit registers) Register Number Register Name Use 0 $zero Constant value 0 1 $at Assembler temporary 2-3 $v0 - $v1 Procedure return values 4-7 $a0 - $a3 Procedure arguments 8-15 $t0 - $t7 Temporary values 16-23 $s0 - $s7 Saved temporary values 24-25 $t8 - $t9 More temporary values 26-27 $k0 - $k1 Reserved for OS 28 $gp Global pointer 29 $sp Stack pointer 30 $fp Frame pointer 31 $ra Return Address Not for you! Callee puts return value here Caller puts arguments here Callee may overwrite these Must be saved by callee if used Callee may overwrite these Also, not for you! Easy access to constants/globals Top of stack Points to local variables on stack Where to go when returning from procedure 8

Another Important CPU Register Program Counter Register that contains the address of the instruction currently being executed Abbreviated as PC for Program Counter Should always point to a memory address in the TEXT segment of memory Side note: common security exploit forces PC to point to point to stack this is BAD Typically increments by 4 after each instruction to point to the next instruction PC = PC + 4 Increments by 4 since instructions are 4 bytes each (32-bits) PC can be set using branch and jump instructions in lieu of typical increment behavior This is how procedures are called 9

Procedure Call Instructions Jump-and-Link (jal) a procedure call instruction Address of instruction immediately after jal is saved in $ra (i.e. $ra = PC + 4) Jumps to target address (e.g. address of ProcedureLabel) jal ProcedureLabel Jump Register (jr) return from a procedure Copies $ra to program counter (i.e. PC = $ra) Can also be used for computed jumps (32-bit jump distance :-) e.g. for case/switch statements jr $ra 10

Leaf Procedure Example A leaf procedure is a procedure that does NOT call another procedure Example C code int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; Assume the following: Parameters g, h, i, j are passed in registers $a0, $a1, $a2, $a3 leaf_example code is called using a jal instruction that sets the $ra register Plan to use register $s0 for local variable f Must place return value in register $v0 when done 11

Leaf Procedure Example (continued) int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; leaf_example: addi $sp, $sp, -4 # plan to use $s0 locally, so make room on stack to save old $s0 sw $s0, 0($sp) # store callers value of $s0 on stack... will be restored later add $t0, $a0, $a1 # $t0 = g + h add $t1, $a2, $a3 # $t1 = i + j sub $s0, $t0, $t1 # f = (g + h) - (i + j)... recall f is stored in $s0 add $v0, $s0, $zero # put f into special return value register $v0 lw $s0, 0($sp) # before returning, restore callers value of $s0 from stack addi $sp, $sp, 4 # shrink stack to previous size (recall increasing $sp shrinks stack) jr $ra # return to caller (instruction after jal instruction that called this) 12

Leaf Procedure Example (continued maybe better) int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; For this example, no real need to use $s0, just put result directly into $v0 and skip the stack manipulation leaf_example: addi $sp, $sp, -4 # plan to use $s0 locally, so make room on stack to save old $s0 sw $s0, 0($sp) # store callers value of $s0 on stack... will be restored later add $t0, $a0, $a1 # $t0 = g + h add $t1, $a2, $a3 # $t1 = i + j sub $v0, $t0, $t1 # f = (g + h) - (i + j)... put directly into $v0 add $v0, $s0, $zero # put f into special return value register $v0 lw $s0, 0($sp) # before returning, restore callers value of $s0 from stack addi $sp, $sp, 4 # shrink stack to previous size (recall increasing $sp shrinks stack) jr $ra # return to caller (instruction after jal instruction that called this) 13