0

So currently at $t0 I have a variable x stored. I want $t0 to now store -x. How can I do this?

I tried

sub $t4, 0, $t0 move $t0, $t4

Any pointers as to why this says parser syntax error?

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • Does this answer your question? https://stackoverflow.com/questions/53568440/about-negate-a-sign-integer-in-mips – noam Jul 14 '20 at 16:31

2 Answers2

2

By the formula 0-x = -x , and knowing that $0 is hard-coded to zero.

Try

sub $t0, $0, $t0

This will negate $t0 and then put it back into $t0.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
1

You can store -x in $t0 by using these 2 lines :

not $t0,$t0
addi $t0,$t0,1

These 2 lines basically apply 2's complement to the binary value stored in $t0.

NOTE: This might not be the most optimised choice in MIPS, but it does the job.

NickDelta
  • 3,697
  • 3
  • 15
  • 25