University of Calgary Department of Electrical and Computer Engineering ENCM 369: Computer Organization Lecture Instructors: S. A. Norman and N. R. Bartley Winter 2012 MID-SESSION TEST Tuesday, March 6 6:30pm to 8:15pm Please do not write your U of C ID number on this cover page. Name (printed): Signature: Lecture section (L01 is MWF 11:00am with S. Norman, L02 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. 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.
ENCM 369 Winter 2012 Mid-Session Test page 2 of 8 PROBLEM 1 (12 marks) Consider the C code listed to the right. Translate the function proc1 into SPIM 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 helper(int x, int *p); int proc1(int *a, const int *b, int n) int k, s, v2; s = 0; for (k = 0; k < n; k++) s += helper(b[k], &v2); a[k] = v2; return s;
ENCM 369 Winter 2012 Mid-Session Test page 3 of 8 PROBLEM 2 (9 marks) Consider the C code listed to the right. Translate the function insert_spaces into SPIM 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. Note: The SPIM assembler will accept (the character constant for a space) as an operand in instructions such as addi and ori. void insert_spaces(char *dest, const char *src) if (*src!= \0 ) *dest = *src; dest++; while (*(src + 1)!= \0 ) *dest = ; *(dest + 1) = *(src + 1); dest += 2; src++; *dest = \0 ;
ENCM 369 Winter 2012 Mid-Session Test page 4 of 8 PROBLEM 3 (11 marks) The SPIM program below on the right is a correct translation of the C program below on the left, using the usual procedure calling conventions presented in ENCM 369. The SPIM program reaches point one twice. For the second of those times fill in both the diagram of the stack and the table of register values, found near the bottom of this page. Show all register and stack slot contents as numbers, using either base ten or hexadecimal notation as convenient. Note that the program will not actually use all of the stack slots shown in the diagram. Assume the following register contents when main starts: $ra = 0x0040_0018; $sp = 0x7fff_fd9c; $s0 = 0x50; $s1 = 0x51. Also assume that the address of the first instruction of main is 0x0040_0024, the address of the first instruction of foo is 0x0040_0080, and the address of the first instruction of bar is 0x0040_0098. void foo(int *q); void bar(int i, int *p); int main(void) int a, b, c; a = 31; b = 47; c = 52; bar(100, &b); bar(200, &c); a += c - b; return 0; void foo(int *q) *q = *q / 10;.text.globl main main: addiu $sp, $sp, -16 sw $ra, 12($sp) sw $s0, 8($sp) addiu $s0, $zero, 31 addiu $t0, $zero, 47 sw $t0, 0($sp) addiu $t1, $zero, 52 sw $t1, 4($sp) addiu $a0, $zero, 100 addiu $a1, $sp, 0 jal bar addiu $a0, $zero, 200 addiu $a1, $sp, 4 jal bar lw $t2, 4($sp) lw $t3, 0($sp) subu $t4, $t2, $t3 addu $s0, $s0, $t4 addu $v0, $zero, $zero lw $s0, 8($sp) lw $ra, 12($sp) addiu $sp, $sp, 16 jr $ra.globl foo foo: lw $t5, ($a0) addiu $t6, $zero, 10 div $t5, $t6 mflo $t7 sw $t7, ($a0) # POINT ONE void bar(int i, int *p) foo(p); *p += i; high addr esses data saved before main was called jr $ra.globl bar bar: addiu $sp, $sp, -12 sw $ra, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) addu $s0, $a0, $zero addu $s1, $a1, $zero addu $a0, $s1, $zero jal foo lw $t8, ($s1) addu $t9, $t8, $s0 sw $t9, ($s1) lw $s0, 0($sp) lw $s1, 4($sp) lw $ra, 8($sp) addiu $sp, $sp, 12 jr $ra register $a0 $ra $sp $t5 value
ENCM 369 Winter 2012 Mid-Session Test page 5 of 8 PROBLEM 4 (total of 11 marks) In this problem, you are asked to translate sequences of one or more C statements into sequences of one or more SPIM instructions, not complete SPIM procedures. Use only instructions from the Midterm Instruction Subset. Use as many t-registers as you wish for intermediate values. Example. (No marks.) GPR $s0 is used for x, of type int. C code SPIM instruction(s) x = 42; addi $s0, $zero, 42 Part a. (2 marks.) $a0 is used for p, of type int*, and $s1 is used for i, of type int. C code SPIM instruction(s) p[3] += i; Part b. (4 marks.) $s0 is used for j and $s1 is used for k, both of type int. $s2 is used for p, of type int*; The function func has an argument of type int and returns an int. C code if (p == 0 *p < 32) j++; else j += func(*p); k++; SPIM instruction(s) Part c. (3 marks.) An array of 5 ints named x is allocated within a stack frame. The address of x[0] is 8($sp). $s0 is used for the int variable k. The function swap takes two arguments of type int* and does not return a value. C code swap(&x[0], &x[k]); SPIM instruction(s) Part d. (2 marks.) $s3 is used for m, and $s4 for n, both of type unsigned int. C code SPIM instruction(s) m = n % 99;
ENCM 369 Winter 2012 Mid-Session Test page 6 of 8 PROBLEM 5 (total of 10 marks). The SPIM program below on the right is a correct translation of the C program below on the left, assuming that the address of pmax is 0x1001_0000. The program searches the array arr for its largest element, and leaves the address of that element in the variable pmax. int *pmax = 0; int arr[6] = 10, 11, 12, 13, 99, 15 ; int main(void) int *p; int *guard; int max; pmax = arr; p = arr; max = *p; guard = p + 6; p++; while (p!= guard) if (max < *p) max = *p; pmax = p; p++; /* POINT ONE */ return 0; Part a. (1 mark.) List the two assembly language instructions that correspond to the C statement p = arr;.data.globl pmax pmax:.word 0.globl arr arr:.word 10, 11, 12, 13, 99, 15.text.globl main main: lui $t0, 0x1001 ori $t0, 0x0004 lui $t1, 0x1001 sw $t0, 0($t1) lui $t7, 0x1001 ori $t7, 0x0004 lw $t9, ($t7) addiu $t8, $t7, 24 addiu $t7, $t7, 4 L1: beq $t7, $t8, L2 lw $t2, ($t7) slt $t3, $t9, $t2 beq $t3, $zero, L3 addu $t9, $zero, $t2 lui $t4, 0x1001 sw $t7, ($t4) L3: addiu $t7, $t7, 4 j L1 L2: # POINT ONE addu jr $v0, $zero, $zero $ra Part b. (1 mark.) List the two assembly language instructions that correspond to the C statement pmax = p; Part c. (8 marks.) Fill in the following tables to show contents of registers and memory when the SPIM program reaches point one. All memory words and register values must be given as numbers, but for each number you can choose base ten or hexadecimal format, whichever is more convenient. memory address 0x1001_0000 0x1001_0004 0x1001_0008 0x1001_000c 0x1001_0010 0x1001_0014 0x1001_0018 memory word value register $t2 $t3 $t7 $t8 $t9 value
ENCM 369 Winter 2012 Mid-Session Test page 7 of 8 PROBLEM 6 (total of 13 marks) Part a. (3 marks.) Suppose $t0 contains 0x5c00_0000, $t1 contains 0xa400_0000, and the instruction add $t2, $t0, $t1 is run. What is the 32-bit addition result, expressed in base sixteen? Will there be an exception caused by this instruction? Briefly explain why there will or will not be an exception. Part b. (3 marks.) Suppose an 8-bit subtraction circuit is used to compute a b, where a = 0x90 and b = 0x22. Expressing your answer in base two, what will be the 8-bit subtraction result? Is there overflow in this computation? Is there wraparound? Part c. (3 marks.) Suppose $t0 contains 0xab2a_bcde. What will be in $t3 after these three instructions are run? sll $t1, $t0, 8 andi $t2, $t1, 0x3f00 ori $t3, $t2, 0x8009 Express your answer in base sixteen, and show how you obtained your answer. Part d. (2 marks.) Suppose $t0 contains 0x8000_0000 and $t1 contains 0x0000_1000. Suppose mult $t0, $t1 is executed. What will be the contents of the Hi and Lo registers after this instruction is executed? (Hint: 0x1000 is 2 12.) Part e. (2 marks.) Suppose a student is trying to write assembly language code for a C procedure in which some variables are ints and others are doubles. In particular, k is an int in $s0, and s and t are doubles in floating-point registers $f20 and $f22. The student tries to code s = k - t as sub.d $f20, $s0, $f22. Explain briefly why that idea is seriously incorrect. (Since we have just started to study floating-point programming, you do not have to give correct code assembly language code.)
ENCM 369 Winter 2012 Mid-Session Test page 8 of 8 PROBLEM 7 (total of 8 marks) Part a. (4 marks.) The tools in the toolchain used in C programming are: assembler, compiler, linker, preprocessor. Note that the order of this list is alphabetical and might not reflect the order in which the tools are used. The kinds of files these tools work with are: assembly language files,.c files, executable files, header files, library machine code files, object files, translation units. This list is also alphabetical and might not reflect the order in which these files are created. Fill in the following table. Note that some tools may have more than one kind of input file, but each tool has only one kind of output file. input file(s) tool output file assembler compiler linker preprocessor Part b. (4 marks.) As presented in a recent lecture, an IEEE 754 32-bit floating-point number is organized as follows: bit 31 is the sign bit, bits 30 23 are used for the biased exponent, and the other 23 bits are for the fraction part of the significand. The bias in the exponent is 01111111 two = 127 ten. Use the above information to determine what base ten number is represented by the 32-bit IEEE floating-point bit pattern 0x41980000. Show the work needed to obtain your answer. 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: Problem Mark 1 / 12 2 / 9 3 / 11 U of Calgary ID number: 4 / 11 5 / 10 6 / 13 Lecture Section (L01 is 11:00am, L02 is 10:00am): 7 / 8 TOTAL / 74