Implementing Functions at the Machine Level

Similar documents
Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines

Midterm 2 Review Chapters 4-16 LC-3

TRAPs and Subroutines. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

System Calls. Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

Chapter 10 And, Finally... The Stack

Homework 06. Push Z Push Y Pop Y Push X Pop X Push W Push V Pop V Push U Pop U Pop W Pop Z Push T Push S

Chapter 10 Memory Model for Program Execution. Problem

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

CSE Lecture In Class Example Handout

Chapter 14 Functions

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Input & Output. Lecture 16. James Goodman (revised by Robert Sheehan) Computer Science 210 s1c Computer Systems 1 Lecture Notes

Scope: Global and Local. Concept of Scope of Variable

Systems I. Machine-Level Programming V: Procedures

LC-3 Subroutines and Traps. (Textbook Chapter 9)"

MIPS Functions and Instruction Formats

CS 2461: Computer Architecture I

CSC 8400: Computer Systems. Using the Stack for Function Calls

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Implementing Procedure Calls

Introduction to Computer Engineering. Chapter 5 The LC-3. Instruction Set Architecture

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

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

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

Subprograms, Subroutines, and Functions

Chapter 5 The LC-3. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2

Instruction Set Architecture

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Computing Layers. Chapter 5 The LC-3

Assembly Language: Function Calls

20/08/14. Computer Systems 1. Instruction Processing: FETCH. Instruction Processing: DECODE

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

LC-3 Instruction Set Architecture. Textbook Chapter 5

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

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.

CSC 2400: Computer Systems. Using the Stack for Function Calls

EEL 5722C Field-Programmable Gate Array Design

CS 2461: Computer Architecture I

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

LC-3 Instruction Set Architecture

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

CSE Lecture In Class Example Handout

CIT Week13 Lecture

Wednesday, October 15, 14. Functions

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Lecture V Toy Hardware and Operating System

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Introduction to Computer. Chapter 5 The LC-3. Instruction Set Architecture

NET3001. Advanced Assembly

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

LC-3 TRAP Routines. Textbook Chapter 9

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

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

That is, we branch to JUMP_HERE, do whatever we need to do, and then branch to JUMP_BACK, which is where we started.

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

COSC121: Computer Systems: Review

CA Compiler Construction

The LC-3 Instruction Set Architecture. ISA Overview Operate instructions Data Movement instructions Control Instructions LC-3 data path

CS429: Computer Organization and Architecture

Anne Bracy CS 3410 Computer Science Cornell University

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Chapter 2A Instructions: Language of the Computer

Run-time Environment

appendix a The LC-3 ISA A.1 Overview

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

And. Fin a l l i... The Slack

G Programming Languages - Fall 2012

THEORY OF COMPILATION

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Computing Layers

10/31/2016. The LC-3 ISA Has Three Kinds of Opcodes. ECE 120: Introduction to Computing. ADD and AND Have Two Addressing Modes

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

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses

Lecture 7: Procedures and Program Execution Preview

This section provides some reminders and some terminology with which you might not be familiar.

Compiling Code, Procedures and Stacks

Anne Bracy CS 3410 Computer Science Cornell University

CSC 2400: Computing Systems. X86 Assembly: Function Calls

COSC121: Computer Systems: Review

CS61C : Machine Structures

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Course Administration

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 8: Virtual Machine II: Program Control

Computer Organization & Assembly Language Programming (CSE 2312)

ECE251: Tuesday September 18

Shift and Rotate Instructions

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Transcription:

Subroutines/Functions Implementing Functions at the Machine Level A subroutine is a program fragment that... Resides in user space (i.e, not in OS) Performs a well-defined task Is invoked (called) by a user program Returns control to the calling program when finished Virtues Reuse useful code without having to keep typing it in (and debugging it!) Divide task among multiple programmers Use vendor-supplied library of useful routines Based on slides McGraw-Hill Additional material 2004/2005 Lewis/Martin Modified by Diana Palsetia CIT 593 1 CIT 593 2 LC3 Opcode for CALLING a Subroutine /R saves the return address in R7 and computes the the starting address of the subroutine and loads it o PC PC-relative mode (just like PC-relative LD/ST) Target address of the subroutine is incremented PC + offset R 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 1 PCoffset11 Base addressing mode Target address of the subroutine is obtained from Base Register R 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 0 0 0 BaseR 0 0 0 0 0 0 CIT 593 3 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 1 PCoffset11 Register File R0 R1 R2 R3 R4 R6 R7 0100000000011001 PC IR 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 1 BR 512 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 9 SEXT Note: This is PC of next instruction B A ADD ALU 1 0 CIT 593 4 Just like JMP (but PC is saved in R7) 0000001000000000 1

Returning From a Subroutine Use Just a special case of JMP i.e. == JMP R7 Note If we use JMP to call subroutine instead of /R, we can t use to return from subroutine! Why not? Example: 2 s complement routine.orig x3000 DoSomething1 ;need to compute R4 = R1 - R3 x3005 ADD R0, R3, #0 ; copy R3 to R0 x3006 TwosComp ; negate x3007 ADD R4, R1, R0 ; add to R1 x3008 BRz DoSomething2 TwosComp NOT R0, R0 ; R0 is the input to the routine ADD R0, R0, #1 ; add one ; return to caller DoSomething2 CIT 593 5 CIT 593 6 R IR R 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 1 0 0 0 0 0 BaseR 0 0 0 0 0 0 R 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 Note: This is PC of next instruction Virtues of R? Register File R0 R1 R2 R3 R4 000001000011000 R6 R7 0100010000011001 PC 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 1 this zero means register mode CIT 593 7 Information regarding Subroutines How to Pass Information To/From? In registers (simple, fast, but limited number) E.g. R0 contains input and the return value is also place in R0 In memory (many, but expensive) This is the stack implementation Both What should the User of the Function know? Address: or at least a label that will be bound to its address In high-level we use the subroutine name Function: what it does NOTE: The programmer does not need to know how the subroutine works, but what changes are visible in the machine s state after the routine has run Arguments: what they are and where they are placed Return values: what they are and where they are placed CIT 593 8 2

Saving and Restoring Registers Remember that any piece of code uses same set of machine registers So we must maain state of machine after the subroutine call Called routine callee-save Before start, save register(s) that will be altered (unless altered value is desired by calling program!) Before return, restore those same register(s) Values are saved by storing them in memory Calling routine caller-save If register value needed later, save register(s) calling the routine This is much harder as you may need to know the ernal workings of a tion By convention, callee-saved Location for argument(s) and return value Caller/Callee must agree on argument & return value location Approach 1 Every subroutine does what it likes Program needs to look at documentation for each one Approach 2 Define a consistent calling convention E.g. LC-3 argument/ret-val location First 4 arguments passed in R0, R1, R2, R3 Single value returned in register other than R7 Subsequent arguments passed in memory CIT 593 9 CIT 593 10 Example of Callee Save for subroutines TwosComp: ;good to save R7 even if u don t use it ST R7, SAVER7;save R7 ST R1, SAVER1;save R1 NOT R0, R0 ;R0; is the input to the routine AND R1, R1, #0 ADD R1, R1, #1 ADD R0, R0,R1 LD R7, SAVER7 ;restore R7 LD R1, SAVER1 SAVER7:.FILL x0000 SAVER1:.FILL x0000 Stack Concept A last-in first-out (LIFO) storage structure The first thing you put in is the last thing you take out The last thing you put in is the first thing you take out Not like an array, where you can access any item based on an index Two operations PUSH: add an item to the stack POP: remove an item from the stack CIT 593 11 CIT 593 12 3

A Physical Stack A Software Stack implemented in Memory Coin holder Initial State 1995 1996 1998 1998 1982 1995 1982 1995 After One Push After Three More Pushes Last quarter in is the first quarter out (LIFO) After One Pop x3ffb x3ffc x3ffd x3ffe x3fff Assume section x3fffb to x3fff in memory used for stack Data items don't move in memory, just our idea about where TOP of the stack is (a register is used to keep track of the location) 12 5 TOP / / / / / 12 5 31 31 TOP 18 TOP 18 18 TOP x4000 R6 x3fff R6 x3ffc R6 x3ffe R6 Initial State After One Push After Three More Pushes After Two Pops By convention, in LC3 R6 holds the Top of Stack (TOS) poer CIT 593 13 CIT 593 14 Basic Push and Pop Code What happens when we run out of space? Push ADD R6, R6, #-1 ; decrement stack ptr STR R0, R6, #0 ; store data contained in R0 Overflow When we have no more locations and we try to push some data on the stack Pop Note LDR R0, R6, #0 ; load data from TOS ADD R6, R6, #1 ; increment stack ptr Stacks can grow in either direction (toward higher address or toward lower addresses) Underflow The stack is empty and we try to pop the data Some how the programmer needs to learn about the overflow/underflow problem Java: Stack Overflow C: Segmentation fault CIT 593 15 CIT 593 4

POP routine w/ Underflow Detection General Implementation POP: LD R1, EMPTY ;R1 = -x4000 ADD R2, R6, R1 ;R6 = Stack Poer BRz Failure ;check whether R6 = x4000 LDR R0, R6, #0 ;POP value of the stack ADD R6, R6, #1 ;update the stack poer Failure: AND,, #0 ADD,, #1 ; = 1 indicates that the POP was ;not successful Empty:.FILL xc000 ; - x4000 (stack limit) MAX Limit ////// / / / / / 8 SP 12 BOTTOM Base E.g. 3 entry stack Some processors use three registers to track: Stack Poer (SP): contains addr of top of stack Stack Base: Contains the addr of the bottom location Stack Limit: contains the addr of the other end of the stack (checks for PUSH error) CIT 593 17 CIT 593 18 Uses of Stack Passing arguments & routines for subroutine calls Instead passing them through registers To perform recursive subroutines Allocating space for local variables inside a subroutine Use as Temporary storage Computers without registers store temporary values during computation on a stack Interrupt I/O More when we do I/O Use: Passing arguments & return value in a subroutine Use the stack for arguments & return values instead of registers Calling routine pushes arguments on the stack and calls the subroutine Called routine pops the passed arguments from the stack and pushes any return value onto the stack Finally, the calling routine then retrieves the return value(s) from the stack and continues execution Why would this be helpful? This is especially helpful if a subroutine has many arguments Also stack is need for recursion CIT 593 19 CIT 593 20 5

Function Call scenario (Recursion Problem) Main... Next... HALT Foo Foo ST R7, SaveR7 AND R0, R0, #0... Foo After... LD R7, SaveR7 Save7.FILL #0 Counter.FILL #0.END First call to Foo SaveR7 contains address of Next Second call to Foo SaveR7 contains address of After First return from Foo Returns to After Second return from Foo Returns to After again!!! CIT 593 21 Solution to Recursion problem For each subroutine call, need a mechanism to distinguish its invocation This is known as activation record Activation Record contains Invocation-specific data (e.g., local variables, saved registers, arguments and returns) Need to store this information per tion call and discard when the tion call is over Stack data structure fits this description well When tion is called we store (push) record on the stack When tion call is over we discard (pop) record from the stack CIT 593 22 Big Picture Complete view of an activation record Memory Memory Memory Activation Record of a called tion: 1. input parameters passed & return value Local var 2. book keeping information E.g. Callers Return address Frame Poer Book keeping Before call During call After call 3. local variables Args + return value Stack Frame/Record CIT 593 23 CIT 593 24 6

Frame Poer Each tion gets a record (a.k.a frame), but we need to somehow Delineate one tion s record from other, especially if we are in a recursive call Track within an activation record the local variables, book keeping info, and arguments This tracking is done by what is called the frame poer Frame poer is the address which pos to the top of the record/frame Once we know the frame poer, all information can be accessed via frame poer + offset Note: By convention in LC3, holds frame poer Activation Record Arguments The calling routine places the arguments on top of the stack before calling a tion The callee will use the arguments from the stack to do its work Book keeping Callers Frame Poer Need to save callers frame poer so that when the control returns to the caller, it will be able to access its own local variable, arguments etc. If we destroy this value then we have trouble restarting the caller correctly when the callee finishes CIT 593 25 CIT 593 26 Activation Record contd.. Book Keeping Record 2. Return address Save poer to next instruction in calling tion Convenient location to store R7 Especially helpful during recursive calls 3. Saved Registers Save all registers that will be used for temporary work in the tion 4. Returns Every tion always allocates a space for return value on the activation record, whether or not it returns anything Even if the tion return is void CIT 593 27 Activation Record contd.. Local Variables Local Variables are added after arguments and book keeping information They are added in the order they are declared Therefore e e variable ab declared ed last is always ays on the top of the stack (LIFO) Compiler tracks each variable Just like assembler, maains symbol table for labels Compiler stores for very variable 1.Name (identifier) 2.Type Name Type Offset Scope 3.Location in memory 4.Scope Compiler only stores offset and uses as the base address CIT 593 28 7

Example local variables of () () LC3 initialization code: { ADD R0, R0, #0 r = 0 double amt = 0; t = 0 STR R0,, #3 p = 0 p = 0; STR R0,, #2 amt = 0 t = 0; r = 0; STR R0,, #1 STR R0,, #0... amt = p * t * r; Compiler s Symbol Table We know that contains the frame Name Type Offset Scope poer of tion amt double 3 (). Now variables in can accessed p 2 t 1 by : r 0 Frame Poer + Offset CIT 593 29 Example local variables of () (contd..) To calculate amt, we can now easily access local variables p,t, and r using the frame poer Assigning amt = p * t * r ; LDR R1,, #2 ;R1 = p LDR R2,, #1 ; R2 = t; LDR R3,, #0 ; R3 = r MUL R1, R1, R2 ; R1 = p * t MUL R1, R1, R3 ; R1 = p * t * r STR R1,, #3 ; Store R1 = amt Note: Reserved Opcode is used to create MUL instruction Compiler s Symbol Table Name Type Offset Scope amt p t r double 3 2 1 0 r t p amt Note: (explanation deviates from book CIT 593 section 12.5) 30 Example of a Complete Activation Record ( a, b) { w, x, y;... return y; bookkeeping Name Type Offset Scope b a ret. value w x y 7 6 5 2 1 0 y x locals w Callers Frame Poer return addr. (R7) return value a args b a & b were placed on the stack by caller () { x, y, val; x = 10; y = 11; val = max(x + 10, y); prf( %d, val); return 0; max( a, b) { result = 0; if (b > a) { result = b; else { result = a; return result; Function Call Example s view result Caller s FP () save R0 save R1 save R7 max s return value a b val y x s return value max s view CIT 593 31 CIT 593 32 8

Other Use: Non-Register Machines LC3 three address machine because it specifies all 3 locations (i.e. 1 Dest and 2 Src). E.g. ADD R1, R2, R1 Some ISAs, are zero address machines or stack machine They use stack for Dst and Src operands and the instruction does not explicitly specify them In an all memory machine, some location in memory holds the address of the top of the stack The address stored is called Stack Poer (SP) PUSH Instruction Example: Zero Address ISA PUSH address Adjusts the stack poer (ernally done), and pushes value onto the stack Programmer can only read the stack poer (just like in LC3 the programmer can read the condition code registers). POP Is similar to PUSH, except that value is removed from top of the stack and put back o memory location other than stack space. (the location is specified by the address field) ADD Instruction ADD 0 0 Takes the two values out the stack (2 POP instructions) Then gives that to ALU to compute the result The result is PUSHed back on the stack CIT 593 33 CIT 593 34 Stack Machine: Multiply Add Example Want to compute E = (A + B).(C + D). Let say A = 25, B = 17, C = 3 and D = 2 Stack Implementation Register Implementation Push A (take a data from memory and push it on to stack section) Push B Add (2 POP s, so that operands can be given to ALU, and then result PUSHed backed on stack) Push C Push D Add Mult POP E (pop it off stack and store somewhere in memory) LD R0, A LD R1,B ADD R0, R0, R1 LD R2, C LD R3, D ADD R2, R2,R3 MUL R0, R0, R2 ST R0, E Does everyone see the usefulness of register file?? CIT 593 35 9