sub and neg Instructions


The subtraction instruction computes the 2’s complement operation between the two source registers (rs and rt), and places the result in the destination register rd. The unary operation neg (“taking the 2’s complement”) performs its function on the value in the source and places the result in the destination.
sub rd, rs, rt         # subtract with overflow
Computes the two’s complement difference for two signed values. This instruction subtracts the contents of rt from the contents of rs, or it can subtract the contents of the immediate from the rs value. Subtract puts the result in the destination register. When the true result’s sign differs from the destination register’s sign, the machine signals an overflow exception.

neg rd, rs         # negate with overflow
Computes the negative of a value. This instruction negates the contents of rs and puts the result in the destination register rd. If the value in rs is -2147483648=2-31, the machine signals an overflow exception.
sub Char '7' to Int 7 neg
     .data 
 X:  .byte  10
 Y:  .word  -20

     .text 
     lb   $t0, X
     lw   $t1, Y 
     sub  $a0, $t1, $t0
     li   $v0, 1 
     syscall
     .data
 c:  .byte   '7' 

     .text 
     lb    $t0, c
     
     move  $a0, $t0 
     li    $v0, 1
     syscall
   .text 
   li    $t0, -20
   move  $t1, $t0
   neg   $t1, $t1
   sub   $a0, $t1, $t0
   li    $v0, 1
   syscall
Output Output Output




7 (integer)








      “If you have a dream, don’t just sit there.   
      Gather courage to believe that you can succeed and    
      leave no stone unturned to make it a reality.”    
      ― Dr Roopleen