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 2003 MID-SESSION TEST Monday, March 10 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 Marks will be recorded on the last page of this question paper. When you are told to start the test, the first thing you should do is to put your name, signature and U of C ID number in the appropriate spaces at the bottom of the last page. 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 15 marks) The function f3 is defined in the following C translation unit: int f1(int x, int y); int *f2(int *d, int *e, int f); int f3(int p, int q, int *r) int w1, w2; w2 = q - 15; w1 = f1(p, *r); w2 += *f2(&w1, r, w1); return (w2 + p + q) * 8; Part a. (3 marks) For each of the local variables w1 and w2 state whether the variable should be in a register or on the stack. Briefly give reasons to support your answer. Part b. (12 marks) Translate the definition of the function f3 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. PROBLEM 2 (16 marks) Below is a partial listing of a C program. (Definitions of func3 and func4 are omitted to save space.) void func1(char *p, int n, int c); int func2(int m); void func3(char *p); void func4(int a); int main(void) int j = 12; j += func2(6); func4(j); return 0; void func1(char *p, int n, int c) int i; for (i = 0; i < n; i++) p[i] = c; p[n] = \0 ; /* point one */ int func2(int m) char s[8]; int r; if (m >= 8) r = 7; func1(s, 7, x ); PAGE 2 OF 5
else r = m; func1(s, m, y ); func3(s); return r; Below is a correct translation of the C code 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. main: func1: for: quit:.globl main addiu $sp, $sp, -8 sw $ra, 4($sp) sw $s0, 0($sp) addiu $s0, $zero, 12 addiu $a0, $zero, 6 jal func2 addu $s0, $s0, $v0 addu $a0, $s0, $zero jal func4 addu $v0, $zero, $zero lw $s0, 0($sp) lw $ra, 4($sp) addiu $sp, $sp, 8.globl func1 addiu $sp, $sp, -4 sw $s0, 0($sp) addiu $s0, $zero, 0 slt $t0, $s0, $a1 beq $t0, $zero, quit addu $t1, $a0, $s0 sb $a2, ($t1) addiu $s0, $s0, 1 j for addu $t1, $a0, $a1 sb $zero, ($t1) # point one lw $s0, 0($sp) addiu $sp, $sp, 4 func2:.globl func2 addiu $sp, $sp, -20 PAGE 3 OF 5
else: endif: sw $a0, 16($sp) sw $ra, 12($sp) sw $s0, 8($sp) lw $t0, 16($sp) slti $t1, $t0, 8 bne $t1, $zero, else addiu $s0, $zero, 7 addiu $a1, $zero, 7 addiu $a2, $zero, x jal func1 j endif addu $s0, $zero, $t0 addu $a1, $t0, $zero addiu $a2, $zero, y jal func1 jal func3 addu $v0, $s0, $zero lw $s0, 8($sp) lw $ra, 12($sp) addiu $sp, $sp, 20 What To Do: Consider the moment 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, and list the contents of the following registers: $a0, $a1, $a2, $sp, $ra, $s0, and $t1. When necessary, use this format to show bytes within a memory word, starting with the lowest-addressed byte on the left: Show the contents of the stack slots and registers as numbers. Assume the following: The characters codes for x, y, \0 are 120, 121 and 0. When main begins execution, $ra contains 0x0040_0018, $s0 contains 0, and $sp contains 0x7fff_ff00. The address of the first intruction in main is 0x0040_0024, the address of the first instruction in func1 is 0x0040_005c, and the address of the slti instruction in func2 is 0x0040_00a8. PAGE 4 OF 5
PROBLEM 3 (total of X marks) Part a. (2 marks) Consider the bit pattern 1000_0001_1100_0000_0000_0000_0000_0000. If it is interpreted as a two s-complement number, what number does it represent? Express your answer in a format similar to 2 27 + 2 22 + 2 20. Part b. (3 marks) The IEEE 754 32-bit floating-point format is as follows: bit 31 is the sign bit; bits 30 23 are the bits of the biased exponent the bias is 127; bits 22 0 are the significand bits. What does the bit pattern of part a represent if it is interpreted as an IEEE 754 number? Express your answer in a format similar to 3.25 2 57. Part c. (2 marks) Suppose $t0 contains 0x6000_0000 and $t1 contains 0x9fff_ffff. If the instruction sub $t2, $t0, $t1 is executed, will that update $t2, or will it cause an exception? Explain how you obtained your answer. Part d. (2 marks) Repeat part c, but this time assume that subu $t2, $t0, $t1 is executed instead. Part e. (2 marks) Suppose $t0 contains 0x000c_0f00. What values will be in $t1 and $t2 after the following two instructions are executed? andi $t1, $t0, 0x0700 sll $t2, $t0, 4 Part f. (2 marks) Explain why the following piece of assembly langauge can not be assembled into a single MIPS machine instruction. addiu $t0, $t0, -65540 PROBLEM 4 (12 marks) Part a. (12 marks) Consider the C function below, which returns the largest value among a[0], a[1],..., a[n-1]. 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 max and $s1 for i. int max_value(int *a, int n) int max, i; max = a[n - 1]; i = n - 2; while (i >= 0) if (a[i] > max) max = a[i]; i--; return max; PAGE 5 OF 5