EE380 Assignment 2 Solution

    100% For this question, check all that apply. Which of the following are correct statements about assembly languages for most modern computers?
    A SIMD computer generally has some support for enable masking
    The same add instruction could compute an integer result or an address
    There is no while instruction; jumps/branches are used to implement loops
    Data is marked with its type in memory so int and float cannot be confused
    Local variables for a function are found in memory at an address computed as an offset from the stack pointer
  1. 100% 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?
    There are special instructions for stack push/pop
    Each instruction is encoded in precisely one 32-bit word
    The result of a comparison is setting of a special condition code register
    Arithmetic instructions allow 3 registers: two sources and one destination
    One operand to an arithmetic instruction can come from an address in memory
  2. 100% An array a of 32-bit integers is specified as shown below. On the MIPS architecture, suppose &(a[0]) is 8000. What is the rval of a[2]?
    int a[3] = { 42, 86, 601 };
    

  3. 100% 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:	beq	$t1, $0, l2
    	addu	$t1, $t1, $t3
    	j	l1
    l2:	sw	$t1, 0($t0)
    

    x=x+y;
    if (x==0) { x=x+y; }
    if (x!=0) { x=x+y; }
    while (x==0) { x=x+y; }
    while (x!=0) { x=x+y; }
  4. 100% What is the 32-bit hexadecimal value used to represent the MIPS assembly language instruction addi $t0,$0,1?
  5. 100% 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 myxor that will make x have the value it would get if C code like x=(y^z); were executed, i.e., the result of eXclusive ORing y and 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 myxor routine here. Remember that you can and should comment your code, especially if there are any known bugs. Half off for documented bugs. :-)
  6. 50% 50% 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 ispow2 that will compute x=ispow2(y);, which will return 1 if y is a power of 2 and 0 otherwise. The algorithm for this is on the MAGIC algorithms page; basically, here you are implementing (x&(x-1))==0. (Hint: you can convert a non-zero value to 1 using the sltu instruction comparing with 0.) You should test your routine using SPIM before you submit it.
  7. 100% Sorting numbers is a very common problem; here, you just have 3 numbers to sort. 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 mysort that will sort the values of x , y, and z and into increasing order (i.e., make x the smallest and z the largest). (Hint: your code may take advantage of the fact that x, y, and z are consecutive words in memory.) 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 mysort routine here.
  8. 50% 50% In the space below, write MIPS code for a leaf subroutine mymul that will compute x=y*z;. Hint: use addu because add detects overflow. You should test your routine using SPIM before you submit it. Although there are many ways to do a multiply, here's C code for one of them that you might want to use:
    extern int x, y, z;
    
    void
    mymul(void)
    {
         /* do x = y * z the hard way... */
         int t0 = 0;
         int t1 = 1;
         int t2 = y;
         int t3 = z;
    
         while (t1 != 0) {
              if ((t1 & t3) != 0) {
                   t0 += t2;
              }
              t1 += t1;
              t2 += t2;
         }
    
         x = t0;
    }
    


EE380 Computer Organization and Design.