page 1 of 5 University of Calgary Department of Electrical and Computer Engineering ENCM 369: Computer Organization Lecture Instructors: Steve Norman and Norm Bartley Winter 2017 MIDTERM TEST #1 Wednesday, February 8 7:00pm to 8:30pm Please do not write your U of C ID number on this cover page. Name (printed): Signature: Lecture section (01 is MWF 11:00am with S. Norman, 02 is MWF 10:00am with N. Bartley): 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, U of C ID number, and lecture section in the appropriate spaces at the bottom of the last page. If you use a calculator, it must be one of the following models sanctioned by the Schulich School of Engineering: Casio FX-260, Casio FX-300MS, TI- 30XIIS. 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.
ENCM 369 Winter 2017: Midterm Test #1 page 2 of 5 PROBLEM 1 (11 marks) Consider the listed to the right. Translate the function foo into MARS assembly language. Follow the usual calling conventions from lectures and labs, and use only instructions from the Midterm Instruction Subset described on the Reference Material page. int bar(int x); int quux(int y); int foo(const int *a, int *b, int n) int j, k, aj; k = 0; for (j = 0; j < n; j++) aj = a[j]; if(bar(aj)!= 0) b[k] = quux(aj); k++; return k;
ENCM 369 Winter 2017: Midterm Test #1 page 3 of 5 PROBLEM 2 (total of 13 marks). In this problem, you are asked to translate sequences of one or more C statements into sequences of one or more MARS instructions, not complete MARS procedures. Use only instructions from the Midterm Instruction Subset. Use as many t-registers as you wish for intermediate values. Example. (No marks.) $s0 is used for x, of type int. x = 42; addi $s0, $zero, 42 Part a. (3 marks.) $s2 is used for p, of type int*. $s3 is used for q, also of type int*. *p = *q - 3; q = p - 2; Part b. (3 marks.) a is an array of char elements on the stack, and the address of a[0] is 20($sp). $s5 is used for p, of type char*. p = a; while (*p!= \0 ) p++; Part c. (4 marks.) j, in $s3, and k, in $s4, are both of type int. f takes an argument of type int and returns a value of type int. if (j < 10 f(j) < 100) k++; else k--; Part d. (3 marks.) y, in $s1, and x, in $s2, are both of type int. (Hint: 20 = 16 + 4.) y = 20 * x;
ENCM 369 Winter 2017: Midterm Test #1 page 4 of 5 PROBLEM 3 (13 marks). The MARS assembly-language program on this page is a correct translation of the C program. Fill in the table of GPRs and the blank boxes in the diagram of memory with numbers to show the state of the assembly-language program, at the third of the four times it will get to point one. label zed wye main exx address 0x0040_0030 0x0040_0044 0x0040_00b0 0x1001_0000 GPR value of GPR when main starts $s0 111 $s1 222 $s2 333 $sp 0x7fff_eb80 $ra 0x0040_0028 Use base ten or hexadecimal format for numbers, whichever is more convenient for any particular number. Note that some of the memory words in the diagram might not be used by the program. GPR values, third time at point one GPR $a0 $v0 $s0 $s1 $s2 $t3 $sp value Memory, third time at point one higher addresses STACK data saved before main was called..data int zed(int za) return za >= -8 && za < 9; void wye(int *a, int *aend, const int *b) while (a < aend) if (zed(*b)) *a = *b; else *a = 12; a++; b++; int exx[4] = -9, -8, 11, 8 ; int main(void) int ww[4]; wye(ww, ww + 4, exx); //...more code... zed:.globl zed addi $v0, $zero, 0 slti $t0, $a0, -8 bne $t0, $zero, L1 slti $v0, $a0, 9 # POINT ONE L1: jr $ra.globl wye wye: addi $sp, $sp, -16 sw $ra, 12($sp) sw $s2, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) add $s0, $a0, $zero add $s1, $a1, $zero add $s2, $a2, $zero L2: slt $t2, $s0, $s1 beq $t2, $zero, L3 lw $a0, ($s2) jal zed beq $v0, $zero, L4 lw $t3, ($s2) sw $t3, ($s0) j L5 L4: addi $t4, $zero, 12 sw $t4, ($s0) L5: addi $s0, $s0, 4 addi $s2, $s2, 4 j L2 L3: lw $s0, 0($sp) lw $s1, 4($sp) lw $s2, 8($sp) lw $ra, 12($sp) addi $sp, $sp, 16 jr $ra.data.globl exx exx:.word -9, -8, 11, 8.globl main main: addi $sp, $sp, -20 sw $ra, 16($sp) addi $a0, $sp, 0 addi $a1, $sp, 16 la $a2, exx jal wye #... more code... addi $v0, $zero, 0 lw $ra, 16($sp) addi $sp, $sp, 16 jr $ra 0x1001_0000 return 0;
ENCM 369 Winter 2017: Midterm Test #1 page 5 of 5 PROBLEM 4 (total of 7 marks) Part a. (4 marks.) Assume that $t0 contains 0x0003_5000 and $t1 contains 0x000a_6000 before the following sequence of instructions runs: and $t2, $t0, $t1 nor $t3, $t1, $zero srl $t4, $t2, 2 lui $t5, 0x73ac What are the values in $t2, $t3, $t4, and $t5 after the instructions have run? Give your answers as hexadecimal numbers. Part b. (3 marks.) In ENCM 369, the words preprocessor and compiler are the names for two tools in the toolchain for building executable files from C source files. Briefly but precisely describe what each of these two tools does. MARKS: The space below will be used to record your marks for each question and your overall test mark. Please put your name, signature, and U of C ID number in the appropriate boxes. Name (printed): Signature: U of Calgary ID number: Problem Mark 1 / 11 2 / 13 3 / 13 4 / 7 Lecture Section (01 is 11:00am with S. Norman, 02 is 10:00am with N. Bartley): TOTAL / 44