2

In my textbook, there are these lines of code:

addu $t0, $t1, $t2
nor $t3, $t1, $zero
sltu $t3, $t3, $t2
bne $t3, $zero, Overflow

I understand the addu function, but when I get to the nor function and everything afterward, I don't understand what it does. The textbook just says t3 = the 2's complement of t1 - 1 for the 2nd line, but I don't understand how that works in binary. Would it just switch out all 0's for 1's and vice versa and then interpret that as 2's complement, resulting in a negative number? The book's explanation doesn't make sense to me.

saturdy
  • 21
  • 1
  • 1
  • 3

2 Answers2

4

Yes nor-ing a value with $zero simply inverts all the bits. For example binary 101011 nor-ed with 0 results in binary 010100. This is also a way the assembler psuedo-op not can be implemented.

markgz
  • 6,054
  • 1
  • 19
  • 41
0

Your question refers to: nor $t3, $t1, $zero # t3 = the 2's complement of t1 - 1

Let me explain.... appying twos complement to a binary number is a way of converting that number between positive and negative. So for example if you have 0111 which is a +7, then you apply twos complement you get 1001 which represents -7. The method used is to flip all the bits, then add 1. So in our example of +7 0111 flipping the bits gives 1000 then adding 1 gives 1001, which is a twos complement representation of -7. If you start with 1001 which is -7 in twos complement, and you want the positive version, you apply the SAME method, flip the bits gives 0110 then add 1 gives 0111. So this same operation applies both ways.

Now, let's look at what NOR does with a small truth table:

t1 0 or nor
1 0 1 0
0 0 0 1

So NOR gives us a flipped t1, whatever t1 is, NORing with 0 produces the opposite. With this information we know that if we do t1 NOR 0 we get ~t1, and that is basically flipping all the bits in t1.

Now, can you think of a method that involves flipping bits? That's right, twos complement. Oh but twos complement flips them but also adds 1, so if we just want to flip them only, we can do the twos complement and then minus 1 to cancel out that add 1.

So this is why the textbook said this, because NORing with 0 essentially flips t1's bits, and another way of saying that is twos complement minus 1.

martagnan
  • 23
  • 2