assemblymipsmplabmips32pic32

How to calculate negative number in MIPS assembly?


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)


Solution

  • 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.