Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand and I will come to you. Please work the exam in pencil and do not separate the pages of the exam. For maximum credit, show your work. Good Luck! Your Name (please print) 1 2 3 4 total 25 25 25 25 100 1
Problem 1 (4 parts, 25 points) Complete the following C code by following the steps below: int Square(int x) return(x*x); int Cube(int y) return(exp(y, 3)); int Quad(int z) return(square(z)*square(z)); typedef int My_Math(int a, int lim, int b) { Function Pointers Walk(a, lim, ); /* part B */...rest of My_Math's body... int Walk(int Init, int Limit, ){ /* part C */ int i; for (i = Init; i<limit; i=s(i)) printf( %d\n, GlobalArray[i]); /* line P, part D */...rest of body of Walk... Part A (9 points) Create a local variable, called Stride, in My_Math that is a function pointer that points to Cube if b>100 and to Quad otherwise. Use a typedef to define the function pointer type. Part B (2 points) Pass this function pointer to the subroutine Walk as its third parameter. Part C (2 points) Complete the subroutine Walk by filling in type and name information for its third parameter which should be named S. Part D (12 points) Suppose Square, Cube, and Quad are defined in instruction memory starting at the following addresses: Square: 1000 Cube: 1200 Quad: 1600 The stack has the state shown below when My_Math is called. Complete the activation frames for My_Math and Walk and show the state of the stack after line P in Walk is executed twice. Symbolically label stack entries and give their values if they are known. a 2 lim 262144 b 105 SP & FP: 9900 RV N/A 2
Problem 2 (5 parts, 25 points) Heap Management Below is a snapshot of heap storage. The heap has been allocated contiguously beginning at 6000, with no gaps between objects. Heap management maintains a sorted free list to implement a best-fit strategy for reusing freed objects. The heap pointer is 6152 and the free pointer is 6064. addr value addr value addr value addr value addr value addr value 6000 12 6032 8 6064 6036 6096 8 6128 0 6160 0 6004 24 6036 6080 6068 4 6100 6052 6132 4 6164 0 6008 4 6040 10 6072 20 6104 16 6136 6032 6168 0 6012 16 6044 12 6076 16 6108 12 6140 0 6172 0 6016 12 6048 16 6080 6128 6112 152 6144 24 6176 0 6020 6008 6052 6032 6084 6004 6116 8 6148 0 6180 0 6024 2020 6056 6100 6088 4 6120 16 6152 0 6184 0 6028 28 6060 4 6092 6080 6124 24 6156 0 6188 0 Part A (4 points) Assuming the heap has been allocated contiguously beginning at 6000, with no gaps between objects, circle all object size words in the map above. Part B (6 points) List the address of the first data word in each object in the free list in order, starting with the head of the free list. Addresses of Objects on Free List (in order): Part C (3 points) Based on the free list created in part B, if an object of size 28 bytes is allocated, what address will be returned? How many bytes of slack (if any) will result? Address: Slack: Part D (6 points) Suppose the next instruction is free(ptr), where ptr = 6048. List all the changes to memory that will result: Address New Value Part E (6 points) Based on the free list after part D, if an object of size 22 bytes is allocated, what address will be returned? How many bytes of slack (if any) will result? Address: Slack: List all the changes to memory that will result: Address New Value 3
Problem 3 (7 parts, 25 points) Complete the following C code by following the steps below: typedef struct Med { float Temp; int HDL; int BMI; struct Med *Next; Med; Med *Records = NULL; void Add_Patient(float T, int C, int B){ Dynamic Allocation on Heap ; /* part B*/ if ( ){ /* part D */ printf( Error: Insufficient space. ); exit(1); ; /* part E*/ ; /* part E*/ ; /* part E*/ ; /* part F*/ ; /* part F*/ Part A (3 pts) Add a local variable called NewPatient to Add_Patient that is a pointer to a Med object. Part B (4 points) Allocate space for a Med structure using malloc and make NewPatient point to the object allocated. Be sure to include appropriate type casting to avoid type errors. Part C (4 points) How many bytes are allocated in Part B for the Med structure, excluding its size header? bytes Part D (3 pts) Fill in the test for whether malloc found enough space which controls the print statement. Part E (4 pts) Initialize the fields (Temp, HDL, BMI) of the newly allocated Med object to the values of the 3 input parameters (T, C, B, respectively). Part F (4 points) Push the newly allocated Med object onto the front of the list of Med objects pointed to by the global variable Records. Part G (3 points) Suppose a subroutine Delete_Records is implemented only by the statement: Records = NULL; What common memory problem would this create? 4
Problem 4 (4 parts, 25 points) Tile Puzzle Suppose you were given a C program P1-1.c that solved the tile puzzle assigned in Project 1. Recall that the tiles are stored in memory in row-column order and each tile is stored as a word of packed bytes, where each byte encodes the color of a triangle in the tile according to the following ordering: North: Byte 3 West: Byte 2 South: Byte 1 East: Byte 0 Part A (3 points) If you wanted to test the compiled program for the best case running time on a 5 by 5 grid, what value would you select for number of colors? Briefly explain why. #colors: Why? Part B (3 points) If you wanted to test the compiled program for the worst case running time on a 5 by 5 grid, what value would you select for number of colors? Briefly explain why. #colors: Why? Part C (10 points) Suppose register $5 contains the address of tile I in the puzzle and register $6 contains the row length (number of tiles in a row). Write MIPS code to branch to the label SouthOK when tile I's south triangle has the same color as one of the triangles in tile I's southern neighbor; otherwise, it should branch to NoFit. Assume tile I is not on the bottom row of the tile grid. Use a minimum number of static and dynamic instructions and modify only registers $1, $2, and $3. label instruction comment Part D (9 points) Write MIPS code to take the tile whose address is in $5, rotate it 90 degrees clockwise and store the result back in the address given in $5. Use a minimum number of instructions and registers. label instruction comment 5
MIPS Instruction Set (core) instruction example meaning arithmetic add add $1,$2,$3 $1 = $2 + $3 subtract sub $1,$2,$3 $1 = $2 - $3 add immediate addi $1,$2,100 $1 = $2 + 100 add unsigned addu $1,$2,$3 $1 = $2 + $3 subtract unsigned subu $1,$2,$3 $1 = $2 - $3 add immediate unsigned addiu $1,$2,100 $1 = $2 + 100 set if less than slt $1, $2, $3 if ($2 < $3), $1 = 1 else $1 = 0 set if less than immediate slti $1, $2, 100 if ($2 < 100), $1 = 1 else $1 = 0 set if less than unsigned sltu $1, $2, $3 if ($2 < $3), $1 = 1 else $1 = 0 set if < immediate unsigned sltui $1, $2, 100 if ($2 < 100), $1 = 1 else $1 = 0 multiply mult $2,$3 Hi, Lo = $2 * $3, 64-bit signed product multiply unsigned multu $2,$3 Hi, Lo = $2 * $3, 64-bit unsigned product divide div $2,$3 Lo = $2 / $3, Hi = $2 mod $3 divide unsigned divu $2,$3 Lo = $2 / $3, Hi = $2 mod $3, unsigned transfer move from Hi mfhi $1 $1 = Hi move from Lo mflo $1 $1 = Lo load upper immediate lui $1,100 $1 = 100 x 2 16 logic and and $1,$2,$3 $1 = $2 & $3 or or $1,$2,$3 $1 = $2 $3 and immediate andi $1,$2,100 $1 = $2 & 100 or immediate ori $1,$2,100 $1 = $2 100 nor nor $1,$2,$3 $1 = not($2 $3) xor xor $1, $2, $3 $1 = $2 $3 xor immediate xori $1, $2, 255 $1 = $2 255 shift shift left logical sll $1,$2,5 $1 = $2 << 5 (logical) shift left logical variable sllv $1,$2,$3 $1 = $2 << $3 (logical), variable shift amt shift right logical srl $1,$2,5 $1 = $2 >> 5 (logical) shift right logical variable srlv $1,$2,$3 $1 = $2 >> $3 (logical), variable shift amt shift right arithmetic sra $1,$2,5 $1 = $2 >> 5 (arithmetic) shift right arithmetic variable srav $1,$2,$3 $1 = $2 >> $3 (arithmetic), variable shift amt memory load word lw $1, 1000($2) $1 = memory [$2+1000] store word sw $1, 1000($2) memory [$2+1000] = $1 load byte lb $1, 1002($2) $1 = memory[$2+1002] in least sig. byte load byte unsigned lbu $1, 1002($2) $1 = memory[$2+1002] in least sig. byte store byte sb $1, 1002($2) memory[$2+1002] = $1 (byte modified only) branch branch if equal beq $1,$2,100 if ($1 = $2), PC = PC + 4 + (100*4) branch if not equal bne $1,$2,100 if ($1 $2), PC = PC + 4 + (100*4) jump jump j 10000 PC = 10000*4 jump register jr $31 PC = $31 jump and link jal 10000 $31 = PC + 4; PC = 10000*4 6