CS447 Homework 3 - Due in Class - 2/14
  1. For each instruction in the code segment below, determine the instruction format, and the decimal and binary values of each instruction field.
     
    and $s1, $zero, $zero # initialize $s1 to 0
    add $t0, $s0, $zero # set $t0 to the start address of some array
    loop: lw $t1, 0 ($t0) # copy a word from the array into $t1
    add $s1, $s1, $t1 # add the value of $t1 to the running total in $s1
    addi $t0, $t0, 4 # increment $t0
    bne $t1, $zero, loop # loop if word copied does not equal zero

    Solution:

    and $s1, $zero, $zero
    R-type
    Decimal:00017036
    Binary:00000000000000001000100000100100

    add $t0, $s0, $zero
    R-type
    Decimal:01608032
    Binary:00000010000000000100000000100000

    lw $t1, 0($t0)
    I-type
    Decimal:35890
    Binary:10001101000010010000000000000000

    add $s1, $s1, $t1
    R-type
    Decimal:017917032
    Binary:00000010001010011000100000100000

    addi $t0, $t0, 4
    I-type
    Decimal:8884
    Binary:00100001000010000000000000000100

    bne $t1, $zero, loop
    I-type
    Decimal:590-12
    Binary:00010101001000001111111111110100

  2. As discussed in your book on page 157, pseudoinstructions are not actually part of the MIPS instruction set but are often used in MIPS programs.  For each pseudoinstruction in the table below, produce the shortest possible sequence of actual MIPS instructions that accomplish the function described, using $at when necessary (the usage of the $at register is explained on page 147).  In the following pseudoinstructions, big represents a 32-bit number and small represents a 16-bit number.  Note: With the exception of bgt, these instructions have been created for the purposes of this homework only, and should not be used in actual MIPS programs.
     
    Pseudoinstruction
    What it accomplishes
    Solution:
    dec $s0 $s0 = $s0 - 1 subi $s0, $s0, 1
        or
    addi $s0, $s0, -1
    quad $s0 $s0 = $s0 x 4 add $s0, $s0, $s0
    add $s0, $s0, $s0
        or
    sll $s0, $s0, 2
    swap $s0, $s1 $s0 = $s1 and $s1 = $s0 add $at, $s0, $zero
    add $s0, $s1, $zero
    add $s1, $at, $zero

    See Note!
    swinc $s0, $t0 stores $s0 to address $t0 in memory
    and increments $t0 by 4
    sw $s0, 0($t0)
    addi $t0, $t0, 4
    subi $s0, $s1, big $s0 = $s1 - big lui $at, big
    ori $at, big
    sub $s0, $s1, $at

    See Note!
    bgt $s0, small, L if ($s0 > small) go to L slti $at, $small, $s0
    bne $at, $zero, L

    See Note!

    Special Note: The $at register is reserved for system use - which would happen during pseudo-instructions. The $t# registers are reserved for programmer use. If pseudo-instructions were able to use $t# registers, you could never trust the values which you stored in those registers. Therefore, the system is forced to use the $at register to complete pseudo-instructions. Any students who used $t# registers in the solutions to their pseudo-instructions should take note of this...

  3. Convert the following decimal numbers into 8-bit two's complement binary numbers:
    1. 123ten
      Solution:
      123 ÷ 2 = 61  R1
      61 ÷ 2 = 30  R1
      30 ÷ 2 = 15  R0
      15 ÷ 2 = 7  R1
      7 ÷ 2 = 3  R1
      3 ÷ 2 = 1  R1
      1 ÷ 2 = 0  R1


      12310 = 0111 10112
    2. -96ten
    3. Solution:
      96 ÷ 2 = 48  R0
      48 ÷ 2 = 24  R0
      24 ÷ 2 = 12  R0
      12 ÷ 2 = 6  R0
      6 ÷ 2 = 3  R0
      3 ÷ 2 = 1  R1
      1 ÷ 2 = 0  R1


      9610 = 110 00002

      Sign extend to 8 bits -> 9610 = 0110 00002
      Convert to -96...
      Invert all bits -> 1001 1111
      Add 1...
      -9610 = 1010 00002

  4. Convert the following decimal numbers into 16-bit two's complement binary numbers:
    1. 9023ten
      Solution:
      9023 ÷ 2 = 4511  R1
      4511 ÷ 2 = 2255  R1
      2255 ÷ 2 = 1127  R1
      1127 ÷ 2 = 563  R1
      563 ÷ 2 = 281  R1
      281 ÷ 2 = 140  R1
      140 ÷ 2 = 70  R0
      70 ÷ 2 = 35  R0
      35 ÷ 2 = 17  R1
      17 ÷ 2 = 8  R1
      8 ÷ 2 = 4  R0
      4 ÷ 2 = 2  R0
      2 ÷ 2 = 1  R0
      1 ÷ 2 = 0  R1


      902310 = 0010 0011 0011 11112

    2. -746ten
      Solution:
      746 ÷ 2 = 373  R0
      373 ÷ 2 = 186  R1
      186 ÷ 2 = 93  R0
      93 ÷ 2 = 46  R1
      46 ÷ 2 = 23  R0
      23 ÷ 2 = 11  R1
      11 ÷ 2 = 5  R1
      5 ÷ 2 = 2  R1
      2 ÷ 2 = 1  R0
      1 ÷ 2 = 0  R1


      74610 = 010 1110 10102

      Sign extend to 16 bits -> 74610 = 0000 0010 1110 10102
      Convert to -746...
      Invert all bits ->1111 1101 0001 0101
      Add 1...
      -74610 = 1111 1101 0001 01102

  5. Convert the following 32-bit two's complement binary numbers into decimal numbers:
    1. 1111 1111 1111 1111 1111 1010 0010 1010two
      Solution:
      Convert to positive...
      Invert all bits -> 0000 0000 0000 0000 0000 0101 1101 0101
      Add 1 -> 0000 0000 0000 0000 0000 0101 1101 0110

      (1x210) + (1x28) + (1x27) + (1x26) + (1x24) + (1x22) + (1x21) = 1494
      Since we converted the original bit string to positive, we simply convert the decimal value back to negative...
      1111 1111 1111 1111 1111 1010 0010 1010two = -1494ten
    2. 0000 0000 0000 0000 0001 1100 1011 0001two
      Solution:
      (1x212) + (1x211) + (1x210) + (1x27) + (1x25) + (1x24) + (1x20) = 7345

      0000 0000 0000 0000 0001 1100 1011 0001two = 7345ten

  6. Show the 8-bit two's complement representation of the decimal number 73, then show how to find the 16-bit two's complement representation of -73.
     
    Solution:
    73 ÷ 2 = 36  R1
    36 ÷ 2 = 18  R0
    18 ÷ 2 = 9  R0
    9 ÷ 2 = 4  R1
    4 ÷ 2 = 2  R0
    2 ÷ 2 = 1  R0
    1 ÷ 2 = 0  R1


    7310 = 100 10012

    Sign extend to 8 bits -> 7310 = 0100 10012
    Convert to -73...
    Invert all bits -> 1011 0110
    Add 1...
    -7310 = 1011 01112
    Sign extend to 16 bits -> 1111 1111 1011 0111
  7. The andi and ori instructions are I-type instructions, and thus limit the masks to be 16 bits.
    1. Explain how these instructions affect the upper 16 bits of the result.
      Solution: They pad the constant to the left with zeros to make it a 32-bit constant.
    2. How does this affect you as an assembly language programmer?  (i.e., under what circumstances is what MIPS does ok, and under what circumstances is it not ok?  When it is not ok, what do you have to do to overcome the problem?)
      Solution: This is ok sometimes and not ok others. For example, suppose you want to set the last four bits of regsiter $s0 to 1 (while leaving the rest unchanged). You would do:

      ori $s0, $s0, 0xf

      The 16-bit representation of 0xf is 0000 0000 0000 1111, so padding with zeros gives us 0000 0000 0000 0000 0000 0000 0000 1111, which correctly leaves the 16 most significant bits unchanged. However, if you want to clear the last four bits to 0 (while leaving the first 16 unchanged), you might try:

      andi $s0, $s0, 0xfff0

      The 16-bit representation of 0xfff0 is 1111 1111 1111 0000, so padding gives 0000 0000 0000 0000 1111 1111 1111 0000. Thus, you will actually clear the first sixteen and the last four bits to 0, which is not the desired effect. What you really wanted to do was to and $s0 with 1111 1111 1111 1111 1111 1111 1111 0000. Therefore, because andi pads with zeros, you'll have to make the 32-bit constant yourself in this case. You could do:

      lui $t0, 0xffff     # sets first 16 bits to 1
      ori $t0,$t0,0xfff0     # sets last 16 bits of desired bit mask
      and $s0,$s0,$t0     # note we use "and" now, not "andi"


  8. You have been asked to implement a circuit for a graphics application.  This circuit consists of three inputs (Pixel1, Pixel2, and M) and one output (PixelOut).  When M equals 1, PixelOut should equal the disjunction (OR) of Pixel1 and Pixel2.  If M equals 0, PixelOut should equal the conjunction (AND) of Pixel1 and Pixel2.
    1. Construct a truth table to describe the behavior of the circuit.
      Pixel 1Pixel 2 M  Pixel Out
      000 0
      001 0
      010 0
      011 1
      100 0
      101 1
      110 1
      111 1
    2. Write the logic function for PixelOut.
      Solution:
      Question 8.b

      Note: If your homework has "Reduce" written next to the logic function for PixelOut no points were taken off, but you should make a point of reducing logic equations before implementing a PLA or constructing the circuit with AND and OR gates. In the case of implementing a PLA, note that you can only do reductions that keep the logic equation in the standard "sum-of-products" format, like the example.
    3. Specify a PLA to implement this function.
      Solution:
      Question 8.c
    4. Show how to implement this function with AND and OR gates.
      Solution:
      Question 8.d