Procedures & Stacks Worksheet

Similar documents
How can the machine ULg01 be programmed?

How Computers Work Lecture 2. Assembly Tools Calling Conventions. Review: β Model of Computation Processor State

6.004 Computation Structures Spring 2009

Stacks and procedures

6.004 Computation Structures Spring 2009

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

MIPS Procedure Calls. Lecture 6 CS301

Topic 7: Activation Records

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

CSE Lecture In Class Example Handout

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

MIPS Functions and Instruction Formats

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

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

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

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

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

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

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

Segmentation in Assembly Language Programming

ECE260: Fundamentals of Computer Engineering

Scope, Functions, and Storage Management

Data Structure Layout. In HERA/Assembly

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

Compiling Code, Procedures and Stacks

Parsing Scheme (+ (* 2 3) 1) * 1

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

Sungkyunkwan University

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

Stacks and Procedures

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

6.004 Tutorial Problems L3 Procedures and Stacks

CS429: Computer Organization and Architecture

Functions in MIPS. Functions in MIPS 1

CSE 351 Midterm - Winter 2015

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

Machine-level Programming (3)

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

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

An Experience Like No Other. Stack Discipline Aug. 30, 2006

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

University of Washington

Implementing Subroutines. Outline [1]

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

231 Spring Final Exam Name:

Subprograms, Subroutines, and Functions

CS153: Compilers Lecture 8: Compiling Calls

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

CSE Lecture In Class Example Handout

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

Memory Hierarchy & Caches Worksheet

CS 316: Procedure Calls/Pipelining

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

SRC Assembly Language Programming - III

EE 3170 Microcontroller Applications

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Assembly Language Manual for the STACK Computer

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

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Systems I. Machine-Level Programming V: Procedures

Compilers and computer architecture: A realistic compiler to MIPS

Anne Bracy CS 3410 Computer Science Cornell University

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

Problem: ! bell! help!! 1! 2!! Bal help!!!! Bal bell! 3 Jr $31!!!! ! Jr $31! What happend! What will happen here?!

NET3001. Advanced Assembly

CSE 351 Midterm - Winter 2015 Solutions

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

Control Instructions

MIPS Functions and the Runtime Stack

Chapter 17 Recursion

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Stacks and Procedures

Lecture V Toy Hardware and Operating System

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

Stack Discipline Jan. 19, 2018

Designing an Instruction Set

Language of the Machine Recursive functions

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

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

Implementing Procedure Calls

Anne Bracy CS 3410 Computer Science Cornell University

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

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

CA Compiler Construction

Code Generation. Lecture 19

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CS213. Machine-Level Programming III: Procedures

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

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

Stacks and Function Calls

CS240: Programming in C

EEL 3801 Introduction to Computer Engineering Summer Home Work Schedule

Run-time Environments

Scope: Global and Local. Concept of Scope of Variable

Run-time Environments

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Transcription:

Procedures & Stacks Worksheet PUSH(X): Push Reg[x] onto stack ADDC(SP,4,SP) ST(Rx,-4,SP) POP(X): Pop value at top of stack into Reg[x] LD(SP,-4,RX) SUBC(SP,4,SP) ALLOCATE(k): Reserve k words of stack ADDC(SP,4*k,SP) DEALLOCATE(k): Release k words of stack SUBC(SP,4*k,SP) Stack discipline: leave stack the way you found it => for every PUSH(), there s a corresponding POP() or DEALLOCATE() Activation record layout on the stack (aka stack frame): CALLING SEQUENCE PUSH(argn) // push args, last arg first PUSH(arg1) BR(f, LP) // call f, return addr in LP DEALLOCATE(n) // remove args from stack ENTRY SEQUENCE f: PUSH(LP) // save return addr // save old frame pointer MOVE(SP,BP) // initialize new frame pointer ALLOCATE(nlocals) // make room for locals (push other regs) // preserve old reg vals EXIT SEQUENCE // return value in R0 MOVE(BP,SP) // remove locals // restore old frame pointer // recover return address // resume execution in caller 6.004 Worksheet - 1 of 6 - Procedures and Stacks

Problem 1. You are given an incomplete listing of a C program (shown below) and its translation to Beta assembly code (shown on the right): int fn(int x) { int lowbit = x & 1; int rest = x >> 1; if (x == 0) return 0; else return???; (A) What is the missing C source corresponding to??? in the above program? C source code: (B) Suppose the instruction bearing the tag zz: were eliminated from the assembly language program. Would the modified procedure work the same as the original procedure (circle one)? Work the same? YES NO fn: PUSH(LP) MOVE(SP,BP) ALLOCATE(2) ANDC(R0,1,R1) xx: ST(R1,0,BP) SHRC(R0,1,R1) ST(R1,4,BP) yy: BEQ(R0,rtn) LD(BP,4,R1) BR(fn,LP) DEALLOCATE(1) LD(BP,0,R1) ADD(R1,R0,R0) rtn:pop(r1) zz: MOVE(BP,SP) (C) In the space below, fill in the binary representation for the instruction stored at the location tagged xx: in the above program. (fill in missing 1s and 0s for instruction at xx:) 6.004 Worksheet - 2 of 6 - Procedures and Stacks

The procedure fn is called from an external procedure and its execution is interrupted just prior to the execution of the instruction tagged yy:. The contents of a region of memory are shown on the left below. NB: All addresses and data values are shown in hex. The contents of BP are 0x1C8 and SP contains 0x1D4. 184: 4 188: 7 18C: 47 190: C4 194: 170 198: 1 19C: 23 1A0: 22 1A4: 23 1A8: 4C 1AC: 198 1B0: 1 1B4: 11 1B8: 23 1BC: 11 1C0: 4C 1C4: 1B0 1C8: 1 BP 1CC: 8 1D0:??? 1D4: 0 SP (D) What was the argument to the most recent call to fn? Most recent argument (HEX): x= (E) What is the missing value marked??? for the contents of location 1D0? Contents of 1D0 (HEX): (F) What is the hex address of the instruction tagged rtn:? Address of rtn (HEX): (G) What was the argument to the original call to fn? Original argument (HEX): x= (H) What is the hex address of the BR instruction that called fn originally? Address of original call (HEX): (I) What were the contents of R1 at the time of the original call? Original R1 contents (HEX): (J) What value will be returned to the original caller? Return value for original call (HEX): 6.004 Worksheet - 3 of 6 - Procedures and Stacks

Problem 2. You are given an incomplete listing of a C program (shown below) and its translation to Beta assembly code (shown on the right): int f(int x, int y) { x = (x >> 1) + y; if (y == 0) return x; else return???; (A) What is the missing C source corresponding to??? in the above program? C source code: (B) Suppose the instruction bearing the tag zz: were eliminated from the assembly language program. Would the modified procedure work the same as the original procedure? Work the same (circle one)? YES NO The procedure f is called from an external procedure and then execution is stopped just prior to one of the executions of the instruction labeled rtn:. The addresses and contents of a region of memory are shown in the table on the right; all addresses and data values in the table are in hex. When execution is stopped BP contains the value 0x14C and SP contains the value 0x150. (C) What are the arguments to the currently active call to f? Most recent arguments (in hex): x = 0x, y = 0x (D) If you can tell from the information provided, specify the arguments to the original call to f, otherwise select CAN T TELL. Original arguments (in hex) : x = 0x, y = 0x, or CAN T TELL (E) What is the missing value in location 0x12C? Contents of location 0x12C (in hex): 0x (F) What is the hex address of the instruction labeled rtn:? Address of instruction labeled rtn: (in hex): 0x (G) What is the hex address of the BR instruction that called f originally? Address of original call (in hex): 0x, or CAN T TELL (H) What value will be returned to the original caller? Return value for original call (in hex): 0x f: PUSH(LP) MOVE(SP,BP) SHRC(R0,1,R0) LD(BP,-16,R1) ADD(R0,R1,R0) BEQ(R1,rtn) SUBC(R1,1,R1) PUSH(R0) BR(f,LP) DEALLOCATE(2) rtn: POP(R1) zz: MOVE(BP,SP) 108 7 10C 320 110 104 114 3 118 A 6.004 Worksheet - 4 of 6 - Procedures and Stacks 11C 2C4 120 104 124 3 128 2 12C 130 348 134 124 138 2 13C 1 140 6 144 348 148 138 14C 1 150 0 154 4 158 348 15C 14C 160 0

Problem 3. The following C program implements a function H(x,y) of two arguments, which returns an integer result. The assembly code for the procedure is shown on the right. int H(int x, int y) { int a = x - y; if (a < 0) return x; else return???; The execution of the procedure call H(0x68,0x20) has been suspended just as the Beta is about to execute the instruction labeled rtn: during one of the recursive calls to H. A partial trace of the stack at the time execution was suspended is shown to the right below. (A) Examining the assembly language for H, what is the appropriate C code for??? in the C representation for H? C code for???: (B) Please fill in the values for the blank locations in the stack dump shown on the right. Express the values in hex or write --- if value can t be determined. Hint: Figure out the layout of H s activation record and use it to identify and label the stack frames in the stack dump. Fill in the blank locations with values (in hex!) or --- (C) Determine the specified values at the time execution was suspended. Please express each value in hex or write CAN T TELL if the value cannot be determined. H: PUSH(LP) MOVE(SP, BP) ALLOCATE(1) LD(BP,-16,R1) SUB(R0,R1,R1) ST(R1,0,BP) CMPLTC(R1,0,R1) BT(R1,rtn) LD(BP,-16,R1) LD(BP,0,R0) PUSH(R0) BR(H,LP) DEALLOCATE(2) rtn: POP(R1) MOVE(BP,SP) 0x0024 0x0070 0x0048 0x0068 Value in R0 or CANT TELL : 0x Value in R1 or CANT TELL : 0x Value in BP or CANT TELL : 0x Value in LP or CANT TELL : 0x Value in SP or CANT TELL : 0x BP 0x0028 0x007C 0x00C8 0x0008 6.004 Worksheet - 5 of 6 - Procedures and Stacks

Problem 4. The following C program computes the log base 2 of its argument. The assembly code for the procedure is shown on the right, along with a stack trace showing the execution of ilog2(10). The execution has been halted just as it s about to execute the instruction labeled rtn: /* compute log base 2 of arg */ int ilog2(unsigned x) { unsigned y; if (x == 0) return 0; else { /* shift x right by 1 bit */ y = x >> 1; return ilog2(y) + 1; (A) What are the values in R0, SP, BP and LP at the time execution was halted? Please express the values in hex or write CAN T TELL. Value in R0: 0x in SP: 0x ilog2: PUSH(LP) MOVE(SP,BP) ALLOCATE(1) rtn: xxx: BEQ(R0,rtn,R31) LD(BP,-12,R1) SHRC(R1,1,R1) ST(R1,0,BP) LD(BP,0,R1) BR(ilog2,LP) DEALLOCATE(1) ADDC(R0,1,R0) POP(R1) DEALLOCATE(1) MOVE(BP,SP) Value in BP: 0x in LP: 0x (B) Please fill in the values for the five blank locations in the stack trace shown on the right. Please express the values in hex. Fill in values (in hex!) for 5 blank locations (C) In the assembly language code for ilog2 there is the instruction LD(BP,- 12,R0). If this instruction were rewritten as LD(SP,NNN,R0) what is correct value to use for NNN? Correct value for NNN: (D) In the assembly language code for ilog2, what is the address of the memory location labeled xxx:? Please express the value in hex. Address of location labeled xxx: : 0x Values are in hex! BP 0 1 0 5 1A8 208 2 5 1 1A8 230 6.004 Worksheet - 6 of 6 - Procedures and Stacks