COMP 210 Example Question Exam 2 (Solutions at the bottom)

Similar documents
CS , Fall 2004 Exam 1

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls" Goals of this Lecture"

CPSC W Term 2 Problem Set #3 - Solution

Machine-level Programming (3)

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Machine Program: Procedure. Zhaoguo Wang

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

You may work with a partner on this quiz; both of you must submit your answers.

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

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

Machine-Level Programming III: Procedures

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

CS213. Machine-Level Programming III: Procedures

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

The Hardware/Software Interface CSE351 Spring 2013

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CPS104 Recitation: Assembly Programming

CSE 351: Week 4. Tom Bergan, TA

Instruction Set Architecture

Instruction Set Architecture

Data Representa/ons: IA32 + x86-64

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Machine Programming 3: Procedures

CS241 Computer Organization Spring Loops & Arrays

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

W4118: PC Hardware and x86. Junfeng Yang

Instruction Set Architecture

CISC 360 Instruction Set Architecture

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

CS241 Computer Organization Spring 2015 IA

Function Calls and Stack

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CPEG421/621 Tutorial

Machine/Assembler Language Putting It All Together

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Sungkyunkwan University

Instruction Set Architecture

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Sungkyunkwan University

Assignment 11: functions, calling conventions, and the stack

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Credits and Disclaimers

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Machine-Level Programming II: Control and Arithmetic

System Programming and Computer Architecture (Fall 2009)

X86 Stack Calling Function POV

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Lecture 4 CIS 341: COMPILERS

CSE351 Spring 2018, Midterm Exam April 27, 2018

CS , Fall 2002 Exam 1

Instructor: Alvin R. Lebeck

The von Neumann Machine

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

x86 assembly CS449 Fall 2017

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Instruction Set Architectures

Process Layout and Function Calls

CS , Fall 2001 Exam 1

211: Computer Architecture Summer 2016

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

X86 Assembly -Procedure II:1

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

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

CS 3843 Final Exam Fall 2012

Process Layout, Function Calls, and the Heap

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

Assembly I: Basic Operations. Jo, Heeseung

Lab 10: Introduction to x86 Assembly

Credits to Randy Bryant & Dave O Hallaron

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

Machine- Level Programming II: Arithme6c & Control

Assembly Language: IA-32 Instructions

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Credits and Disclaimers

Machine-Level Programming I: Introduction Jan. 30, 2001

Transcription:

_ Problem 1. COMP 210 Example Question Exam 2 (Solutions at the bottom) This question will test your ability to reconstruct C code from the assembled output. On the opposing page, there is asm code for a routine called bunny. It comes from a C routine with the following outline. Don t fill in the outline yet. static int bunny(int l, int r, int *A){ int x = ; int i = ; int j = ; while( ) do j--; while( ); do i++; while( ); if( ){ int t = A[i]; A[i] = A[j]; A[j] = t; _return ; _ A. (3 points): Fill in the following table of register usage. Use the variable names from the outline. If a register gets used to store two different things, just list both of them. I ve filled in two blanks to show examples. This will help you understand the code; do this before part C. Register Variable %eax %ebx %ecx %edx %esi %edi %esp %ebp B. (3 points): Why does bunny push %edi, %esi, and %ebx on to the stack?

bunny:.l16:.l7: jl.l7.l3: pushl %ebp movl %esp, %ebp pushl %edi pushl %esi pushl %ebx movl 8(%ebp), %eax movl 16(%ebp), %esi movl (%esi,%eax,4), %edi leal -1(%eax), %ecx movl 12(%ebp), %ebx incl %ebx cmpl %ebx, %ecx jge.l3 decl %ebx cmpl %edi, (%esi,%ebx,4) jg.l16 incl %ecx cmpl %edi, (%esi,%ecx,4) cmpl %ebx, %ecx jge.l3 movl (%esi,%ecx,4), %edx movl (%esi,%ebx,4), %eax movl %eax, (%esi,%ecx,4) movl %edx, (%esi,%ebx,4) jmp.l16 movl %ebx, %eax popl %ebx popl %esi popl %edi popl %ebp ret

C. (5 points): Fill in the blanks on the outline (on the previous page). D. (4 points): Look at draft_horse and write out the control flow structure. As an example, the control flow structure of bunny would be: bunny() { while() { while() { while() { if() { return; I want to know about any if, while, function calls, and returns, but I don t care about anything else. Do not use goto. draft_horse() { E. (bragging rights): What algorithm is this code implementing?

draft_horse: pushl %ebp movl %esp, %ebp subl $28, %esp movl %ebx, -12(%ebp) movl %esi, -8(%ebp) movl %edi, -4(%ebp) movl 8(%ebp), %ebx movl 12(%ebp), %esi movl 16(%ebp), %edi cmpl %esi, %ebx jge.l17 movl %edi, 8(%esp) movl %esi, 4(%esp) movl %ebx, (%esp) call bunny movl %eax, -16(%ebp) movl %edi, 8(%esp) movl %eax, 4(%esp) movl %ebx, (%esp) call draft_horse movl %edi, 8(%esp) movl %esi, 4(%esp) movl -16(%ebp), %eax incl %eax movl %eax, (%esp) call draft_horse.l17: movl -12(%ebp), %ebx movl -8(%ebp), %esi movl -4(%ebp), %edi movl %ebp, %esp popl %ebp ret

Fill in the run time stack for the subroutine f (and only f) just before the assembly code call instruction corresponding to the return statement is exectued. Assume that the registers %rax, %rbx, and %r12 will be used in the body of the subroutine for f. Include the parameters, return address, and any other conventions that gcc will follow. Assume that the return address to main will be 0x08048f6e. Assume that %rsp contains 0xbffffa30 and %rbp contains 0xbffffbec just before line 1 is executed. This is shown on the chart. Assume there is no code optimization. Put any actual value on the stack that you can determine. Use the value that is actually on the stack in hex in little endian format. If you can t determine the value put three question marks:??? Addresses stored on the stack should not be in little endian format. Assume that z (in f) is stored on the runtime stack, not just in a register. Assume that the assembly code for f saves the parameters on the stack at the beginning of the subroutine Label the stack with any memory address that you can calculate (you can calculate just about all). Label each value on the stack with the corresponding variable name from the C program if it can be determined. Label each entry on the stack with its relative distance from the %rbp pointer in its stack frame (recall that each call to a subroutine produces a new stack frame), i.e., +0x08, 0x00, -0x08, -0x10, etc. Your values must be in hexadecimal! Assume that space is allocated on the run-time stack only when it is required by convention (i.e., to follow stack discipline). In other words, the compiler will change %rsp only when needed for some value like a parameter, saved register, return address, or local variable. Show where %rbp and %rsp will be pointing just before line 1 is executed, and just before the return is executed.

long f(long a, long b) { long sum; sum = a + b; return sum; // write out the runtime stack just before this return is executed int main() { long a, b, c; a = 5; b = 10; c = f(a, b); // line 1 print( %d + %d = %d\n, a, b, c); return 0;

Memory Address offset from %ebp value C variable name 0xbffffa30 %rsp before line 1

Solutions: A. Register Variable %eax l, A[j], j %ebx r, j %ecx i %edx A[i], t %esi A %edi x %esp stack pointer %ebp stack frame pointer B. These are callee save registers. C. static int bunny(int l, int r, int *A) { int x = A[l]; int i = l - 1; int j = r + 1; while(i < j) { do { j--; while(a[j] > x); do { i++; while(a[i] < x); if(i < j) { int t = A[i]; A[i] = A[j]; A[j] = t; return j; D. draft_horse() { if() { bunny(); draft_horse(); draft_horse(); return; E. This is the code for quicksort.