CS , Fall 2007 Exam 1

Similar documents
15-213, Fall 2007 Midterm Exam

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

CS , Fall 2009 Exam 1

CS , Fall 2009 Exam 1

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

CS , Spring 2004 Exam 1

CS , Fall 2002 Exam 1

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

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

CS , Fall 2004 Exam 1

CS , Fall 2001 Exam 1

CSE 351 Midterm - Winter 2015 Solutions

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

Question Points Score Total: 100

15-213/18-213, Fall 2012 Final Exam

Recitation 15: Final Exam Preparation

EECS 213 Fall 2007 Midterm Exam

CSE 351 Midterm - Winter 2017

CSE 351 Midterm - Winter 2015

Do not turn the page until 5:10.

1 Number Representation(10 points)

CS , Fall 2001 Exam 2

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

Areas for growth: I love feedback

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CSE351 Spring 2018, Midterm Exam April 27, 2018

Review addressing modes

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

3. Address Translation (25 pts)

CS , Fall 2001 Exam 2

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

CSE 351 Midterm Exam

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

CSCI 2021: x86-64 Control Flow

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

Do not turn the page until 5:10.

CS , Spring 2002 Exam 2

(18-243), Fall 2010 Final Exam

CSE 351 Spring 2017 Midterm Exam (8 May 2017)

Control flow (1) Condition codes Conditional and unconditional jumps Loops Conditional moves Switch statements

x86 Programming II CSE 351 Winter

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Do not turn the page until 11:30.

Question 1: Number Representation

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Setting & Examining Condition Codes. int gt(long x, long y) { return x > y; } int has_nonzero_masked(long x, long mask) { return!!

last time Assembly part 2 / C part 1 condition codes reminder: quiz

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

Do not turn the page until 5:10.

Machine/Assembler Language Putting It All Together

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Do not turn the page until 12:30.

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

Do not turn the page until 5:10.

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

x86-64 Programming II

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

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

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

UW CSE 351, Winter 2013 Midterm Exam

x86 64 Programming II

CS , Fall 2003 Exam 2

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Machine-Level Programming (2)

Procedures and the Call Stack

Do not turn the page until 12:30.

Corrections made in this version not seen in first lecture:

Assembly II: Control Flow

Question F5: Caching [10 pts]

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CS , Fall 1998 Final Exam

Assembly Programming III

Outline. x86 Architecture

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

CS 3843 Final Exam Fall 2012

CS 3330 Exam 1 Fall 2017 Computing ID:

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

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

Do not turn the page until 12:30.

Processes, Signals, I/O, Shell Lab and Midterm Review

Do not turn the page until 12:30.

Datorarkitektur, 2009 Tentamen

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

CS / ECE , Spring 2010 Exam 1

Introduction Presentation A

CSE 351 Spring 2017 Final Exam (7 June 2017)

Computer Systems C S Cynthia Lee

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

u Arrays One-dimensional Multi-dimensional (nested) Multi-level u Structures Allocation Access Alignment u Floating Point

Systems Programming and Computer Architecture ( )

CS429: Computer Organization and Architecture

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine Language CS 3330 Samira Khan

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine Programming 3: Procedures

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

How Software Executes

Transcription:

Andrew login ID: Full Name: CS 15-213, Fall 2007 Exam 1 Wednesday October 17, 2007 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write your answers in the space provided below the problem. If you make a mess, clearly indicate your final answer. The exam has a maximum score of XX points. The problems are of varying difficulty. The point value of each problem is indicated. Pile up the easy points quickly and then go back to the harder problems. This exam is OPEN BOOK. You may use any books or notes you like. Calculators are allowed, but no other electronic devices. Good luck! 1 (8): 2 (8): 3 (8): 4 (6): 5 (8): 6 (8): 7 (10): TOTAL (XX): Page 1 of 10

Problem 1. (8 points): Assume we are running code on an IA32 machine, which has a 32-bit word size and uses two s complement arithmetic for signed integers. Consider the following definitions: int x = foo(); unsigned ux = x; Fill in the empty boxes in the table below. For each of the C expressions in the first column, either: State that it is true of all possible values returned by foo(), or Give an example where it is not true. Puzzle True / Counterexample x < 0 (x*2) < 0 False (TMin) x < 0 (x-1) < 0 x >= 0 ((x ˆ (x>>31))) <= 0 x > 0 (x+ux) > 0U x < 0 ( x+1) > 0 ( (x>>31)+1) == (x<0) (-(!x) & x) == 0 ( (ux>>31) & ux)!= ux x >= 0 -(( x >> 1) + 1) > 0 Page 2 of 10

Problem 2. (8 points): Consider the following 5-bit floating point representation based on the IEEE floating point format. This format does not have a sign bit it can only represent nonnegative numbers. There are k = 3 exponent bits. The exponent bias is 3. There are n = 2 fraction bits. Numeric values are encoded as a value of the form V = M 2 E, where E is exponent after biasing, and M is the significand value. The fraction bits encode the significand value M using either a denormalized (exponent field 0) or a normalized representation (exponent field nonzero). Below, you are given some decimal values, and your task it to encode them in floating point format. If rounding is necessary, you should use round-to-even, as you did in Lab 1 for the float i2f puzzle. In addition, you should give the rounded value of the encoded floating point number. Give these as whole numbers (e.g., 17) or as fractions in reduced form (e.g., 3/4). Value Floating Point Bits Rounded value 9/32 001 00 1/4 7/8 15/16 9 10 Page 3 of 10

Problem 3. (8 points): Consider the following C function s x86-64 assembly code: # On entry %edi = n # 0000000004004a8 <foo>: 4004a8: b8 00 00 00 00 mov $0x0,%eax 4004ad: 83 ff 01 cmp $0x1,%edi 4004b0: 7e 1a jle 4004cc <foo+0x24> 4004b2: 01 f8 add %edi,%eax 4004b4: ba 00 00 00 00 mov $0x0,%edx 4004b9: 39 fa cmp %edi,%edx 4004bb: 7d 08 jge 4004c5 <foo+0x1d> 4004bd: 01 d0 add %edx,%eax 4004bf: ff c2 inc %edx 4004c1: 39 fa cmp %edi,%edx 4004c3: 7c f8 jl 4004bd <foo+0x15> 4004c5: ff cf dec %edi 4004c7: 83 ff 01 cmp $0x1,%edi 4004ca: 7f e6 jg 4004b2 <foo+0xa> 4004cc: f3 c3 repz retq # treat repz as a no-op Please fill in the corresponding C code: int foo (int n) { int a, i; a = 0; for (; n > ; ) { a = a + ; for (i = ; i < ; ) a = a + ; return ; Page 4 of 10

Problem 4. (6 points): Consider the C code below, where H and J are constants declared with #define. int array1[h][j]; int array2[j][h]; int copy_array(int x, int y) { array2[y][x] = array1[x][y]; return 1; Suppose the above C code generates the following x86-64 assembly code: # On entry: # %edi = x # %esi = y # copy_array: movslq %esi,%rsi movslq %edi,%rdi movq %rsi, %rax salq $7, %rax subq %rsi, %rax addq %rdi, %rax leaq (%rdi,%rdi,2), %rdi addq %rsi, %rdi movl array1(,%rdi,4), %edx movl %edx, array2(,%rax,4) movl $1, %eax ret What are the values of H and J? H = J = Page 5 of 10

Problem 5. (6 points): Consider the following C declarations: typedef struct { int x; int y; int sensor_id; Sensor; typedef struct { int sensor_id; double data; Data; typedef struct { Sensor *sensor; char status; Sensor_Status; Sensor sensor; Data data; Sensor_Status status_array[10]; For all of the following, assume x86-64 alignment. A. What is the total allocated space for sensor? bytes B. What is the total wasted space for sensor? bytes C. What is the total allocated space for data? bytes D. What is the total wasted space for data? bytes E. What is the total allocated space for status_array? bytes F. What is the total wasted space for status_array? bytes Page 6 of 10

Problem 6. (8 points): Consider the following data structure declarations: struct node { struct data d; struct node *next; ; struct data { int x; char str[6]; ; Below are given four C functions and four x86-64 code blocks. Next to each of the x86-64 code blocks, write the name of the C function that it implements. int alpha(struct node *ptr) { return ptr->d.x; char *beta(struct node *ptr) { ptr = ptr->next; return ptr->d.str; movq addq ret movq ret 16(%rdi), %rax $4, %rax %rdi, %rax char gamma (struct node *ptr) { return ptr->d.str[4]; movl ret (%rdi), %eax int *delta (struct node *ptr) { struct data *dp = (struct data *) ptr; return &dp->x; movsbl ret 8(%rdi),%eax Page 7 of 10

Reverse Engineering Switch Code The next problem concerns the code generated by GCC for a function involving a switch statement. Following a bounds check, the code uses a jump to index into the jump table 400476: ff 24 d5 a0 05 40 00 jmpq *0x4005a0(,%rdx,8) Using GDB, we extract the 8-entry jump table as: 0x4005a0: 0x0000000000400480 0x4005b0: 0x0000000000400480 0x4005c0: 0x0000000000400480 0x4005d0: 0x0000000000400485 0x0000000000400491 0x0000000000400496 0x0000000000400489 0x0000000000400496 The following block of disassembled code implements the branches of the switch statement 400480: 48 8d 04 3f lea (%rdi,%rdi,1),%rax 400484: c3 retq 400485: 48 0f af f7 imul %rdi,%rsi 400489: 48 89 f8 mov %rdi,%rax 40048c: 48 21 f0 and %rsi,%rax 40048f: 90 nop 400490: c3 retq 400491: 48 8d 04 37 lea (%rdi,%rsi,1),%rax 400495: c3 retq 400496: 48 8d 46 ff lea 0xffffffffffffffff(%rsi),%rax 40049a: c3 retq Page 8 of 10

Problem 7. (10 points): Fill in the blank portions of the C code below to reproduce the function corresponding to this object code. You can assume that the first entry in the jump table is for the case when s equals 0. Parameters a, b, and s are passed in registers %rdi, %rsi, and %rdx, respectively. long fun(long a, long b, long s) { long result = 0; switch (s) { case : case : result = ; break; case : b = ; /* Fall through */ case : result = ; break; case : result = ; break; default: result = ; return result; Page 9 of 10

Problem 8. (16 points): Consider a computer with an 8-bit address space and an associative 64-byte data cache, with a LRU replacement policy. Assume that the cache is 2-way set associative and has 8-byte cache lines. The boxes below represent the bit-format of a physical address. In each box, indicate which field that bit represents (it s possible that a field doesn t exist). Here are the fields: CO The byte offset within the cache line CI The cache (set) index CT The cache tag 7 6 5 4 3 2 1 0 The table below on the left shows a trace of load addresses accessed in the data cache. Below on the right is a list of possible final states of the cache, showing the hex value of the tag for each cache line in each set. Which is the correct final cache state? How many of the loads were hits? Assume that initially all cache lines are invalid (represented by X), and that sets are filled from left to right. Load No. Hex Address Binary Address 1 1a 0001 1010 2 55 0101 0101 3 e6 1110 0110 4 53 0101 0011 5 77 0111 0111 6 28 0010 1000 7 94 1001 0100 8 a6 1010 0110 9 75 0111 0101 10 c7 1100 0111 11 56 0101 0110 (a) (b) (c) (d) (e) (f) (g) 0 X 4 2 1 X 6 5 0 X 2 3 1 X 6 5 0 X 2 4 1 X 6 5 0 X 2 3 1 X 7 6 0 X 4 2 1 X 7 6 X 0 5 2 1 3 7 6 0 2 4 2 1 3 5 6 Correct final state: Number of hits: Page 10 of 10

Problem 9. (7 points): Consider the C code below: int forker(int x) { int pid; printf("a"); if (x > 0) { pid = fork(); printf("b"); if (pid == 0) { printf("c"); else { waitpid(pid, NULL, 0); printf("d"); if (x == 5) { pid = fork(); if (pid!= 0) { exit(8); printf ("E"); printf("f"); exit(4); Consider each of the following outputs and circle the ones that could be produced by the code above (after all processes are terminated). AA ABCDEF ABCBFDF ABCBDFF ABCBFDE ABCFBDF ABBCFDEF Page 11 of 10