| 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: 0 0 0 17 0 36 Binary: 000000 00000 00000 10001 00000 100100
add $t0, $s0, $zero
R-type
Decimal: 0 16 0 8 0 32 Binary: 000000 10000 00000 01000 00000 100000
lw $t1, 0($t0)
I-type
Decimal: 35 8 9 0 Binary: 100011 01000 01001 0000000000000000
add $s1, $s1, $t1
R-type
Decimal: 0 17 9 17 0 32 Binary: 000000 10001 01001 10001 00000 100000
addi $t0, $t0, 4
I-type
Decimal: 8 8 8 4 Binary: 001000 01000 01000 0000000000000100
bne $t1, $zero, loop
I-type
Decimal: 5 9 0 -12 Binary: 000101 01001 00000 1111111111110100
|
|
|
|
| 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...
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
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
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
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
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
Solution:
(1x212) + (1x211) + (1x210) + (1x27) + (1x25) + (1x24) + (1x20) = 7345
0000 0000 0000 0000 0001 1100 1011 0001two = 7345ten
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
Solution: They pad the constant to the left with zeros to make it a 32-bit constant.
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"
Pixel 1 Pixel 2 M   Pixel Out 0 0 0   0 0 0 1   0 0 1 0   0 0 1 1   1 1 0 0   0 1 0 1   1 1 1 0   1 1 1 1   1
Solution:
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.
Solution:
![]()
Solution:
![]()