Spring 2021 CPE380 Assignment 3

Sample solution.

  1. For this question, check all that apply. Which of the following are correct statements about assembly languages for most modern computers?
    a+b is an add producing an rval, but a[b] is an add that produces an lval
    The same add instruction could compute an integer result or an address
    Local variables for a function are usually allocated in registers or in the stack frame
    Enable masking is a key aspect of MIMD supercomputers and Multi-core processors
    No, that's primarily for SIMD
    Most computers do not have special instructions that implement if, else, while, and for
  2. For this question, check all that apply. MIPS is not a CISC architecture, but a RISC with a simple and highly regular instruction set. Which of the following attributes correctly describe the MIPS instruction set?
    Each instruction is encoded in precisely one 32-bit word
    There is a call instruction that pushes the return address on the stack
    No, jal puts the return address in $31 (aka $ra)
    The result of a comparison for less than is a 32-bit integer value of 0 or -1
    No, 0 or 1
    There are no unconditional branch instructions, only conditional branches
    Of course, beq $t,$t is always true
    A single instruction can load a value from memory and add it to any register
    No, lw is the only way to load a word, and you need a separate add
  3. An array a of 32-bit integers is specified as shown below. On the MIPS architecture, suppose &(a[0]) is 4000. What is the rval of a[3]?
    int a[3] = { 601, 86, 42, 22 };
    

  4. Which of the following segments of C code best describes what the following MIPS assembly code does? (Note: oddly, unlike nearly every programming language, MIPS add detects integer overflow -- so you really should always use addu as this code does.)
    	la	$t0, x
    	lw	$t1, 0($t0)
    	la	$t2, y
    	lw	$t3, 0($t2)
    l1:	bne	$0, $t3, l2
    	addu	$t1, $t1, $t3
    	beq	$t1, $t1, l1
    l2:	sw	$t1, 0($t0)
    

    x=x+y;
    if (y==0) { x=x+y; }
    if (y!=0) { x=x+y; }
    while (y==0) { x=x+y; }
    while (y!=0) { x=x+y; }
  5. What is the 32-bit hexadecimal value used to represent the MIPS assembly language instruction ori $t0,$0,1? Hint: you can use SPIM to find out.
  6. This MIPS/SPIM program includes a subroutine called myadd that performs x=(y+z);. In the space below, replace the myadd subroutine with one named negz that will make x have the value which is the 2's complement negation of z (i.e., -z). You should test your routine using SPIM before you submit it, which will require merging it with a test framework like the one used in this MIPS/SPIM program -- but only submit the negz routine here. Remember that you can and should comment your code, especially if there are any known bugs. Half off for documented bugs. :-)
  7. This (now familiar) MIPS/SPIM program includes a subroutine called myadd that performs x=y+z;. In the space below, replace the myadd subroutine with one named popcount that will compute x=popcount(y);, the Population Count of y, the number of 1 bits in y's value. and the following C code gives a simple algorithm to compute it. This uses a little trick credited to Brian Kernighan (and described here) to count the "population" of 1s; t0 & (t0 - 1) removes the least-significant 1 bit from the value of t0.
    extern int x, y, z;
    
    void
    popcount(void)
    {
        int t0 = y;
        int t1 = 0;
    
        while (t0) {
            t1 = t1 + 1;
            t2 = t0 - 1;
            t0 = t0 & t2;
        }
        x = t1;
    }
    

  8. This MIPS/SPIM program includes a subroutine called myadd that performs x=(y+z);. In the space below, replace the myadd subroutine with one named sort that will re-order the values of x, y, and z to be in increasing order: the smallest value should end up in x and the largest in z. Your code should take advantage of the fact that x, y, and z are consecutive words in memory; you should treat x as an array of three elements. You should test your routine using SPIM before you submit it, which will require merging it with a test framework like the one used in this MIPS/SPIM program -- but only submit the sort routine here.
  9. Briefly explain: what MIPS instructions would you expect to be generated by the assembly code: li $t0,0x12345678
  10. What does the following C++ program print? Why?
    #include <iostream>
    using namespace std;
    int a=380, b=480, c=42;
    void f(int *x, int &y, int z) { ++*x; ++y; ++z; }
    int main() {
      cout <<"a="<<a <<" b="<<b <<" c="<<c <<endl;
      f(&a,b,c);
      cout <<"a="<<a <<" b="<<b <<" c="<<c <<endl;
    }
    


CPE380 Computer Organization and Design.