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 28 20 16 36 100 1
Problem 1 (2 parts, 28 points) Storage Allocation and Pointers In the following problems, assume all data types are word aligned. Pay attention to the word size, which is different in each part. Part A (8 points) Assuming a 64-bit operating system, show how the following global variables map into static memory. Assume they are allocated starting at address 3000. For each variable, draw a box showing its size and position in memory. Label the box with the variable name. Label each element of an array (e.g., Name[0]). 3000 float f = 2.0; double B[] = {3.14, 6.28; float *fp = &f; double *dp = B; 3004 3008 3012 3016 3020 3024 3028 3032 3036 3040 3044 3048 Part B (20 points) For this part, assume a 32-bit operating system. Suppose the following variables are allocated beginning at address 4000. Complete the table below, listing the value of the expression following this definition. int x = 8; int y = 9; double D = 6.25; double E = 9.33; int A[3] = {17, 6, 5; int *P = A+1; double *Q = &D; float w = 7.5; *(P-1) *(A+2) P+1 &(A[1]) A Q Q++ &w 2
Problem 2 (1 part, 20 points) Parameter Passing Consider the following C code fragment. Foo is called by main. int main() { int x = 100, V[] = {10, 3, 5, 6; float F[] = {3.14, 5.25; int y=25, d; int *z = &y; <more code here> d = Foo(x, F, F[0], V, z); <more code here> int Foo (int a, float *b, float c, int *S, int *g) { <Foo's code goes here> For each statement from Foo below, list the resulting value. If the result is an address, just list address. Also determine if it affects Foo s activation frame variables, main s activation frame variables, or both. Consider each statement independently and non-cumulatively (i.e., as if it is the only statement in Foo). statement in Foo result (assigned value) modify Foo s AF variables modify main s AF variables c = *b + 3.0; Yes No Yes No a *= 3; Yes No Yes No *g = *g + S[2]; Yes No Yes No S[1] += 2; Yes No Yes No b++; Yes No Yes No 3
Problem 3 (2 parts, 16 points) Array Access Part A (4 points) Suppose a 64x64 image array is stored in memory as in Project 1: each pixel is represented by 1 byte of storage. Each row of pixels is stored contiguously, one after another. Suppose register $4 holds the linear offset into the array (i.e., it would be added to the base address of the image array to access a particular pixel). Write a MIPS code fragment that computes the row and column number and places the row number into $5 and the column number into $6. For 3 bonus points, use only 2 instructions. Label Instruction Comment Part B (12 points) Write the MIPS code implementation of the dynamically allocated array access below in the smallest number of instructions. A pointer to the array (declared below) is stored in $3. Variables X, Y, Z, and R reside in $4, $5, $6, and $2 respectively. Modify only registers $1 and $2. int Array[4][3][32]; /* array declaration */ R = Array[Z][Y][X]; /* implement this */ Label Instruction Comment 4
Problem 4 (2 parts, 36 points) Consider the following C code fragment: typedef struct { int x; int y; pair; Activation Frames int Bar() { int M = 3; int N = 4; pair P; int Q[] = {10, 20, 30; int Foo(int, pair *, int *); P.x = 5; P.y = 6; N = Foo(M, &P, Q); return(n); int Foo(int A, pair *B, int *C) { int D = 25; int E; C[2] = C[1] + A; E = B->y + D; return(e); Part A (20 points) Describe the current state of the stack just before Foo returns to Bar. Fill in the unshaded boxes to show Bar s and Foo s activation frames. Include a symbolic description and the actual value (in decimal). For return addresses, show only the symbolic description; do not include a value. Label the frame pointer and stack pointer. address description Value 9900 RA of Bar s caller 9896 FP of Bar s caller SP, Bar s FP 9892 9888 9884 9880 9876 9872 9868 9864 9860 9856 9852 9848 9844 9840 9836 9832 5
Part B (16 points) Write MIPS code fragments to implement the subroutine Foo by following the steps below. Do not use absolute addresses in your code; instead, access variables relative to the frame pointer. Assume no parameters are present in registers (i.e., access all parameters from Foo s activation frame). First, write code to properly set Foo s frame pointer and to allocate space for Foo s local variables and initialize them if necessary. C[2] = C[1] + A; E = B->y + D; return(e); (compute return value, deallocate locals, and return) 6
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 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 7