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 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

CS 2505 Computer Organization I

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];

CS 2505 Computer Organization I

A short session with gdb verifies a few facts; the student has made notes of some observations:

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

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

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

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

Machine Programming 1: Introduction

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

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

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

Do not start the test until instructed to do so!

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.

Do not start the test until instructed to do so!

CS/ECE 354 Practice Midterm Exam Solutions Spring 2016

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

Do not start the test until instructed to do so!

CS 2506 Computer Organization II Test 2

CS 2506 Computer Organization II Test 1

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

Machine Level Programming II: Arithmetic &Control

Machine-level Programming (3)

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

ASSEMBLY III: PROCEDURES. Jo, Heeseung

UW CSE 351, Winter 2013 Midterm Exam

Assembly III: Procedures. Jo, Heeseung

System Programming and Computer Architecture (Fall 2009)

CS241 Computer Organization Spring 2015 IA

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

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

Lab 10: Introduction to x86 Assembly

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

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 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CS 3843 Final Exam Fall 2012

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CS , Fall 2001 Exam 1

Systems I. Machine-Level Programming I: Introduction

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

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

CS213. Machine-Level Programming III: Procedures

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

CS241 Computer Organization Spring Addresses & Pointers

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

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

CS 3214 Spring # Problem Points Min Max Average Median SD Grader. 1 Memory Layout and Locality Bill

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

Instruction Set Architectures

Machine-Level Programming II: Control Flow

Machine- Level Programming III: Switch Statements and IA32 Procedures

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

CS61 Section Solutions 3

CS241 Computer Organization Spring Loops & Arrays

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Machine Programming 3: Procedures

Sungkyunkwan University

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

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Credits and Disclaimers

CS61, Fall 2012 Midterm Review Section

Assembly I: Basic Operations. Jo, Heeseung

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

Machine-Level Programming II: Control and Arithmetic

This is a medical robot, guided by a skilled surgeon and designed to get to places doctors are unable to reach without opening a pacent up.

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

The Hardware/Software Interface CSE351 Spring 2013

ANITA S SUPER AWESOME RECITATION SLIDES

CS , Fall 2004 Exam 1

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Carnegie Mellon. 5 th Lecture, Jan. 31, Instructors: Todd C. Mowry & Anthony Rowe

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

Machine-Level Programming Introduction

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

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

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

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

Buffer Overflow Attack

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

Instruction Set Architectures

Compiler Construction D7011E

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

Giving credit where credit is due

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

x86 assembly CS449 Fall 2017

The Hardware/Software Interface CSE351 Spring 2015

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

CS 2504 Intro Computer Organization Test 1

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

Assembly Language: Function Calls

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

CPS104 Recitation: Assembly Programming

X86 Assembly -Procedure II:1

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Assembly Language: Function Calls" Goals of this Lecture"

Transcription:

Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. 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 6 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 B 1

xkcd.com B 2

1. [12 points] Recall that for an earlier assignment you created a BinaryInt type. For this question you will write a similar C function, in this case you will be incrementing an unsigned integer. You may not use any of the functions from your BinaryInt assignment, however you may use the add_one_bit function declared (but not implemented) below. This function takes the two "bits" you are adding (a and b), plus a carry in "bit" cin, and then returns the sum while placing the carry out "bit" in cout. uint8_t add_one_bit(uint8_t a, uint8_t b, uint8_t cin, uint8_t * cout); /* Increments an **unsigned** integer value by 1. * * Pre: (*Sum)[] is of dimension currsize, currsize > 0 * (*Sum)[] stores an unsigned integer value * * Post: (*Sum)[] == (*Sum)[] + One[] (where One[] contains 000...01) * If an overflow would occur when computing sum of (*Sum)[] + One[]; * (*Sum)[] should be resized dynamically (currsize = 2*currSize) * to accommodate the bigger number. * * So if we had four "bit" integers, adding 15 + 1 should produce: * 1111 * + 0001 * -------- * 00010000 * Ret: the (potentially changed) dimension of (*Sum)[] */ uint8_t BI_Increment(uint8_t ** Sum, uint8_t currsize) uint8_t result, i = 0, carry = 0; (*Sum)[i] = add_one_bit((*sum)[i], 1, 0, &carry); do if(i == currsize - 1 && carry == 1) uint8_t newsize = 2*currSize; uint8_t *temp = realloc(*sum, currsize); else if (temp!= NULL) *Sum = temp; (*Sum)[currSize] = 1; for(int s = currsize + 1; s < newsize; s++) (*Sum)[s] = 0; currsize = newsize; break; i++; (*Sum)[i] = add_one_bit((*sum)[i], 0, carry, &carry); while(i < currsize); return currsize; B 3

2. For this question you will create a "dense bool" type. A dense bool uses 1 bit to hold each boolean value, 0 is false and 1 is true. Our dense bool will be represented by a uint8_t type, so it will contain 8 boolean values: uint8_t dense_bool = 00000001 uint8_t other_bool = 00001100 // bool 0 is true, everything else is false // bool 2 and 3 are set to true a) [8 points] Implement the get function for a dense bool variable as described below: /* Pre: N is the bit we want to get, 0 would get the value of bit 0, and so on. * Returns: a uint8_t with only bit N set. The value at bit N should contain * the same value as bit N in the dense_bool. */ uint8_t DB_get(uint8_t dense_bool, uint8_t N) uint8_t Mask = 1 << N; return dense_bool & Mask; b) [8 points] Implement the toggle function for a dense bool variable as described below: /* Pre: N is the bit we want to change, 0 would change bit 0, and so on. * * Post: bit N in dense_bool is flipped (negated); no other bits of dense_bool * are changed */ void DB_toggle(uint8_t *dense_bool, uint8_t N) uint8_t Mask = 1 << N; return dense_bool ^ Mask; B 4

3. Consider the following short C program. /* Process data inside of a buffer. * Pre: * *pbuffer contains at least 2 bytes. * The first byte in *pbuffer is the count of elements in the rest of pbuffer. * The second byte is number of bytes per element (unit_size). * The remaining count*unit_size bytes are data */ void Q3(uint8_t *pbuffer) uint8_t count = *pbuffer, x = 0; uint8_t unit_size = *pbuffer + 1; pbuffer +=2; if(unit_size == 4) /* each element is a 4 byte value */ uint32_t *pnew = malloc(unit_size * count); uint32_t *newtemp = pnew; while(x < count) *newtemp = *((uint32_t *) pbuffer); newtemp +=1; pbuffer +=1; x++; /* do something with pnew, not relevant */ a) [10 points] There are two pointer bugs in this function. Analyze the code and determine the location of these errors. You may assume the input buffer is correctly formatted. Be specific, vague answers will receive no credit. (1) *pbuffer + 1. The * will happen before the + 1, so unit_size ends up being count + 1, instead of the value at (pbuffer + 1). (2) pbuffer +=1. pbuffer is a pointer to 8 bit (1 byte) integers but we are only incrementing it by 1 each time in the while loop. That will only move the pointer 1 bytes or to next 8 bit integer, rather than to the next 32 bit integer, 4 bytes away. Those were the two "big" bugs that alter the intended behavior of the program. Not checking the return value of malloc is a bug. Mentioning this issue got you couple points if you didn't mention one (or both) of the bugs above. Not free'ing the malloc'ed memory is also a potential bug, however in this case more code comes after copying from pbuffer as indicated by the comment at the end of the code. b) [10 points] How could you fix each of the two bugs while keeping the same functionality? *(pbuffer + 1); pbuffer +=4; B 5

4. [6 points] The following x86 assembly code is part of a C function:.l3: movl $0, -8(%ebp) # 1 movl -8(%ebp), %eax # 2 movl (%eax), %eax # 3 movl %eax, -4(%ebp) # 4 movl $0, %eax # 5 Explain what would happen if this code were executed, and why. Statement #1 loads 0 into a local variable stored at %ebp 8 on the stack. Statement #2 loads the value of that local variable into %eax. Statement #3 dereferences %eax... which is unfortunate, since %eax == 0. So, a segmentation fault will occur (null pointer dereference). B 6

5. A developer has an executable file that contains a C function and a main() function that calls it, but doesn't know much about the function except that it is named mystery(). So, she tries a gdb analysis. A partial transcript follows: CentOS > gdb mdriver (gdb) break mystery Breakpoint 1 at 0x80483c2 (gdb) run Breakpoint 1, 0x080483c2 in mystery () a) (gdb) p *(int*)($ebp + 8) $1 = 73 (gdb) p *(int*)($ebp + 12) $2 = 14 (gdb) disassem Dump of assembler code for function mystery: 0x080483bc < +0>: push %ebp 0x080483bd < +1>: mov %esp,%ebp 0x080483bf < +3>: sub $0x10,%esp => 0x080483c2 < +6>: mov 0x8(%ebp),%eax 0x080483c5 < +9>: mov %eax,%edx 0x080483c7 <+11>: sar $0x1f,%edx 0x080483ca <+14>: idivl 0xc(%ebp) 0x080483cd <+17>: mov %eax,-0x4(%ebp) 0x080483d0 <+20>: mov -0x4(%ebp),%eax 0x080483d3 <+23>: imul 0xc(%ebp),%eax 0x080483d7 <+27>: mov %eax,-0x4(%ebp) 0x080483da <+30>: mov -0x4(%ebp),%eax 0x080483dd <+33>: mov 0x8(%ebp),%edx 0x080483e0 <+36>: mov %edx,%ecx 0x080483e2 <+38>: sub %eax,%ecx 0x080483e4 <+40>: mov %ecx,%eax 0x080483e6 <+42>: mov %eax,-0x4(%ebp) 0x080483e9 <+45>: mov -0x4(%ebp),%eax 0x080483ec <+48>: leave 0x080483ed <+49>: ret End of assembler dump. 0x080483c5 in mystery () 0x080483c7 in mystery () 0x080483ca in mystery () 0x080483cd in mystery () (gdb) disassem 0x080483c7 <+11>: sar $0x1f,%edx 0x080483ca <+14>: idivl 0xc(%ebp) => 0x080483cd <+17>: mov %eax,-0x4(%ebp) 0x080483d0 <+20>: mov -0x4(%ebp),%eax B 7

b) CS 2505 Computer Organization I Test 2 (gdb) p $eax $3 = 5 (gdb) p $edx $4 = 3 0x080483d0 in mystery () 0x080483d3 in mystery () 0x080483d7 in mystery () (gdb) disassem 0x080483d3 <+23>: imul 0xc(%ebp),%eax => 0x080483d7 <+27>: mov %eax,-0x4(%ebp) 0x080483da <+30>: mov -0x4(%ebp),%eax (gdb) p $edx $5 = 3 (gdb) p $eax $6 = 70 0x080483da in mystery () 0x080483dd in mystery () 0x080483e0 in mystery () 0x080483e2 in mystery () 0x080483e4 in mystery () (gdb) disassem 0x080483e2 <+38>: sub %eax,%ecx => 0x080483e4 <+40>: mov %ecx,%eax 0x080483e6 <+42>: mov %eax,-0x4(%ebp) c) (gdb) p $ecx $8 = 3 For the following questions, label the parameters to mystery() as P1, P2, etc. Each question that follows refers to the part of the gdb session above that is labeled to match the question, but you may consider other parts of the gdb session in answering each question. B 8

a) [4 points] Explain what the two print commands tell us about the function. You might want to consider the disassem output in your explanation. These are the values of the two parameters passed to the function mystery(). We know that only two parameters are used since there are no other accesses to parameters in the disassembly of mystery() that follows. b) [4 points] The idivl instruction performs integer division of the value in %eax by the operand to idivl. The result of an integer division is a quotient and a remainder (as in Discrete Math); idivl puts one of those values into %eax and one into %edx. Which value goes into which register? %eax holds the quotient and %edx holds the remainder. We establish this by tracing the preceding code, which shows that prior to the idivl instruction, %eax holds the value of the first parameter (73), and knowing already that 0xc(%ebp) refers to the second parameter (value 14). c) [10 points] Give an algebraic expression, in terms of the parameters to mystery(), for the value that is in $ecx. Justify your answer by showing work next to the disassembly on page 7. Tracing the code reveals that the value returned equals: P1 (P1 / P2) * P2 However, since these are all integer computations, this is just P1 % P2. B 9

6. Consider the x86-32 translation of a short C function shown at right. Assume that all parameters and local variables are of type int or int*. a) [6 points] How many parameters does the function Q6() receive? Three. State the stack address at which each parameter is stored (e.g., %ebp 48). %ebp + 8 # 20 %ebp + 12 # 6 %ebp + 16 # 23 b) [6 points] How many local variables does the function Q6()have? Four. State the stack address at which each local variable is stored (e.g., %ebp 48).... Q6: pushl %ebp # 1 movl %esp, %ebp # 2 subl $16, %esp # 3 movl $0, -16(%ebp) # 4 movl $0, -12(%ebp) # 5 movl 12(%ebp), %eax # 6 subl $1, %eax # 7 movl %eax, -8(%ebp) # 8 movl $0, -4(%ebp) # 9 jmp.l2 # 10.L4: # 11 movl -16(%ebp), %eax # 12 sall $2, %eax # 13 addl 8(%ebp), %eax # 14 movl (%eax), %eax # 15 cmpl 16(%ebp), %eax # 16 jle.l3 # 17 movl -16(%ebp), %eax # 18 sall $2, %eax # 19 addl 8(%ebp), %eax # 20 movl (%eax), %eax # 21 addl -12(%ebp), %eax # 22 subl 16(%ebp), %eax # 23 movl %eax, -12(%ebp) # 24 jmp.l2 # 25.L3: # 26 addl $1, -4(%ebp) # 27.L2: # 28 movl -16(%ebp), %eax # 29 cmpl -8(%ebp), %eax # 30 jle.l4 # 31 movl -12(%ebp), %eax # 32 leave # 33 ret # 34... %ebp 4 # 9 %ebp 8 # 8 %ebp 12 # 5 %ebp 16 # 4 B 10

c) [6 points] Examine the given x86-32 code. If there are any if or if-else control structures in the code, for each such structure, what range of statements (e.g., lines 17 through 23) belong to that structure (including any relevant labels and the boolean test)? We recognize an if-statement by a conditional forward branch. An if-else-statement would have an unconditional forward branch, shortly before the target of the first forward branch. The forward branches in #17 and #25 meet the criteria for an if-else-statement: if-else begins: # 16 (comparison for if-test) ends: # 28 (label for the jump over the else-clause) (One could argue that the if-else begins a few statements earlier, where the necessary value for the comparison is placed into %eax.) d) [6 points] Examine the given x86-32 code. If there are any loops in the code, for each loop, what range of statements (e.g., lines 17 through 23) belongs to the body of the loop (including any relevant labels and the loop test)? We recognize a loop by a backward branch (usually conditional) to a label, from which execution falls back to the branch statement. Such a branch occurs in #31. The target of than branch is preceded (#10) by an unconditional jump to the test for the loop, indicating this is a while-loop: while begins: # 10 (jump forward to loop test) ends: # 31 (jump backward to beginning of loop body) e) [4 points] Examine the given x86-32 code. How many bytes are in the stack frame for Q6, including the backed up value for the frame pointer ebp? Justify your answer. The first three instructions create the stack frame: pushl %ebp # 1 movl %esp, %ebp # 2 subl $16, %esp # 3 The push instruction decrements %esp by 4, allocating space to store the old value of %ebp. The subl instruction allocates 16 more bytes. So, the stack frame contains 20 bytes altogether. B 11