CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Similar documents
CS 2505 Computer Organization I

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Do not start the test until instructed to do so!

CS 2505 Computer Organization I

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Do not start the test until instructed to do so!

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Credits and Disclaimers

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

Credits and Disclaimers

Do not start the test until instructed to do so!

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS 2506 Computer Organization II Test 1

Do not start the test until instructed to do so!

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

Credits and Disclaimers

UW CSE 351, Winter 2013 Midterm Exam

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CS 3214 Computer Systems. Do not start the test until instructed to do so! printed

Pointer Casts and Data Accesses

CSE351 Spring 2018, Midterm Exam April 27, 2018

Solution READ THIS NOW! CS 3114 Data Structures and Algorithms

CS61, Fall 2012 Midterm Review Section

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Machine Programming 3: Procedures

Machine Program: Procedure. Zhaoguo Wang

Pointer Accesses to Memory and Bitwise Manipulation

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

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

Credits and Disclaimers

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

CSE 351 Midterm - Winter 2015 Solutions

CS , Fall 2004 Exam 1

Accessing Data in Memory

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

CSE 351 Midterm - Winter 2017

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

CS 2506 Computer Organization II Test 1. Do not start the test until instructed to do so! printed

CS 2506 Computer Organization II Test 2

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

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

Important From Last Time

Areas for growth: I love feedback

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

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

CS 2506 Computer Organization II

Important From Last Time

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

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

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

CPS104 Recitation: Assembly Programming

CS61 Section Solutions 3

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Credits and Disclaimers

gcc o driver std=c99 -Wall driver.c bigmesa.c

CS 2504 Intro Computer Organization Test 1

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Assembly Language: Function Calls

CS 2506 Computer Organization II Test 2. Do not start the test until instructed to do so! printed

CS , Fall 2002 Exam 1

Pointer Accesses to Memory and Bitwise Manipulation

Assembly Language: Function Calls" Goals of this Lecture"

Do not start the test until instructed to do so!

Representation of Information

CS 3114 Data Structures and Algorithms READ THIS NOW!

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

CS 2506 Computer Organization II Test 2. Do not start the test until instructed to do so! printed

Assembly Language: Function Calls" Goals of this Lecture"

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

GCC and Assembly language. GCC and Assembly language. Consider an example (dangeous) foo.s

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

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

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

Assembly Language II: Addressing Modes & Control Flow

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

CS241 Computer Organization Spring 2015 IA

Pointer Accesses to Memory and Bitwise Manipulation

Assembly II: Control Flow

Do not turn the page until 5:10.

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

1 Number Representation(10 points)

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

CS 3330 Exam 1 Fall 2017 Computing ID:

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

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

The Hardware/Software Interface CSE351 Spring 2013

CS 2506 Computer Organization II

CSE 351 Midterm - Winter 2015

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

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

CS , Fall 2007 Exam 1

Transcription:

Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page fact sheet. Your fact sheet may contain definitions and examples, but it may not contain questions and/or answers taken from old tests or homework. You may include examples from the course notes. No calculators or other electronic devices may be used. The use of any such device will be interpreted as an indication that you are finished with the test and your test form will be collected immediately. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that and label the continuation with the question number. If you want partial credit, justify your answers, even when justification is not explicitly required. There are 5 questions, some with multiple parts, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. If you brought a fact sheet to the test, write your name on it and turn it in with the test. Note that either failing to return this test, or discussing its content with a student who has not taken it is a violation of the Honor Code. Do not start the test until instructed to do so! Name Solution printed Pledge: On my honor, I have neither given nor received unauthorized aid on this examination. signed C 1

xkcd.com C 2

1. [14 points] Consider the following two C data types: struct _Score { uint8_t high; uint8_t low; ; typedef struct _Score Score; struct _Result { char winner; char loser; Score score; ; typedef struct _Result Result; Implement the C function shown below, conforming to the header comment. Violations of the restrictions will earn a score of zero. Be careful of syntax in your answer! The function could be called in this manner: const int nresults = 1000; Result pres = malloc(nresults sizeof(result)); // initialize Result array Score averagescore; avgscore(&averagescore, pres, nresults); / Computes an "average" score for an array of Result objects. Pre: pscore points to a Score object pres points to an array of nres Result objects Post: pscore has been updated to store the average high and low point values represented in the array pres[] Restrictions: You may only use pointer syntax when accessing the elements in the array of Result objects. / void avgscore(score const pscore, const Result pres, uint32_t nres) { const Result current = pres; uint32_t sumhigh = 0, sumlow = 0; for (uint32_t idx = 0; idx < nres; idx++) { sumhigh += current->score.high; sumlow += current->score.low; current++; pscore->high = sumhigh / nres; pscore->low = sumlow / nres; Notes: A uint8_t will overflow if its value exceeds 255, so you really need to use a "wider" type to accumulate the sums (I did not deduct points for that). Nothing in the preconditions says that the fields in pscore have been initialized, so you need to set them to zero if you use them to accumulate the sums. The "dot" operator must have the name of a struct variable on its left and the name of a field of that struct variable on its right current points to a Result object; Result objects have a field named score of type Score; so you access the high field of the Score object by writing current->score.high. C 3

2. Suppose a hexdump of a memory region shows the following bytes: 0 1 2 3 4 5 6 7 8 9 A B C D E F ---------------------------------------------------------- 0 39 86 B5 BF C5 52 A2 3E 8C 31 3B 1B 27 94 EE 5E 1 69 2F 8E FC AF F5 F5 56 C0 FE 20 A7 C4 45 9C 9D 2 CB 51 CC 91 A3 6E CF 30 9F 0A 4B C6 C6 39 24 07 3 68 B3 04 18 A8 B5 6E 69 B3 8E 10 78 D3 AC 15 9F a) [8 points] If the four bytes here are interpreted to represent the value of an int32_t variable, is that variable negative or nonnegative? Justify your conclusion. Integers are stored in little-endian order (on our machines); the high byte is "BF", so the high nybble is 1011, so the high bit is 1, so the value is negative. The question did not specify whether the data was stored in big-endian or little-endian order. Little-endian order is the rational assumption, given the nature of current systems. If you explicitly said you were assuming big-endian order, I intended to give full credit. If your answer implied you assumed that, but you were not explicit, I made a deduction. b) [8 points] Suppose that the uint8_t variable P points to the first byte in the memory region shown above. Complete the C statement below so that the variable X is assigned the value referred to in part a). uint32_t X = (uint32_t)p; To get a uint32_t value, you need to dereference a pointer whose target type is 4 bytes wide, hence the typecast of P to uint32_t. c) [8 points] Suppose that the uint32_t variable Q points to the first byte in the memory region shown above. Complete the C statement below so that the pointer R will point to this byte in the memory region. uint8_t R = (uint8_t)q + 15; The byte in question is 15 bytes after the target of Q; I typecast to get a pointer with a 1- byte target, then added 15. Other solutions are possible. There's no dereference because we want to make R point to the correct byte, not give R the value of that byte. C 4

d) [12 points] Implement the following function, using only pointer notation: / Returns the value of the first byte in the memory region that occurs in two or more consecutive locations. For the region given above, the function would return the value 0xF5. Pre: P points to the first byte of a block of length nbytes. / uint8_t firstdupe(const uint8_t P, uint32_t nbytes) { for (uint32_t offset = 0; offset < nbytes - 1; offset++) { if ( (P + offset) == (P + offset + 1) ) { return (P + offset); return 0; There's a basic flaw in the question, since there's no value reserved (or specified) to return if no suitable pair of bytes is found I was not picky about what you returned in that case. It would have been better to return a pointer to the relevant (first) byte; then NULL could have been used to indicate no pair was found. Some errors (common or otherwise): Not allowing for the possibility that no match would be found no matter what you return in that case, you cannot just assume there will be a match. Going one byte of bounds if the traversal reaches the end of the data region. Not stopping with the first match. Using array index notation instead of pointer notation (read the instructions). C 5

3. The short C function shown below compiles to the assembly code shown below: int q3(int x, int y) { int a = x + y; // 1 int b = x - y; // 2 return a b; // 3 q3: pushq %rbp # 1 movq %rsp, %rbp # 2 subq $24, %rsp # 3 movl %edi, -16(%rbp) # 4 movl %esi, -20(%rbp) # 5 movl -20(%rbp), %eax # 6 movl -16(%rbp), %edx # 7 addl %edx, %eax # 8 movl %eax, -12(%rbp) # 9 movl -20(%rbp), %eax # 10 movl -16(%rbp), %edx # 11 subl %eax, %edx # 12 movl %edx, %eax # 13 movl %eax, -4(%rbp) # 14 movl -12(%rbp), %eax # 15 imull -4(%rbp), %eax # 16 leave # 17 ret # 18 a) [6 points] Line 1 of the C code is translated into lines 6 through 9 of the assembly code. Why does gcc chose to use movl and addl instructions for this, but chooses movq and subq instructions for lines 2 and 3 of the assembly code? Instructions 2 and 3 are manipulating 8-byte pointer values in registers rsp and rbp; instructions 6 through 9 are manipulating the parameters x and y and the local variable a, which are 4-byte int values. The question was about WHY the compiler chose the specific instructions, not about the EFFECT of that choice. Many answers did not relate the choice to the data types that were being manipulated. b) [6 points] In relation to the C code, what is the assembly instruction in line 16 doing? Computing a b and placing it in the eax register to be the return value from q3(). There were two parts to a complete answer, the product of the parameters is being computed, and the return value is being set. Most answers did not address the second issue. c) [6 points] Many of the assembly instructions shown above access registers. Which of these assembly instructions access RAM (and possibly registers as well)? (Just list the numbers of the relevant instructions.) 1, 4, 5, 6, 7, 9, 10, 11, 14, 15, 16, 18 1 pushes a value to the stack; 18 pops a value from the stack; the stack is stored RAM. Each of the other instructions listed above explicitly uses an address expression as a parameter. C 6

d) [8 points] Using the variable names from the C code, show what (if anything) is stored at each occupied location in the stack frame for the function q3. If a frame location is not occupied, leave it blank. rbp rbp - 4 old value of rbp b rbp - 8 rbp - 12 rbp - 16 rbp - 20 a x y rbp - 24 rbp - 28 The assembly code shows the two parameters, x and y, being copied to the stack frame at addresses rbp - 16 and rbp - 20, respectively (lines 4 and 5). The addl instruction (line 8) is computing x + y; the following movl instruction is storing that sum to rbp - 12 on the stack, which is must be where a is stored. Similarly, the subl instruction (line 12) is computing x - y; the following movl instruction is storing that value to rbp - 4 on the stack, which must be where b is stored. C 7

4. [12 points] Implement the following C function, obeying all restrictions. Violations of the restrictions will earn a score of zero. / Determines whether every byte in the word N represents an odd value. Allowed operations:! ~ & ^ << >> No more than 20 uses of operators. No use of loops, if, if-else, switches. No declarations of constants wider than one byte. Returns 1 if every byte represents an odd value, 0 otherwise. Examples: 00000000 00000000 00000000 00000000 : 0 00000000 00001110 01100010 10000010 : 0 10000011 10000001 10000001 10000111 : 1 01000011 10000001 00000001 00000001 : 1 / int bytesareodd(int N) { int lowbitmask = 0x01; int lowbit01 = N & lowbitmask; int lowbit02 = (N >> 8) & lowbitmask; int lowbit03 = (N >> 16) & lowbitmask; int lowbit04 = (N >> 24) & lowbitmask; return (lowbit01 & lowbit02 & lowbit03 & lowbit04); First point: a value is odd iff its low bit is 1. Therefore, we need to examine the low bit of each of the four bytes in N. We can isolate the low bit in a byte by ANDing the byte with the mask 0000 0001, which is just 0x01. Second point: 0x01 assigned to an int variable will be 00000000 00000000 00000000 00000001 in binary. So: N & 0x01 gives us the low bit of N (i.e., of N's low byte) if we shift N 8 bits to the right and apply the same mask, we get the low bit of the next byte of N and so forth (The C right shift gives us an arithmetic shift, which will fill the high bits with 1s if N < 0, but the mask wipes those to 0s, so we don t have to worry about that.) Now, the function should return 1 iff all of those low bits were 1. If we AND the four variables found above, the result will be 1 iff all of the low bits were 1, and will be 0 iff one or more was 0. So, we want to return the AND of those variables. There are alternate approaches that work, but if you shift the mask up (to the left) instead of shifting N down (to the right), ORing might yield results that are not equal to either 0 or 1. So, if you took that approach, you needed to "post-process" so that the function always returns 0 or 1. C 8

5. [12 points] The assembly code below was produced by gcc from a simple C function. The comments give some clues about what's going on in the function: q5a: pushq %rbp movq %rsp, %rbp subq $20, %rsp # stack frame setup.l2: movl %edi, -20(%rbp) # int parameter: X andl $0x7FFFFFFF, -20(%rbp) # X = X & 0x7FFFFFFF (set high bit of X to 0) movl $0, -4(%rbp) # int local auto: Y = 0 movl -20(%rbp), %eax # eax = X andl $15, %eax # eax = X & 0xF (low nybble of X) movl %eax, -8(%rbp) # int local auto: Z = X & 0xF sarl $4, -20(%rbp) # Z = X >> 4 (shift out low nybble of X) movl -8(%rbp), %eax # eax = Z addl %eax, -4(%rbp) # Y += Z cmpl $5, -8(%rbp) # compare Z to 5 jg.l2 # if Z > 5, goto L2 (repeat loop) movl -4(%rbp), %eax # set return value to Y leave ret Examine the assembly code and write a C version of the function q5a. Your answer may not use a goto statement. int q5a(int X) { X = X & 0x7FFFFFFF; int Y = 0; int Z; do { Z = X & 0xF; X = X >> 4; Y += Z; while ( Z > 5 ); return Y; The comments I added in the assembly code above contain my analysis of the problem. The backward jump to.l2 indicates there's a loop. The fact that there's no forward jump to the loop test from before.l2 shows it is a do-while loop, not a while loop (or a for loop). The given comments identified the parameter (X) and the local automatic variables (Y and Z), so the rest was a matter of analyzing the individual assembly statements and then putting it all together as C code. C 9