CS 354, version A
Spring 2015
Name:___________________
Section:________________
ID:___________________
Exam 2
No electronic devices may be used while taking this exam. No calculators, no cell phones. Each student is allowed one 8.5 by 11 inch sheet of paper with handwritten notes. Do not write on the notes sheet during the exam.

Show all work, and do any/all calculations on the exam. Extra scratch paper may not be used. Partial credit will be given based on work shown.

Exam Score
Q1 = _____ /    10
Q2 = _____ /    10
Q3 = _____ /    10
Q4 = _____ /      5
Q5 = _____ /    37
Q6 = _____ /      8
Q7 = _____ /    20
Total = _____ / 100

Question 1 (10 points)
Write the equivalent of the x86 instruction

popl   %edx
as a series of x86 instructions, without using the popl instruction.



Question 2 (10 points total)
Part A (4 points) Are 2 dimensional arrays for C code stored in row major or column major order?

Part B (6 points) An array is declared with

   int  counts[240];
The base address of this array at address 0x080400c4. At what address is counts[65]? Give your answer in hexadecimal.









Question 3 (10 points total)
Part A (5 points) Circle the statement in this C code fragment that could cause a SIGFPE interrupt to be sent to the program.

    struct sigaction intaction;
    intaction.sa_handler = divby0handler;
    intaction.sa_flags = 0;
    if ( sigaction(SIGFPE, &intaction, NULL) == -1 ) {
        printf("sigaction() failed for arithmetic errors; quitting\n");
        exit(1);
    }
    while (1) {                          /* Loop forever. */
         int1 = 0;
         int2 = 0;
         printf("Enter first integer: ");
         if ( fgets(oneline, BUFSIZE, stdin) != NULL ) {
              int1 = atoi(oneline);
         } else {
              printf("bad input, quitting.\n");
              exit(1);
         }
         printf("Enter second integer: ");
         if ( fgets(oneline, BUFSIZE, stdin) != NULL ) {
              int2 = atoi(oneline);
         } else {
              printf("bad input, quitting.\n");
              exit(1);
         }
         quotient = int1 / int2;
         printf("%d / %d is %d\n", int1, int2, quotient);
    }

Part B (5 points) Which statement in the C code fragment could be executing when a SIGINT is sent to the program?





Question 4 (5 points)
What programming bug does a stack smashing attack rely upon?




Question 5 (37 points total)
Part A (18 points) Write the C code for function f1. Invent variable names for local variables, and show declarations for those variables. Hint: there is a loop that iterates 6 times.

f1:     pushl	%ebp
	movl	%esp, %ebp
	subl	$32, %esp
	movl	$0, -4(%ebp)
	jmp	.L2
.L3:
	movl	-4(%ebp), %edx
	movl	%edx, -28(%ebp,%edx,4)
	addl	$1, -4(%ebp)
.L2:
	cmpl	$5, -4(%ebp)
	jle	.L3
	leave
	ret

Part B (19 points) Show the contents of the stack and where registers %ebp and %esp point just before the leave instruction is fetched.
         |           |  ^   address 0
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|
         |           |
         |-----------|

Question 6 (8 points total)
Here is a a C function (on the left) and its x86 assembly language version (on the right).

int function(int a) {         function: 
                                        pushl  %ebp
                                        movl   %esp, %ebp

   return (a - 1);                      movl   ____(%ebp), %eax
                                        subl   $1, %eax
                                        popl   %ebp
}                                       ret
Part A (3 points)
Circle the prologue code within function().

Part B (5 points) Fill in the blank in the assembly language code to complete the code.

Question 7 (20 points total)
Part A (4 points) (Briefly) Why do we need wear leveling for SSDs?






Part B (3 points) If an application invokes getc(), and no key has been pressed on the keyboard, what technique does the operating system code use to block to wait for input?



Part C (6 points) Name the 2 types of exceptions.




Part D (3 points) On your answer to Part C, circle the type of exception that is asynchronous.
Part E (4 points) Are real world exception handlers reentrant or non-reentrant?