How to calculate negative number in MIPS assembly?

online.0227 picture online.0227 · Mar 29, 2016 · Viewed 10.5k times · Source

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)

Answer

Akshita picture Akshita · Jun 14, 2017

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.