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

Similar documents
Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Q1: /14 Q2: /12 Q3: /8 Q4: /8. Total: /42

Storage in Programs. largest. address. address

Functions and Procedures

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

Q1: /30 Q2: /25 Q3: /45. Total: /100

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

CSE Lecture In Class Example Handout

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Functions in MIPS. Functions in MIPS 1

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

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

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

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

Today. Putting it all together

CSE Lecture In Class Example Handout

Instruction Set Architectures (4)

Implementing Subroutines. Outline [1]

System Software Assignment 1 Runtime Support for Procedures

CS 0449 Sample Midterm

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

CS153: Compilers Lecture 8: Compiling Calls

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

ECE232: Hardware Organization and Design

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

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

Course Administration

Implementing Procedure Calls

MIPS Procedure Calls. Lecture 6 CS301

SPIM Procedure Calls

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Run-Time Data Structures

Understanding Pointers

ECE551 Midterm Version 2

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Lab 3. Pointers Programming Lab (Using C) XU Silei

Topic 7: Activation Records

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

Scope: Global and Local. Concept of Scope of Variable

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

Procedure Call and Return Procedure call

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Lecture 7: Procedures and Program Execution Preview

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

ECE331: Hardware Organization and Design

Low-Level C Programming. Memory map Pointers Arrays Structures

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Concepts Introduced in Chapter 7

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

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.

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Compilers and computer architecture: A realistic compiler to MIPS

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Data Transfer Instructions

M.CS201 Programming language

EE 361 University of Hawaii Fall

MIPS Functions and the Runtime Stack

Anne Bracy CS 3410 Computer Science Cornell University

Wednesday, September 27, 2017

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

Anne Bracy CS 3410 Computer Science Cornell University

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

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

Lecture 5: Procedure Calls

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

ESC101N: Fundamentals of Computing End-sem st semester

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

ECE551 Midterm Version 1

Dynamic Memory Allocation and Command-line Arguments

G Programming Languages - Fall 2012

TILE PROCESSOR APPLICATION BINARY INTERFACE

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

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

THEORY OF COMPILATION

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Programs in memory. The layout of memory is roughly:

Lec 10: Assembler. Announcements

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

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

x86 assembly CS449 Fall 2017

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

Run-time Environment

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

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

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

ECE264 Fall 2013 Exam 1, September 24, 2013

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

CS240: Programming in C

ECE 2035 Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 1 May 2013

University of Illinois at Urbana-Champaign First Midterm Exam, ECE 220 Honors Section

CSCE 5610: Computer Architecture

MIPS Procedure Calls - Review

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Chapter 10 Memory Model for Program Execution. Problem

Transcription:

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 following convention is used when implementing function calls (which is exactly the same as we discussed in class): 1. The stack grows downwards unless specified otherwise. 2. The stack contains the following items, in the order that they are allocated on the stack: Return address Frame Pointer Input Parameters Return Value Local Variables of the Callee 3. All parameters are passed via the stack (none are passed via registers) 4. Caller only allocates part of the activation frame for the callee before calling 'jal' - it only allocates until the return value (including return value) and updates the stack pointer accordingly (so it points to the return value). 5. Caller pushes input parameters onto the stack in the reverse order as the parameters are declared. 6. Callee is responsible of populating the rest of the activation frame (just the local variables). 7. Callee is responsible of deallocating the local variable part of the activation frame when it returns 8. Caller is responsible of deallocating the rest of the callee s activation frame. In addition to the above, we also have the following: 9. The local variables are allocated in the same order as they were declared. 10. Stack pointer points to the last occupied slot on the stack. 2/6

Question 1 stack (20 pts)!! This question does not use the convention defined in page 2!! In our class, we learned that we will use the stack to pass input parameters into functions, track return addresses, collect return values, and hold local variables. In the classroom examples, we always assume that the stack grows towards the lower end of address spaces. However, it is perfectly fine to design a system where the stack grows upwards. Suppose you are given the following system: 1. The stack grows in one direction, but we do not know which direction yet. 2. You can only program in C. 3. You cannot operate on the stack directly (i.e. only function calls in your program can change content of the stack, you cannot push/pop the stack directly). 4. The activation frame contains the following items: i. Return address ii. Frame Pointer iii. Input Parameters iv. Return Value v. Local Variables of the Callee 5. However, we do not know anything about the order that items within an activation frame are placed, e.g., the return address may be at the bottom of the activation frame, or input parameters may be pushed in a random order they may even be placed after the local variables, or the local variables may be allocated not in program order, etc. Write a short c function to detect whether the stack grows downwards or upwards. The function needs to return 0 when it s downwards, and 1 otherwise. 3/6

Question 2 30 points The following variables are allocated in the memory beginning at address 2000 on a 64- bit system. Note that the allocation goes towards the upper end of the address space. Part A: (6 pts) We know the following data type sizes already: char: 1 byte float: 4 bytes int: 4 bytes What are the sizes of the following data types? char * float * int * Part B (16 pts): Complete the memory map below with the variable names at each word or byte in memory. Do not include the variable s value. For B[4], you need to specify the mapping for each of the 4 elements in array B. 2000 2004 char A = A ; int B[4] = {1, 2, 3, 4; char *p1 = &A; int *p2 = &(B[1]); char s[] = bar ; 2008 2012 2016 2020 2024 2028 2032 2036 2040 The variables are allocated in the order that they are defined. The allocation scheme tries not to leave gaps in the memory unless necessary (e.g no gaps between char data types). Watch for alignment. Part C (8 pts): using the values in part B, give the value of each expression. p1 *p1 &p1 p2 *p2 &p2 p2[2] s[3] 4/6

Question 3: 24 points Part A: (6 points) Given the following C code: #include <stdio.h> #include <stdlib.h> int foo(int x) { x = x + 1; int main() { int i = 100; foo(i); printf( %d\n, i); What will main() print? Part B: (10 points) Given the following C code #include <stdio.h> #include <stdlib.h> int foo(int *x) { *x = *x + 1; int main() { int i = 100; foo(&i); printf( %d\n, i); Will the code compile? If yes, what will main() print? If not, Why? 5/6

Part C: ( 8 points) Given the following C code attention: #include <stdio.h> #include <stdlib.h> int foo(char *a) { char b = Y ; *a = X ; a = &b; printf( %c, *a); int main() { char str[] = ABCDE ; foo(str); printf( %c, str[0]); The line labeled attention changes the value of a in foo(), does this change the value of str in main()? What will printf() in foo() print? What will printf() in main() print? 6/6

Question 4: 26 points Given the following C function: int Foo (int n) { int a; a = Bar(0); return a; Implement the function Foo() in MIPS. You need to follow the comment exactly. Add labels as necessary. You need to use the convention on the first page. Also, it is caller s responsibility to save its frame pointer onto the stack before calling a function, and to set the frame pointer register for the callee. However, it is the callee s responsibility to restore caller s frame pointer when the callee returns. Label Instruction comment Foo # reserve return address # allocate local variable a on the stack # Allocate the activation frame for Bar (caller s part) # Save Foo s Frame Pointer # Set Frame Pointer for Bar # Call Bar # read return value of bar() # save the return value into a, i.e. a=bar(); # deallocate the activation frame for Bar (caller s part) # save a as the return value # restore Foo s caller s frame pointer # restore the return address # return 7/6