Implementing Functions at the Machine Level

Size: px
Start display at page:

Download "Implementing Functions at the Machine Level"

Transcription

1 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 CIT 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 PCoffset11 Base addressing mode Target address of the subroutine is obtained from Base Register R BaseR CIT PCoffset11 Register File R0 R1 R2 R3 R4 R6 R PC IR BR SEXT Note: This is PC of next instruction B A ADD ALU 1 0 CIT Just like JMP (but PC is saved in R7)

2 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 CIT R IR R BaseR R Note: This is PC of next instruction Virtues of R? Register File R0 R1 R2 R3 R R6 R PC this zero means register mode CIT 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

3 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 CIT 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 CIT

4 A Physical Stack A Software Stack implemented in Memory Coin holder Initial State 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 / / / / / TOP 18 TOP 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 CIT 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 CIT 593 4

5 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 CIT 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 CIT

6 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 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 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 CIT

7 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 CIT 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 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

8 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 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 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 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 CIT

9 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 CIT 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

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

Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May Computer Science 210 s1c Computer Systems 1 2010 Semester 1 Lecture Notes Lecture 19, 26Apr10: Subroutines & Traps James Goodman! Announcements New due date for assignment 2 5pm Wednesday, 5May Test is

More information

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

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C Chapter 4 Functions Based on slides McGraw-Hill Additional material 4/5 Lewis/Martin Function Smaller, simpler, subcomponent of program Provides abstraction Hide low-level details Give high-level structure

More information

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

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University Chapter 10 And, Finally... The Stack ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 2 Stack: An Abstract Data Type An important abstraction that

More information

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

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack LC-3 Data Structures Textbook chapter 0 CMPE2 Summer 2008 Abstract data structures are LC-3 data structures Defined by the rules for inserting and extracting data In this section, we will talk about The

More information

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines Chapter 9 TRAP Routines and Subroutines System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device registers and the sequence of operations needed to

More information

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines Chapter 9 TRAP Routines and Subroutines ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2 System Calls Certain operations require specialized knowledge

More information

Midterm 2 Review Chapters 4-16 LC-3

Midterm 2 Review Chapters 4-16 LC-3 Midterm 2 Review Chapters 4-16 LC-3 ISA You will be allowed to use the one page summary. 8-2 LC-3 Overview: Instruction Set Opcodes 15 opcodes Operate instructions: ADD, AND, NOT Data movement instructions:

More information

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

TRAPs and Subroutines. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell TRAPs and Subroutines University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell System Calls Certain operations require specialized knowledge and protection: specific knowledge

More information

System Calls. Chapter 9 TRAP Routines and Subroutines

System Calls. Chapter 9 TRAP Routines and Subroutines System Calls Chapter 9 TRAP Routines and Subroutines Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Certain operations require

More information

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines Chapter 9 TRAP Routines and Subroutines System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device registers and the sequence of operations needed to

More information

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

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality Problem Chapter 10 Memory Model for Program Execution Original slides by Chris Wilcox, Colorado State University How do we allocate memory during the execution of a program written in C?! Programs need

More information

Chapter 10 And, Finally... The Stack

Chapter 10 And, Finally... The Stack Chapter 10 And, Finally... The Stack Memory Usage Instructions are stored in code segment Global data is stored in data segment Local variables, including arryas, uses stack Dynamically allocated memory

More information

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

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 Homework 06 1. How many TRAP service routines can be implemented in the LC-3? Why? Since the trap vector is 8 bits wide, 256 trap routines can be implemented in the LC-3. Why must a instruction be used

More information

Chapter 10 Memory Model for Program Execution. Problem

Chapter 10 Memory Model for Program Execution. Problem Chapter 10 Memory Model for Program Execution Original slides by Chris Wilcox, Colorado State University Problem How do we allocate memory during the execution of a program written in C?! Programs need

More information

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} 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

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

Chapter 14 Functions

Chapter 14 Functions Chapter 14 Functions Function Smaller, simpler, subcomponent of program Provides abstraction hide low-level details give high-level structure to program, easier to understand overall program flow enables

More information

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

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison Introduction to Computer Engineering CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison Chapter 7 & 9.2 Assembly Language and Subroutines Human-Readable

More information

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

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming The Stack Abstraction Conventions Provide Implicit Information What does

More information

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

Input & Output. Lecture 16. James Goodman (revised by Robert Sheehan) Computer Science 210 s1c Computer Systems 1 Lecture Notes Computer Science 210 s1c Computer Systems 1 Lecture Notes Lecture 16 Input & Output James Goodman (revised by Robert Sheehan) Credits: Slides prepared by Gregory T. Byrd, North Carolina State University

More information

Scope: Global and Local. Concept of Scope of Variable

Scope: Global and Local. Concept of Scope of Variable Concept of Scope of Variable : Computer Architecture I Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs135/ In assembly, who has access to a memory location/variable?

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

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

LC-3 Subroutines and Traps. (Textbook Chapter 9) LC-3 Subroutines and Traps (Textbook Chapter 9)" Subroutines" Blocks can be encoded as subroutines" A subroutine is a program fragment that:" lives in user space" performs a well-defined task" is invoked

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Protecting System space : Computer Architecture I Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs2461/ System calls go to specific locations in memory

More information

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

CSC 8400: Computer Systems. Using the Stack for Function Calls CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

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?

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? Memory Allocation in C Functions in C Global data pointer: R4 Global and static variables Specify positive offsets Frame pointer: Points to current code block Negative offset Stack Pointer: Top of stack

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

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

Introduction to Computer Engineering. Chapter 5 The LC-3. Instruction Set Architecture Introduction to Computer Engineering CS/ECE 252, Spring 200 Prof. David A. Wood Computer Sciences Department University of Wisconsin Madison Chapter 5 The LC-3 Adapted from Prof. Mark Hill s slides Instruction

More information

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

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6 Problem Solving in C Stepwise Refinement as covered in Chapter 6...but can stop refining at a higher level of abstraction. Same basic constructs Sequential -- C statements Conditional -- if-else, switch

More information

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

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 Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

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

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Let s Act Like Compilers! Let s have some fun! Let s pretend to be a C compiler!

More information

Subprograms, Subroutines, and Functions

Subprograms, Subroutines, and Functions Subprograms, Subroutines, and Functions Subprograms are also called subroutines, functions, procedures and methods. A function is just a subprogram that returns a value; say Y = SIN(X). In general, the

More information

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

Chapter 5 The LC-3. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2 Chapter 5 The LC-3 ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2 Instruction Set Architecture ISA = All of the programmer-visible components

More information

Instruction Set Architecture

Instruction Set Architecture Chapter 5 The LC-3 Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization address space -- how may locations can be addressed? addressibility

More information

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

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program Fall 2006 CS333: Computer Architecture University of Virginia Computer Science Michele Co Announcements Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly Homework #2 Due next Wednesday, Sept.

More information

Computing Layers. Chapter 5 The LC-3

Computing Layers. Chapter 5 The LC-3 Computing Layers Problems Chapter 5 The LC-3 Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Algorithms Language Instruction

More information

Assembly Language: Function Calls

Assembly Language: Function Calls Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent instructions and conventions 2 Function Call Problems (1) Calling and returning

More information

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

20/08/14. Computer Systems 1. Instruction Processing: FETCH. Instruction Processing: DECODE Computer Science 210 Computer Systems 1 Lecture 11 The Instruction Cycle Ch. 5: The LC-3 ISA Credits: McGraw-Hill slides prepared by Gregory T. Byrd, North Carolina State University Instruction Processing:

More information

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

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

LC-3 Instruction Set Architecture. Textbook Chapter 5

LC-3 Instruction Set Architecture. Textbook Chapter 5 LC-3 Instruction Set Architecture Textbook Chapter 5 Instruction set architecture What is an instruction set architecture (ISA)? It is all of the programmer-visible components and operations of the computer

More information

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

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison Introduction to Computer Engineering CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison Chapter 5 The LC-3 Instruction Set Architecture ISA = All of the

More information

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.

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. 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.5 6 compute jump/branch targets memory PC +4 new pc Instruction Fetch

More information

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

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

EEL 5722C Field-Programmable Gate Array Design

EEL 5722C Field-Programmable Gate Array Design EEL 5722C Field-Programmable Gate Array Design Lecture 12: Pipelined Processor Design and Implementation Prof. Mingjie Lin Patt and Patel: Intro. to Computing System * Stanford EE271 notes 1 Instruction

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Computer Architecture is... CS 2461: Computer Architecture I Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs2461/ Instruction Set Architecture Organization

More information

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

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 ECE220: Computer Systems and Programming Spring 2018 Honors Section Machine Problem 11 due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler This assignment requires you to use recursion

More information

LC-3 Instruction Set Architecture

LC-3 Instruction Set Architecture CMPE12 Notes LC-3 Instruction Set Architecture (Textbookʼs Chapter 5 and 6)# Instruction Set Architecture# ISA is all of the programmer-visible components and operations of the computer.# memory organization#

More information

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

More information

Wednesday, October 15, 14. Functions

Wednesday, October 15, 14. Functions Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:

More information

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 Introduction to Computer Engineering CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison Chapter 7 & 9.2 Assembly Language and Subroutines Human-Readable Machine

More information

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 Introduction to Computer Engineering CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison Chapter 5 The LC-3 Announcements Homework 3 due today No class on Monday

More information

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

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 Mechanisms in Procedures Passing Control

More information

Lecture V Toy Hardware and Operating System

Lecture V Toy Hardware and Operating System 2. THE Machine Lecture V Page 1 Lecture V Toy Hardware and Operating System 1. Introduction For use in our OS projects, we introduce THE Machine where THE is an acronym 1 for Toy HardwarE. We also introduce

More information

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

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15 Stack Frames Geoffrey Brown Bryce Himebaugh Indiana University September 2, 2016 Geoffrey Brown, Bryce Himebaugh 2015 September 2, 2016 1 / 15 Outline Preserving Registers Saving and Restoring Registers

More information

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

Introduction to Computer. Chapter 5 The LC-3. Instruction Set Architecture Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin Madison Chapter 5 The LC-3 Instruction Set Architecture

More information

NET3001. Advanced Assembly

NET3001. Advanced Assembly NET3001 Advanced Assembly Arrays and Indexing supposed we have an array of 16 bytes at 0x0800.0100 write a program that determines if the array contains the byte '0x12' set r0=1 if the byte is found plan:

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 4 LAST TIME Enhanced our processor design in several ways Added branching support Allows programs where work is proportional to the input values

More information

LC-3 TRAP Routines. Textbook Chapter 9

LC-3 TRAP Routines. Textbook Chapter 9 LC-3 TRAP Routines Textbook Chapter 9 System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device registers and the sequence of operations needed to use

More information

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 Introduction to Computer Engineering CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison Chapter 8 & 9.1 I/O and Traps Aside Off-Topic: Memory Hierarchy 8-3

More information

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

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design Subroutines and Stack Usage on the MicroBlaze ECE 3534 Microprocessor System Design 1 MicroBlaze Subroutines Same idea as a C/C++ function There s no call instruction Instead, branch and link Example:

More information

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

That is, we branch to JUMP_HERE, do whatever we need to do, and then branch to JUMP_BACK, which is where we started. CIT 593 Intro to Computer Systems Lecture #11 (10/11/12) Previously we saw the JMP and BR instructions. Both are one-way jumps : there is no way back to the code from where you jumped, so you just go in

More information

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

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON Professor Guri Sohi TAs: Newsha Ardalani and Rebecca Lam Examination 4 In Class (50 minutes) Wednesday, Dec 14, 2011 Weight:

More information

COSC121: Computer Systems: Review

COSC121: Computer Systems: Review COSC121: Computer Systems: Review Jeremy Bolton, PhD Assistant Teaching Professor Constructed using materials: - Patt and Patel Introduction to Computing Systems (2nd) - Patterson and Hennessy Computer

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

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

The LC-3 Instruction Set Architecture. ISA Overview Operate instructions Data Movement instructions Control Instructions LC-3 data path Chapter 5 The LC-3 Instruction Set Architecture ISA Overview Operate instructions Data Movement instructions Control Instructions LC-3 data path A specific ISA: The LC-3 We have: Reviewed data encoding

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: February 28, 2018 at 06:32 CS429 Slideset 9: 1 Mechanisms in Procedures

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

appendix a The LC-3 ISA A.1 Overview

appendix a The LC-3 ISA A.1 Overview A.1 Overview The Instruction Set Architecture (ISA) of the LC-3 is defined as follows: Memory address space 16 bits, corresponding to 2 16 locations, each containing one word (16 bits). Addresses are numbered

More information

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

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

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

And. Fin a l l i... The Slack chapter 10 And. Fin a l l i... The Slack We have finished our treatment of the LC-3 ISA. Before moving up another level of abstraction in Chapter 11 to programming in C, there is a particularly important

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 10 Activation Records THEORY OF COMPILATION EranYahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec10.pptx Reference: Dragon 7.1,7.2. MCD 6.3,6.4.2 1 You are here Compiler txt Source Lexical

More information

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

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Computing Layers Chapter 5 The LC-3 Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, S. Rajopadhye Colorado State University Computing Layers Problems Algorithms Language

More information

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

10/31/2016. The LC-3 ISA Has Three Kinds of Opcodes. ECE 120: Introduction to Computing. ADD and AND Have Two Addressing Modes University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 120: Introduction to Computing The LC-3 Instruction Set Architecture The LC-3 ISA Has Three Kinds of Opcodes

More information

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

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

More information

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

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses Chapter 1 The LC-3b ISA 1.1 Overview The Instruction Set Architecture (ISA) of the LC-3b is defined as follows: Memory address space 16 bits, corresponding to 2 16 locations, each containing one byte (8

More information

Lecture 7: Procedures and Program Execution Preview

Lecture 7: Procedures and Program Execution Preview Lecture 7: Procedures and Program Execution Preview CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University of

More information

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

This section provides some reminders and some terminology with which you might not be familiar. Chapter 3: Functions 3.1 Introduction The previous chapter assumed that all of your Bali code would be written inside a sole main function. But, as you have learned from previous programming courses, modularizing

More information

Compiling Code, Procedures and Stacks

Compiling Code, Procedures and Stacks Compiling Code, Procedures and Stacks L03-1 RISC-V Recap Computational Instructions executed by ALU Register-Register: op dest, src1, src2 Register-Immediate: op dest, src1, const Control flow instructions

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. compute jump/branch targets

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the

More information

COSC121: Computer Systems: Review

COSC121: Computer Systems: Review COSC121: Computer Systems: Review Jeremy Bolton, PhD Assistant Teaching Professor Constructed using materials: - Patt and Patel Introduction to Computing Systems (2nd) - Patterson and Hennessy Computer

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #9: MIPS Procedures 2006-07-11 CS 61C L09 MIPS Procedures (1) Andy Carle C functions main() { int i,j,k,m;... i = mult(j,k);... m =

More information

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

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Stack Frames Revisited Recall Why ISAs Define Calling Conventions A compiler

More information

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

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

Course Administration

Course Administration Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

Chapter 8: Virtual Machine II: Program Control

Chapter 8: Virtual Machine II: Program Control Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 Chapter 8: Virtual Machine II: Program Control www.idc.ac.il/tecs Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken

More information

Computer Organization & Assembly Language Programming (CSE 2312)

Computer Organization & Assembly Language Programming (CSE 2312) Computer Organization & Assembly Language Programming (CSE 2312) Lecture 16: Processor Pipeline Introduction and Debugging with GDB Taylor Johnson Announcements and Outline Homework 5 due today Know how

More information

ECE251: Tuesday September 18

ECE251: Tuesday September 18 ECE251: Tuesday September 18 Subroutine Parameter Passing (Important) Allocating Memory in Subroutines (Important) Recursive Subroutines (Good to know) Debugging Hints Programming Hints Preview of I/O

More information

Shift and Rotate Instructions

Shift and Rotate Instructions Shift and Rotate Instructions Shift and rotate instructions facilitate manipulations of data (that is, modifying part of a 32-bit data word). Such operations might include: Re-arrangement of bytes in a

More information

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

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for? Lecture 15: The Stack The Stack What is it? What is it used for? A special memory buffer (outside the CPU) used as a temporary holding area for addresses and data The stack is in the stack segment. The

More information

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

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8 Objectives ICT106 Fundamentals of Computer Systems Topic 8 Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8 To understand how HLL procedures/functions are actually implemented

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information