1

I am trying to compile following assembly source code, First here is code :

#include <xc.h>

.global main
.text

.set noreorder

.ent main 

main:
    
nop /* "no operation"... replace this. */
nop /* "no operation"... replace this. */
nop /* "no operation"... replace this. */
    
addiu $s1, $zero, 22
addiu $s2, $zero, 59
sub   $t2, $s1,   $s2  

.end main 

And here is my problem:

As you see, $s1 = 22 and $2 = 59. So, 22 - 59 = -37

But when I watch $t2 variable it has 4294967259 (in decimal). I don't understand why... it should be -37...

Here is output photo: click here to see my incorrect output

Question 1.

How to fix above problem?

Question 2.

How to calculate negative number?

for example, -22 - 33 = - 55

and source code for this:

add $s1, $zero, -22
add $s2, $zero, -10
sub $t2, $s1,   $s2   

But it also doesn't work. $s1 has like 4294967274 in decimal.. and $s2 same..

Thank you very much if you can help me this problem. (I run compiler called MPLAB X IDE)

Community
  • 1
  • 1
online.0227
  • 640
  • 4
  • 15
  • 29
  • You are just printing the value as unsigned. Check your IDE if it can be switched to signed. See also [two's complement](https://en.wikipedia.org/wiki/Two's_complement). – Jester Mar 29 '16 at 17:06
  • @Jester there is no such switching option in watch windows.. – online.0227 Mar 29 '16 at 17:56
  • Maybe you can cast it to int? Watch `(int)$s1` or similar? – Jester Mar 29 '16 at 17:59
  • @Jester the watch windows doesn't allow casting. About two's complement I know the concept (flip and then +1) but I do not know the mips instruction that does flipping. It seems like i can't just put negative number into any resgister of $s's – online.0227 Mar 29 '16 at 18:52
  • No it's already negative, 4294967274 is -22. Is the display really a problem? – harold Mar 29 '16 at 19:37
  • Your missing the point. The register T2 does contain the value -37. It does contain a negative number. The 32-bit unsigned value 4294967259 and the 32-bit signed twos-complement value -37 have exactly the same representation. Both are stored as 11111111111111111111111111011011 in binary in the register. Your IDE is choosing to display the register using the unsigned interpretation rather than the signed interpretation. – Ross Ridge Mar 29 '16 at 19:55

1 Answers1

2

I'm not sure about Question 1 - however, for Question 2:

addi $s1, $zero, -22
addi $s2, $zero, -10
sub  $t2, $s1,   $s2

This should fix the problem.

Akshita
  • 36
  • 2