CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Similar documents
CSE 351 Midterm - Winter 2015

CSE 351 Midterm - Winter 2015 Solutions

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

UW CSE 351, Winter 2013 Midterm Exam

CSE 351 Midterm - Winter 2017

1 Number Representation(10 points)

Do not turn the page until 5:10.

CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name:

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

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

CS , Fall 2004 Exam 1

Assembly Language: Function Calls

CS61 Section Solutions 3

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

15-213/18-213, Fall 2011 Exam 1

15-213/18-243, Spring 2011 Exam 1

Assembly Language: Function Calls" Goals of this Lecture"

Assembly III: Procedures. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly Language: Function Calls" Goals of this Lecture"

CS 3843 Final Exam Fall 2012

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Do not turn the page until 11:30.

Do not turn the page until 5:10.

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CSE 351 Midterm Exam

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

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

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

CS213. Machine-Level Programming III: Procedures

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

CSCI 356 Fall 2017 : Practice Exam I DO NOT OPEN EXAM PACKET UNTIL INSTRUCTED TO DO SO YOU MAY FILL IN INFORMATION ON THE FRONT NOW

System Programming and Computer Architecture (Fall 2009)

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

Question 1: Number Representation

CSE 351 Spring 2017 Midterm Exam (8 May 2017)

CS429: Computer Organization and Architecture

Machine-level Programming (3)

CPS104 Recitation: Assembly Programming

CS / ECE , Spring 2010 Exam 1

CS241 Computer Organization Spring 2015 IA

Machine Program: Procedure. Zhaoguo Wang

CS , Spring 2004 Exam 1

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

Do not turn the page until 5:10.

15-213, Fall 2007 Midterm Exam

Sungkyunkwan University

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

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

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Assignment 11: functions, calling conventions, and the stack

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

Machine/Assembler Language Putting It All Together

CS61, Fall 2012 Midterm Review Section

15-213/18-243, Summer 2011 Exam 1 Tuesday, June 28, 2011

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

16.317: Microprocessor Systems Design I Fall 2015

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

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

Do not turn the page until 12:30.

16.317: Microprocessor Systems Design I Fall 2014

Machine Programming 3: Procedures

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

Machine Programming 1: Introduction

CS , Fall 2001 Exam 1

CS 261 Fall Binary Information (convert to hex) Mike Lam, Professor

Machine- Level Programming III: Switch Statements and IA32 Procedures

CS , Fall 2002 Exam 1

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

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Instructor: Alvin R. Lebeck

Do not turn the page until 5:10.

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

Lab 10: Introduction to x86 Assembly

CSCI 2021: x86-64 Control Flow

X86 Assembly -Procedure II:1

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

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

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

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

Machine-Level Programming II: Control and Arithmetic

Summary: Direct Code Generation

Machine-Level Programming III: Procedures

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Process Layout and Function Calls

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Digital Forensics Lecture 3 - Reverse Engineering

15-213/18-243, Fall 2010 Exam 1 - Version A

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

Second Part of the Course

Instruction Set Architecture

Instruction Set Architecture

Computer Systems C S Cynthia Lee

How Software Executes

Instruction Set Architecture

Computer Systems C S Cynthia Lee

CS-220 Spring 2018 Final Exam Version Practice May 10, Name:

University of Washington

Transcription:

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012) Please read through the entire examination first! We designed this exam so that it can be completed in 50 minutes and, hopefully, this estimate will prove to be reasonable. There are 4 problems for a total of 100 points. The point value of each problem is indicated in the table below. Write your answer neatly in the spaces provided. If you need more space (you shouldn't), you can write on the back of the sheet where the question is posed, but please make sure that you indicate clearly the problem to which the comments apply. Do NOT use any other paper to hand in your answers. If you have difficulty with part of a problem, move on to the next one. They are independent of each other. The exam is CLOSED book and CLOSED notes. Please do not ask or provide anything to anyone else in the class during the exam. Make sure to ask clarification questions early so that both you and the others may benefit as much as possible from the answers. Name: ID#: Sample Solution Problem Max Score Score 1 15 15 2 20 20 3 40 40 4 25 25 TOTAL 100 100

1. Number Representation (15 points) The decimal value 11,184,810 is represented as a 32-bit signed binary with the bit pattern below (0x00aaaaaa): 0000 0000 1010 1010 1010 1010 1010 1010 When it is cast as a float, it is represented by the 32-bit floating point format (8-bits exp, 23-bit fraction) as (0x4b2aaaaa): 0100 1011 0010 1010 1010 1010 1010 1010 Explain why so many of the low-order bits are the same and why do the others differ. There is no need to convert these to decimal values. The exponent part of the float value is 1001 0110 which translates to E = 150 bias = 150 127 = 23. Given that the value of the float is 1.frac * 2 23, and the fractional part is 23 bits, the binary point moves 23 bits to the right. Therefore, we expect to see the same 23 low-order bits in the integer value with the next high-order bit being 1 (the implied 1. of the float representation). The remaining 8 high-order bits of the integer value are 0.

2. Assembly Code (20 points) A function flip has the following overall structure: int flip (*unsigned x) { int num=*x; int val=0; int i; for ( initialize ; test ; update ) { body } return val; } The GCC C compiler generates the following assembly code: x at %ebp+8 1 movl 8(%ebp), %ebx get x, a pointer to an unsigned int 2 movl (%ebx), %esi dereference x to get the unsigned int into num 3 movl $0, %eax initialize val to 0, stored in %eax 4 movl $0, %ecx initalize i to 0, stored in %ecx 5.L13: 6 leal (%eax, %eax), %edx double val, like shifting 1 left, put in %edx 7 movl %esi, %eax copy num to %eax 8 andl $1, %eax mask to get low-order bit of num 9 orl %edx, %eax OR the low-order bit with doubled val 10 shrl %esi shift num to right by 1 11 add $1, %ecx increment i 12 cmpl $32, %ecx test if reached 32 13 jne.l13 jump to L13 (top of loop) if i < 32 14 ret return with val stored in %eax Reverse engineer the operation of this code and then do the following:

A (15 pts). Use the assembly-code version to fill in the missing parts of the C code below. Also specify which lines above represent each of initialize, test, update, and body. Initialize: Test: Update: 4 12, 13 11 Body: 6, 7, 8, 9, 10_ int flip (*unsigned x) { int num=*x; int val=0; int i; for ( i=0 ; i<32 ; i++ ) { val = (val << 1) (num & 0x1); num = num >> 1; } } return val; B (5 pts). Describe what this function computes in one English sentence (or at most two). The function returns an int that has the same bits as stored at x but in reverse order.

3. Procedures (40 points) The following assembly routine takes a positive integer as input and returns a positive integer: 0000000000400525 <mystery>: 400525: 55 push %rbp 400526: 48 89 e5 mov %rsp,%rbp 400529: 53 push %rbx 40052a: 48 83 ec 18 sub $0x18,%rsp 40052e: 89 7d ec mov %edi,-0x14(%rbp) 400531: 83 7d ec 00 cmpl $0x0,-0x14(%rbp) 400535: 75 07 jne 40053e <mystery+0x19> 400537: b8 00 00 00 00 mov $0x0,%eax 40053c: eb 2b jmp 400569 <mystery+0x44> 40053e: 83 7d ec 01 cmpl $0x1,-0x14(%rbp) 400542: 75 07 jne 40054b <mystery+0x26> 400544: b8 01 00 00 00 mov $0x1,%eax 400549: eb 1e jmp 400569 <mystery+0x44> 40054b: 8b 45 ec mov -0x14(%rbp),%eax 40054e: 83 e8 01 sub $0x1,%eax 400551: 89 c7 mov %eax,%edi 400553: e8 cd ff ff ff callq 400525 <mystery> 400558: 89 c3 mov %eax,%ebx 40055a: 8b 45 ec mov -0x14(%rbp),%eax 40055d: 83 e8 02 sub $0x2,%eax 400560: 89 c7 mov %eax,%edi 400562: e8 be ff ff ff callq 400525 <mystery> 400567: 01 d8 add %ebx,%eax 400569: 48 83 c4 18 add $0x18,%rsp 40056d: 5b pop %rbx 40056e: 5d pop %rbp 40056f: c3 retq A (5 pts). Does this assembly code appear to follow the 32-bit or 64-bit parameterpassing guidelines? How can you tell? The function uses mostly 64-bit registers. Also, input arguments are passed using %edi which indicates parameter passing through registers another sign of a 64- bit architecure. Don t be confused by the presence of the base pointer register, %rbp. A frame base pointer can still be used in the 64-bit architecture.

B (5 pts). Why is %rbx pushed onto the stack initially and then popped at the end? %rbx is a callee-saved register, and the code uses %rbx (or $ebx) so it must save it before it overwrites it and then restore it before it returns. C (5 pts). There are two if() statements in the code that produced this assembly. At which instruction addresses do they begin? 0x400531 and 0x40053e, where both of the comparisons occur. D (5 pts). What does the byte ec most likely correspond to in the instruction at 0x40055a? ec is the twos-complement version of -0x14, the offset being used in the move instruction at that address.

E (15 pts). Write out C code that would assemble into the routine above. unsigned int mystery(unsigned int n) { if (n == 0) return 0; if (n == 1) return 1; return ( mystery(n-1) + mystery(n-2) ); } F (5 pts). What does this function do? It returns the nth Fibonacci number (0,1,1,2,3,5,8,...).

4. Stack Discipline (25 points) Consider a stack from an IA32 machine with the following contents: Line ref number Address in memory Value in memory Check if ret addr Check if arg or local var Check if saved ebp 22 0xfffffffc 0x00000001 21 0xfffffff8 0x00000005 20 0xfffffff4 0xfffffffc 19 0xfffffff0 0x004080a0 18 0xffffffec 0xfffffffc 17 0xffffffe8 0x00000005 16 0xffffffe4 0x0040801e 15 0xffffffe0 0xffffffec 14 0xffffffdc 0x00000004 13 0xffffffd8 0x0040801e 12 0xffffffd4 0xffffffe0 11 0xffffffd0 0x00000003 10 0xffffffcc 0x0040801e 9 0xffffffc8 0xffffffd4 8 0xffffffc4 0x00000002 7 0xffffffc0 0x0040801e 6 0xffffffbc 0xffffffc8 5 0xffffffb8 0x00800000 4 0xffffffb4 0x008000d0 3 0xffffffb0 0x00000001 2 0xffffffac 0x00000001 1 0xffffffa8 0x00408053 0xffffffa4 0xffffffa0 Furthemore, you know that your code is in memory in locations from 0x00400000 to 0x005fffff and that your dynamic data heap is in locations 0x00800000 to 0x009fffff. A (5 pts). Assume that machine execution has just been stopped just before the first instruction of a procedure. What address will we return to after that procedure completes? The return address last placed on the stack in line 1, or 0x00408053.

B (5 pts). How much space did the calling procedure making this last call allocate on the stack for local variables and arguments? List the reference numbers of stack elements. 2, 3, 4, 5 C (10 pts). Annotate the stack on the previous page with the type of data stored at that location on the stack by placing a check mark in the appropriate column. D (5 pts). Is there a recursive procedure on the stack? If so, how many calls deep is the recursion at the point represented by the stack above? Yes, the four return addresses at lines 7, 10, 13, and 16 are the same indicating that we are four deep into a recursion.

REFERENCES Powers of 2: 2 0 = 1 2 1 = 2 2-1 =.5 2 2 = 4 2-2 =.25 2 3 = 8 2-3 =.125 2 4 = 16 2-4 =.0625 2 5 = 32 2-5 =.03125 2 6 = 64 2-6 =.015625 2 7 = 128 2-7 =.0078125 2 8 = 256 2-8 =.00390625 2 9 = 512 2-9 =.001953125 2 10 = 1024 2-10 =.0009765625 Assembly Code Instructions: push pop call ret mov lea add sub and or shr jmp cmp jne push a value onto the stack and decrement the stack pointer pop a value from the stack and increment the stack pointer jump to a procedure after first pushing a return address onto the stack pop return address from stack and jump there move a value between registers and memory compute effective address and store in a register add 1 st operand to 2 nd with result stored in 2 nd subtract 1 st operand from 2 nd with result stored in 2 nd bit-wise AND of two operands with result stored in 2 nd bit-wise OR of two operands with result stored in 2 nd shift data by 1 bit to the right jump to address subtract 1 st operand from 2 nd and set flags conditional jump to address if zero flag is not set