University of Calgary Department of Electrical and Computer Engineering ENCM 369: Computer Organization Instructors: Dr. S. A. Norman (L01) and Dr. S. Yanushkevich (L02) Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm Notes to Winter 2005 ENCM 369 Students This handout contains the questions from an old midterm. Spaces for answers have been removed in order to save paper in printing. General Instructions You may not use electronic calculators or computers during the test. The test is closed-book. You may not refer to books or notes during the test, with one exception: you may refer to the Reference Material page that accompanies this test paper. You are not required to add comments to assembly language code you write, but you are strongly encouraged to do so, because writing good comments will improve the probability that your code is correct and will help you to check your code after it is finished. Some problems are relatively easy and some are relatively difficult. Go after the easy marks first. To reduce distraction to other students, you are not allowed to leave during the last ten minutes of the test. Write all answers on the question paper and hand in the question paper when you are done. Please print or write your answers legibly. What cannot be read cannot be marked. If you write anything you do not want marked, put a large X through it and write rough work beside it. You may use the backs of pages for rough work. PAGE 1 OF 5
PROBLEM 1 (total of 14 marks) The function proc_c is defined in the following C translation unit: int proc_a(int j, int k); int * proc_b(int *p, int *q); int proc_c(int x, int y, int *z) int a, b; a = proc_a(y, *z); b = *proc_b(&a, &x); return a * (b + y + *z); Part a. (3 marks) For each of the local variables a and b state whether the variable should be in a register or on the stack. Briefly give reasons to support your answer. Part b. (11 marks) Translate the definition of the function proc_c into SPIM assembly language. Follow the calling conventions used in lectures and labs, and use only instructions from the Midterm Instruction Subset described on the Reference Material page. Assume the following for integer multiplication: The 64-bit product of two 32-bit operands is computed, then bits 31 0 of the product are copied into the register or memory word corresponding to the result. PROBLEM 2 (16 marks) Here is a C program: void f1(int *a, int *b, int n); int f2(int k); int aaa[3] = 0x100, 0x200, 0x300 ; int main(void) int bbb[3]; int *p; for (p = bbb; p < bbb + 3; p++) *p = 0x150; f1(aaa, bbb, 3); return 0; void f1(int *a, int *b, int n) int i; for (i = 0; i < n; i++) b[i] += f2(i) + a[i]; int f2(int k) int x; x = k + k; /* point one */ return x + k; PAGE 2 OF 5
Below is a correct translation of that C program into SPIM assembly language. Note that it follows all calling conventions used in lectures and labs, and that each procedure uses $s0 as a local variable. (The listing starts on this page and continues on the next page.).data aaa aaa:.word 0x100, 0x200, 0x300 main: loop1: quit1:.text main addiu $sp, $sp, -20 sw $ra, 16($sp) sw $s0, 12($sp) addiu $s0, $sp, 0 addiu $t0, $sp, 12 # $t0 = bbb + 3 addiu $t1, $zero, 0x150 sltu $t2, $s0, $t0 beq $t2, $zero, quit1 sw $t1, ($s0) addiu $s0, $s0, 4 j loop1 la $a0, aaa addiu $a1, $sp, 0 addiu $a2, $zero, 3 jal f1 addiu $v0, $zero, 0 lw $s0, 12($sp) lw $ra, 16($sp) addiu $sp, $sp, 20 f1: loop2: f1 addiu $sp, $sp, -20 sw $a2, 16($sp) sw $a1, 12($sp) sw $a0, 8($sp) sw $ra, 4($sp) sw $s0, 0($sp) addiu $s0, $zero, 0 lw $t0, 16($sp) slt $t1, $s0, $t0 beq $t1, $zero, quit2 addu $a0, $s0, $zero jal f2 addu $t2, $s0, $s0 addu $t2, $t2, $t2 # $t2 = 4*i lw $t3, 12($sp) addu $t3, $t3, $t2 lw $t4, 8($sp) PAGE 3 OF 5
quit2: f2: addu $t4, $t4, $t2 lw $t5, ($t3) lw $t6, ($t4) addu $t7, $v0, $t5 addu $t6, $t7, $t6 sw $t6, ($t3) addiu $s0, $s0, 1 j loop2 lw $s0, 0($sp) lw $ra, 4($sp) addiu $sp, $sp, 20 f2 addiu $sp, $sp, -4 sw $s0, 0($sp) addu $s0, $a0, $s0 # point one addu $v0, $s0, $a0 lw $s0, 0($sp) addiu $sp, $sp, 4 What To Do: Consider the third time that the program passes through point one. For this moment in time: make a diagram of the stack, clearly indicating the boundaries of each stack frame; list the contents of registers $a0, $s0, $sp, and $ra. Show the contents of the stack slots and registers as numbers. Assume the following: When main is called, $ra contains 0x0040_0018, $s0 contains 0, and $sp contains 0x7fff_ffcc. The address of aaa[0] is 0x1001_0000. The address of the instruction jal f1 is 0x0040_0058. The address of the instruction labeled loop2 is 0x0040_008c. PROBLEM 3 (total of 11 marks) Part a. (4 marks) Given the following values in MIPS general purpose registers: $t1 = 0x8000_0000, $t2 = 0x0000_0001 What values are in registers $t3 and $t4 after the following instructions are executed: slt $t3, $t2, $t1 subu $t4, $t1, $t2 PAGE 4 OF 5
Show or explain how you obtained your answers. Part b. (2 marks) In the subu instruction of part a, did overflow occur? Briefly explain how you obtained your answer. Part c. (2 marks) Two (of many) ways to load a 32-bit constant into a register are: to use SPIM instructions lui and addiu; to use SPIM instructions lui and ori. Suppose an assembly language wants to put 0x0001_8000 into $t0. Which of the above ways is preferable? Why? Part d. (3 marks) Write a sequence of SPIM instructions (not a complete procedure) to copy bits 22 16 of register $s0 to bits 8 2 of register $s1 while making all other bits of $s1 equal to zero. Your instructions should not modify the contents of $s0. Modification of some t-registers contents is allowed (but not required). PROBLEM 4 (total of 18 marks) Part a. (12 marks) Consider the following C function, which returns the index of a maximum value among a[0], a[1],... a[n-1]: int index_of_max(const int *a, int n) const int *p; const int *q; int max; const int *x; p = a + 1; q = a; max = *a; x = a + n; while (p < x) if (*p > max) max = *p; q = p; p++; return q - a; Translate the function into SPIM assembly language. Follow the calling conventions used in lectures and labs, and use only instructions from the Midterm Instruction Subset described on the Reference Material page. You must allocate registers as follows: $s0 for p, $s1 for q, $s2 for max, and $s3 for x. Part b. (6 marks) Consider the following C function: void put_zero(char *a, int n) int i; for (i = n - 1; i >= 0; i--) a[i] = 0; Translate the function into SPIM assembly language. Follow the calling conventions used in lectures and labs, and use only instructions from the Midterm Instruction Subset described on the Reference Material page. You must allocate registers as follows: $s0 for i. PAGE 5 OF 5