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

Size: px
Start display at page:

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

Transcription

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

2 Outline Preserving Registers Saving and Restoring Registers ABI Rules for Caller and Callee Saved Registers Stack Frames Access Within the Stack Parameter Passing Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

3 Recursive Procedure Example As an example, consider a simple recursive function that counts the number of 1 s in an unsigned integer parameter: int ones( unsigned int i) { // if operand is zero, return 0 if (i) { // bit 0 plus recursive call return (i & 1) + ones(i >> 1); } else { return 0; } } This procedure needs the value of i both before and after the recursive call, therefore, it must move i to a callee saved register (eg r4) Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

4 Preserving r4 and lr Figure 91 The ones implementation uses r4 to store i lr contains the return address Both must be pushed onto the stack before they are modified Stack before calling function lr sp r4 Stack grows down Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

5 Push ex:push The push operation can write one or more registers to the stack push write lr and r4 to the stack PRE r4 = 0x lr = 0x sp = 0x Address Data (PRE) Data (POST) 0x x x (pre) 0x x x x80010 (empty) 0x (post) 0x8000C (empty) 0x POST r4 = 0x lr = 0x sp = 0x c Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

6 Pop Example: 92 The pop operation can read one or more registers from the stack stack pop {r4,pc} PRE r4 = 0x pc = 0x sp = 0x C Address Data 0x x (post) 0x x x x (pre) 0x8000C read pc and r4 from the stack POST r4 = 0x pc = 0x sp = 0x Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

7 Skeleton for ones procedure ones: push {r4, save registers mov r4, move argument to callee saved register bl recursive call pop {r4, restore r4 and return Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

8 Core Registers and AAPCS Usage Table 91 Register Special Role in Procedure Call Standard r15 pc The Program Counter r14 lr The Link Register r13 sp The Stack Register r12 ip The Intra-Procedure-call scratch register r11 Variable-register 8 r10 Variable-register 7 r9 Platform register (Variable-register 6) r8 Variable-register 5 r7 Variable-register 4 r6 Variable-register 3 r5 Variable-register 4 r4 Variable-register 1 r3 Argument/scratch register 4 r2 Argument/scratch register 3 r1 Argument/scratch register 2 r0 Argument/scratch register 1 Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

9 Roles of Registers The first four registers r0-r3 are used to pass arguments into a subroutine and return a result from a function They may also be used to hold intermediate values (between subroutine calls) Register r12 (ip) is used by the linker to implement long function calls, it may also be used to hold intermediate values Registers r4-r8 and r10-r11 may be used by a subroutine to hold its local values r9 may have a platform specific use, otherwise it may be used to hold local values A subroutine (callee) must preserve the contents of registers r4-r8, r9, and r10-r11 Thus, the caller saved registers are r0-r3 and r12 All the other registers below r13 are callee saved The roles of r13-r15 are defined Geoffrey Brown, Bryce Himebaugh 2015 Preserving Registers September 2, / 15

10 Stack Layout Figure 92 Stack before calling function Stack after calling function Parameter area Caller Parameter area Caller SP Stack grows down Saved link register(lr) Callee saved registers Local storage Callee SP Stack grows down Geoffrey Brown, Bryce Himebaugh 2015 Stack Frames September 2, / 15

11 Stack Frame Example Example 93 extern foo( int *); int alloc( int i) { int local[15]; return i + foo( local); } prolog push {r4, save sub sp, allocate body movs r4, save i add r0, sp, compute pointer bl foo adds r0, epilog add sp, deallocate array pop {r4, restore Geoffrey Brown, Bryce Himebaugh 2015 Stack Frames September 2, / 15

12 Accessing Words in the Stack ldr rt, [sp, rt = *( sp+( imm8 <<2)) str rn, [sp, *( sp+( imm8 <<2)) imm8 is an 8-bit unsigned constant which must be multiple of 4 the assembler accepts values 0,4,1024 and stores the appropriately shifted value in the instruction word Because the stack groes downward, all active stack storage is at a positive offset from the stack pointer Notice that ldr reads from the pointer computed by adding an offset to sp and str writes from a pointer Geoffrey Brown, Bryce Himebaugh 2015 Access Within the Stack September 2, / 15

13 Accessing Local Variables Example 94 extern void foo( int *); int local( int i) { int l = 3; foo(&l); return i + l; } local: push {r4, save sub sp, allocate space movs r4, r4 = i add r0, sp, r0 = &l movs r3, r3 = 3 str r3, *(sp + 4) = r3 bl foo ldr r3, r3 = *( sp + 4) adds r0, r4, retval = i + l add sp, deallocate space pop {r4, restore Geoffrey Brown, Bryce Himebaugh 2015 Access Within the Stack September 2, / 15

14 Parameter Stack Figure 93 Stack after calling function Slot 5 Parameter area Caller Slot 4 r3 Slot 3 Saved link register(lr) r2 r1 Slot 2 Slot 1 Callee saved registers Local storage Callee r0 Slot 0 sp Stack grows down Geoffrey Brown, Bryce Himebaugh 2015 Parameter Passing September 2, / 15

15 Accessing Parameters in the Stack Example 95 extern int foo(int,int,int, int); int call( int a, int b, int c, int d, int e) { return a + e + foo(a,b,c,d); } call: push {r4, save ldr r4, [sp, r4 = e adds r4, r4 += a bl foo adds r0, r4 pop {r4, restore Geoffrey Brown, Bryce Himebaugh 2015 Parameter Passing September 2, / 15

Implementing Subroutines. Outline [1]

Implementing Subroutines. Outline [1] Implementing Subroutines In Text: Chapter 9 Outline [1] General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

More information

Control Flow. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 21

Control Flow. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 21 Control Flow Geoffrey Brown Bryce Himebaugh Indiana University September 2, 2016 Geoffrey Brown, Bryce Himebaugh 2015 September 2, 2016 1 / 21 Outline Condition Codes C Relational Operations C Logical

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

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

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones CprE 288 Introduction to Embedded Systems Course Review for Exam 3 Instructors: Dr. Phillip Jones 1 Announcements Exam 3: See course website for day/time. Exam 3 location: Our regular classroom Allowed

More information

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19 Data Storage Geoffrey Brown Bryce Himebaugh Indiana University August 9, 2016 Geoffrey Brown, Bryce Himebaugh 2015 August 9, 2016 1 / 19 Outline Bits, Bytes, Words Word Size Byte Addressable Memory Byte

More information

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return Outline Implementing Subprograms In Text: Chapter 10 General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

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

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines M J Brockway February 13, 2016 The Cortex-M4 Stack SP The subroutine stack is full, descending It grows downwards from higher

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

Systems Architecture The Stack and Subroutines

Systems Architecture The Stack and Subroutines Systems Architecture The Stack and Subroutines The Stack p. 1/9 The Subroutine Allow re-use of code Write (and debug) code once, use it many times A subroutine is called Subroutine will return on completion

More information

ECE251: Tuesday September 12

ECE251: Tuesday September 12 ECE251: Tuesday September 12 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab

More information

Stacks and Subroutines

Stacks and Subroutines Chapters 8 Stacks and Subroutines Embedded Systems with ARM Cortext-M Updated: Tuesday, March 6, 2018 Basic Idea llarge programs are hard to handle lwe can break them to smaller programs lthey are called

More information

CMPSCI 201 Fall 2006 Midterm #2 November 20, 2006 SOLUTION KEY

CMPSCI 201 Fall 2006 Midterm #2 November 20, 2006 SOLUTION KEY CMPSCI 201 Fall 2006 Midterm #2 November 20, 2006 SOLUTION KEY Professor William T. Verts 10 Points Trace the following circuit, called a demultiplexer, and show its outputs for all possible inputs.

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

CS A331 Programming Language Concepts

CS A331 Programming Language Concepts CS A331 Programming Language Concepts Lecture 9 Subroutines March 17, 2014 Sam Siewert Purpose of a Subroutine Control Abstraction perform a well defined operation E.g. transform a color image into a graymap

More information

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins Programming the ARM Computer Design 2002, Lecture 4 Robert Mullins 2 Quick Recap The Control Flow Model Ordered list of instructions, fetch/execute, PC Instruction Set Architectures Types of internal storage

More information

ECE251: Tuesday September 11

ECE251: Tuesday September 11 ECE251: Tuesday September 11 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab

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

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

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu School of Engineering Brown University Spring 2016 1 ISA is the HW/SW

More information

button.c The little button that wouldn t

button.c The little button that wouldn t Goals for today The little button that wouldn't :( the volatile keyword Pointer operations => ARM addressing modes Implementation of C function calls Management of runtime stack, register use button.c

More information

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3 OMP15111 Lecture 9 1/26 From last time What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3 RSB R0, R0, R0,LSL #2 Write down

More information

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls Instructors: Dr. Phillip Jones 1 Announcements Final Projects Projects: Mandatory

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

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

Storage in Programs. largest. address. address

Storage in Programs. largest. address. address Storage in rograms Almost all operand storage used by programs is provided by memory. Even though registers are more efficiently accessed by instructions, there are too few registers to hold the stored

More information

ARM Assembly Programming

ARM Assembly Programming ARM Assembly Programming Computer Organization and Assembly Languages g Yung-Yu Chuang 2007/12/1 with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C compiler

More information

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100 ECE 2035(B) Programming for Hardware/Software Systems Fall 2013 Exam Two October 22 nd 2013 Name: Q1: /20 Q2: /30 Q3: /24 Q4: /26 Total: /100 1/6 For functional call related questions, let s assume the

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

More information

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Stack Frames EE 357 Unit 8 Stack Frames Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Every call to a subroutine will create a data structure

More information

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

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 EN164: Design of Computing Systems Lecture 11: Processor / ISA 4 Professor Sherief Reda http://scale.engin.brown.edu Electrical Sciences and Computer Engineering School of Engineering Brown University

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

Chapter 9 :: Subroutines and Control Abstraction

Chapter 9 :: Subroutines and Control Abstraction Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November

More information

ARM Assembly Programming

ARM Assembly Programming ARM Assembly Programming Computer Organization and Assembly Languages g Yung-Yu Chuang with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C compiler as:

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

Bonus Lecture: Fibonacci

Bonus Lecture: Fibonacci Bonus Lecture: Fibonacci ECE 362 https://engineering.purdue.edu/ee362/ Rick Reading You could try looking up recurisve function in your textbook. Recursive Fibonacci if (x < 2) return fibonacci(x 1) +

More information

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

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack Part 7 Stacks The Stack Piles of Data Stack Stack A stack is an abstract data structure that stores objects Based on the concept of a stack of items like a stack of dishes Data can only be added to or

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

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

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

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

Code Generation. Lecture 19

Code Generation. Lecture 19 Code Generation Lecture 19 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

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

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

Exam 1. Date: February 23, 2016

Exam 1. Date: February 23, 2016 Exam 1 Date: February 23, 2016 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help others to cheat on this exam:

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

Instruction Set Architectures (4)

Instruction Set Architectures (4) Computer Architecture Week 06 Instruction Set Architectures (4) College of Information Science and Engineering Ritsumeikan University subroutines functions, procedures remember the next instruction s address

More information

Exam 1. Date: February 23, 2018

Exam 1. Date: February 23, 2018 Exam 1 Date: February 23, 2018 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help others to cheat on this exam:

More information

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

Topic 3-a. Calling Convention 2/29/2008 1 Topic 3-a Calling Convention 2/29/2008 1 Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 2/29/2008

More information

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

EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design EN164: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu Electrical Sciences and Computer Engineering School of Engineering Brown

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

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

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly COMP 3221 Microprocessors and Embedded Systems Lectures 18 : Pointers & Arrays in C/ Assembly http://www.cse.unsw.edu.au/~cs3221 Overview Arrays, Pointers, Functions in C Example Pointers, Arithmetic,

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

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

Implementing Functions at the Machine Level

Implementing Functions at the Machine Level 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

More information

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

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

Support for high-level languages

Support for high-level languages Outline: Support for high-level languages memory organization ARM data types conditional statements & loop structures the ARM Procedure Call Standard hands-on: writing & debugging C programs 2005 PEVE

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han s and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements Homework Set #2 due Friday at 11 am - extension Program Assignment #1 due Tuesday Feb. 15 at 11 am - note extension

More information

ARM Assembly Programming II

ARM Assembly Programming II ARM Assembly Programming II Computer Organization and Assembly Languages Yung-Yu Chuang 2007/11/26 with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C

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

Exam 1. Date: February 23, 2018

Exam 1. Date: February 23, 2018 Exam 1 Date: February 23, 2018 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help others to cheat on this exam:

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design 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:

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

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

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read Function Calls Tom Kelliher, CS 240 Feb. 13, 2002 1 Administrivia Announcements Collect homework. Assignment Read 3.7 9. From Last Time SPIM lab. Outline 1. Function calls: stack execution model, memory

More information

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

An Introduction to Assembly Programming with the ARM 32-bit Processor Family An Introduction to Assembly Programming with the ARM 32-bit Processor Family G. Agosta Politecnico di Milano December 3, 2011 Contents 1 Introduction 1 1.1 Prerequisites............................. 2

More information

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

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu School of Engineering Brown University Spring 2014 Sources: Computer

More information

Assembly Programming (III)

Assembly Programming (III) Assembly Programming (III) Lecturer: Annie Guo S2, 2006 COMP9032 Week6 1 Lecture Overview Stack and stack operations Functions and function calls Calling conventions S2, 2006 COMP9032 Week6 2 What is stack?

More information

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015 EE319K Spring 2015 Exam 1 Page 1 Exam 1 Date: Feb 26, 2015 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help

More information

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map (14) Question 1. For each of the following components, decide where to place it within the memory map of the microcontroller. Multiple choice select: RAM, ROM, or other. Select other if the component is

More information

CS 2210 Programming Project (Part IV)

CS 2210 Programming Project (Part IV) CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Computer Architecture and System Software Lecture 06: Assembly Language Programming Computer Architecture and System Software Lecture 06: Assembly Language Programming Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements Assignment 3 due thursday Midterm

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

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution 1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can

More information

Stacks and Function Calls

Stacks and Function Calls Stacks and Function Calls Embedded Systems 3-1 Remember the Memory Map for Our MCU Embedded Systems 3-2 Classifying Data Variables Automatic declared within a function Only exist while the function executes

More information

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

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure Procedures, the basic idea CSCI206 - Computer Organization & Programming Introduction to Procedures zybook: 81 (for next class) MIPS Procedure convention 1 Prepare parameters in $a0 through $a3 2 Return

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

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

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

More information

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

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

x86 assembly CS449 Fall 2017

x86 assembly CS449 Fall 2017 x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS

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

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

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

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Computer Architecture and System Software Lecture 07: Assembly Language Programming Computer Architecture and System Software Lecture 07: Assembly Language Programming Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements New assembly examples uploaded to

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

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

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information