CSE 351 Midterm - Winter 2015 Solutions

Similar documents
CSE 351 Midterm - Winter 2015

CSE 351 Midterm - Winter 2017

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

1 Number Representation(10 points)

Do not turn the page until 5:10.

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE 351 Midterm Exam

Do not turn the page until 5:10.

UW CSE 351, Winter 2013 Midterm Exam

CSE 351 Midterm Exam Spring 2016 May 2, 2015

CSE 351 Spring 2017 Midterm Exam (8 May 2017)

Do not turn the page until 5:10.

Do not turn the page until 11:30.

CSE351 Autumn 2013 Final Exam (11 Dec 2013)

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

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

Do not turn the page until 5:10.

Question Points Score Total: 100

Computer Systems C S Cynthia Lee

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

Question 1: Number Representation

Review addressing modes

CS , Spring 2004 Exam 1

Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

x86 Programming II CSE 351 Winter

Do not turn the page until 12:30.

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

CS , Fall 2007 Exam 1

x86-64 Programming II

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

15-213, Fall 2007 Midterm Exam

University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse March 14, CSE 351 Final Exam. Last Name: First Name:

Machine Program: Procedure. Zhaoguo Wang

CS 3330 Exam 1 Fall 2017 Computing ID:

Instructions and Instruction Set Architecture

CSCI 2021: x86-64 Control Flow

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

Do not turn the page until 12:30.

Machine-Level Programming (2)

x86 64 Programming II

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

Assembly Programming III

Areas for growth: I love feedback

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

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

Assembly Language II: Addressing Modes & Control Flow

The Stack & Procedures

Intel x86-64 and Y86-64 Instruction Set Architecture

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

CS429: Computer Organization and Architecture

University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse March 14, CSE 351 Final Exam. Last Name: First Name:

Buffer Overflow Attack (AskCypert CLaaS)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

CS367 Test 1 Review Guide

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Machine-Level Programming III: Procedures

Machine-level Programs Procedure

Computer Organization: A Programmer's Perspective

231 Spring Final Exam Name:

Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration. Xeno Kovah

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Machine Language CS 3330 Samira Khan

Machine- Level Representa2on: Procedure

x86-64 Programming III & The Stack

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

Question Points Score Total: 100

Do not turn the page until 12:30.

CS , Fall 2001 Exam 1

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Winter 2017 March 2, 2017 CS107 2 nd Midterm Examination

CSE 351 Final Exam - Winter 2017

CS / ECE , Spring 2010 Exam 1

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

CS , Fall 2002 Exam 1

x86-64 Programming III

18-600: Recitation #3

CS , Fall 2004 Exam 1

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

Assembly Programming IV

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Question F5: Caching [10 pts]

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

CSE351 A, Grossman, Midterm Exam

Computer Architecture I: Outline and Instruction Set Architecture. CENG331 - Computer Organization. Murat Manguoglu

Do not turn the page until 12:30.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 5

cmovxx ra, rb 2 fn ra rb irmovq V, rb 3 0 F rb V rmmovq ra, D(rB) 4 0 ra rb mrmovq D(rB), ra 5 0 ra rb OPq ra, rb 6 fn ra rb jxx Dest 7 fn Dest

Machine/Assembler Language Putting It All Together

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Lecture 3 CIS 341: COMPILERS

Instruction Set Architectures

How Software Executes

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

Machine Programming 3: Procedures

Computer Architecture and System Programming Laboratory. TA Session 3

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Transcription:

CSE 351 Midterm - Winter 2015 Solutions February 09, 2015 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 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. 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 (no summary sheets, no calculators, no mobile phones, no laptops). 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. Good Luck! Name: Student ID: Section: Problem Max Score Score 1 20 2 20 3 25 4 35 EC 15 TOTAL 100 1

1. Number Representation (20 points) Integers (a) Assuming unsigned integers, what is the result when you compute UMAX+1? 0 (b) Assuming two s complement signed representation, what is the result when you compute TMAX+1? TMIN (0x80000000) Floating Point (c) Give M and E in the floating point representation of 3.75. Express each in both decimal and binary. (Remember, E is the actual value of the exponent, not the encoding with bias) Binary E 1 1 Decimal M 1.111 or.111 1.875 or.875 Because the format of M was unspecified, either with or without implicit 1 was acceptable (d) Is the == operator a good test of equality for floating point values? Why or why not? No, the == operator is not a good test of equality because floating point numbers often have a margin of error. Casting and Pointers (e) Given the following code: float f = 5.0; int i = (int) f; int j = *((int *)&f); Does i==j return true or false? Explain. i!= j because i will contain the estimate of f as an integer while j contains the bit pattern representation of 5.0 in floating point. 2 of 8

2. Assembly and C (20 points) Consider the following x86-64 assembly and C code: <do_something>: cmp $0x0,%rsi jle <end> xor %rax,%rax sub $0x1,%rsi <loop>: lea (%rdi,%rsi, 2 ),%rdx add (%rdx),%ax sub $0x1,%rsi jns <loop> <end>: retq short do_something(short* a, int len) { short result = 0; for (int i = len - 1; i >= 0 ; i-- ) { result += a[i] ; return result; (a) Both code segments are implementations of the unknown function do something. Fill in the missing blanks in both versions. (Hint: %rax and %rdi are used for result and a respectively. %rsi is used for both len and i) (b) Briefly describe the value that do something returns and how it is computed. Use only variable names from the C version in your answer. do something returns the sum of the shorts pointed to by a. backwards. It does so by traversing the array 3 of 8

3. Pointers and Values (25 points) Consider the following variable declarations assuming x86-64 architecture: int x; int y[11] = {0,1,2,3,4,5,6,7,8,9,10; int z[][5] = {{210, 211, 212, 213, 214, {310, 311, 312, 313,314; int aa[3] = {410, 411, 412; int bb[3] = {510, 511, 512; int cc[3] = {610, 611, 612; int *w[3] = {aa, bb, cc; Variable aa bb cc w y z Address of First Element 0x000 0x100 0x200 0x300 0x500 0x600 (a) Fill in the table below with the address, value, and type of the given C expressions. Answer N/A if it is not possible to determine the address or value of the expression. The first row has been filled in for you. C Expression Address Value Type (int/int*/int**) x 0x400 N/A int *&x 0x400 N/A int y N/A 0x500 int* *y 0x500 0 int y[0] 0x500 0 int *(y+1) 0x504 1 int &(y[10]) N/A 0x528 int* z[0]+1 N/A 0x604 int* *(z[0]+1) 0x604 211 int z[0][6] 0x618 311 int w[1] 0x308 0x100 int* w[2][0] 0x200 610 int 4 of 8

4. Recursion (35 points) The fictional Fibonatri sequence is defined recursively for n=0,1,... by the following C code: int fibonatri(int n) { if (n == 0) { return 0; else if (n == 1) { return 1; else if (n == 2) { return 2; else { return fibonatri(n-3) - fibonatri(n-2) + fibonatri(n-1); Here is a disassembly of fibonatri(): 000000000040057b <fibonatri>: 40057b: 53 push %rbx 40057c: 48 83 ec 10 sub $0x10,%rsp 400580: 89 7c 24 0c mov %edi,0xc(%rsp) 400584: 83 7c 24 0c 00 cmpl $0x0,0xc(%rsp) 400589: 75 07 jne 400592 <fibonatri+0x17> 40058b: b8 00 00 00 00 mov $0x0,%eax 400590: eb 4c jmp 4005de <fibonatri+0x63> 400592: 83 7c 24 0c 01 cmpl $0x1,0xc(%rsp) 400597: 75 07 jne 4005a0 <fibonatri+0x25> 400599: b8 01 00 00 00 mov $0x1,%eax 40059e: eb 3e jmp 4005de <fibonatri+0x63> 4005a0: 83 7c 24 0c 02 cmpl $0x2,0xc(%rsp) 4005a5: 75 07 jne 4005ae <fibonatri+0x33> 4005a7: b8 02 00 00 00 mov $0x2,%eax 4005ac: eb 30 jmp 4005de <fibonatri+0x63> 4005ae:???????? mov 0xc(%rsp),%eax 4005b2: 83 e8 03 sub $0x3,%eax 4005b5: 89 c7 mov %eax,%edi 4005b7: e8 bf ff ff ff callq 40057b <fibonatri> 4005bc: 89 c3 mov %eax,%ebx 4005be: 8b 44 24 0c mov 0xc(%rsp),%eax 4005c2: 83 e8 02 sub $0x2,%eax 4005c5: 89 c7 mov %eax,%edi 4005c7:???????? callq 40057b <fibonatri> 4005cc: 29 c3 sub %eax,%ebx 4005ce: 8b 44 24 0c mov 0xc(%rsp),%eax 4005d2:???????? sub $0x1,%eax 4005d5: 89 c7 mov %eax,%edi 4005d7: e8 9f ff ff ff callq 40057b <fibonatri> 4005dc:???????? add %ebx,%eax 4005de: 48 83 c4 10 add $0x10,%rsp 4005e2: 5b pop %rbx 4005e3: c3 retq 5 of 8

(a) Fill in the four blanks in the disassembly. You should be able to gather hints from the surrounding code. (b) What register is used to pass the single argument to fibonatri()? %edi (c) Why is the register %rbx pushed onto the stack at the beginning of the function? %rbx is pushed on to the stack because it is a callee saved register and is used during fibonatri(). (d) Why are iterative solutions generally preferred over recursive solutions from a memory usage perspective? How much of the stack is used during each iteration of fibonatri()? Because fibonatri() is recursive, each call to fibonatri() creates a new stack frame. From a memory usage perspective this can use large amounts of the stack and has the possibility of overflowing the stack if it is called too many times. The stack frame of fibonatri() is 32 bytes. 8 bytes for %rbx, 16 bytes for local variables, and 8 bytes for the return address. (e) What pattern do numbers in the Fibonatri sequence follow? 0, 1, 2, 1, 0, 1, 2, 1,... Extra Credit (15 points) Write a non-recursive function in C with the same output as fibonatri() using only a switch statement (Hint: use the modulus % operator) int fibonatri_non_recursive(int n) { switch(n % 4){ case 0: return 0; case 1: return 1; case 2: return 2; case 3: return 1; 6 of 8

References Powers of 2: 2 0 = 1 2 1 = 2 2 1 = 0.5 2 2 = 4 2 2 = 0.25 2 3 = 8 2 3 = 0.125 2 4 = 16 2 4 = 0.0625 2 5 = 32 2 5 = 0.03125 2 6 = 64 2 6 = 0.015625 2 7 = 128 2 7 = 0.0078125 2 8 = 256 2 8 = 0.00390625 2 9 = 512 2 9 = 0.001953125 2 10 = 1024 2 10 = 0.0009765625 Hex help: 0x00 = 0 0x0A = 10 0x0F = 15 0x20 = 32 0x28 = 40 0x2A = 42 0x2F = 47 Assembly Code Instructions: push pop call ret mov lea 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 add src (1 st operand) to dst (2 nd ) with result stored in dst (2 nd ) sub subtract src (1 st operand) from dst (2 nd ) with result stored in dst (2 nd ) and bit-wise AND of src and dst with result stored in dst or bit-wise OR of src and dst with result stored in dst sar shift data in the dst to the right (arithmetic shift) by the number of bits specified in 1 st operand jmp jne jns cmp test jump to address conditional jump to address if zero flag is not set conditional jump to address if sign flag is not set subtract src (1 st operand) from dst (2 nd ) and set flags bit-wise AND src and dst and set flags 7 of 8

Register map for x86-64: Note: all registers are caller-saved except those explicitly marked as callee-saved, namely, rbx, rbp, r12, r13, r14, and r15. rsp is a special register. %rax Return Value %r8 Argument #5 %rbx Callee Saved %r9 Argument #6 %rcx Argument #4 %r10 Caller Saved %rdx Argument #3 %r11 Caller Saved %rsi Argument #2 %r12 Callee Saved %rdi Argument #1 %r13 Callee Saved %rsp Stack Pointer %r14 Callee Saved %rbp Callee Saved %r15 Callee Saved 8 of 8