1

I need to implement an instruction in MIPS assembly that jumps to a location stored in a register if its value is non-negative; otherwise, it jumps to a location stored in a second register.

I'm having an issue with how to check for negative values in registers and also need help understanding how to implement this.

orde
  • 5,233
  • 6
  • 31
  • 33
zeta
  • 23
  • 5

1 Answers1

1

Suppose you have in $t1 the test register (the one pointing to the address to jump if its contents is non-negative), and in $t2 the register which will hold the address of the jump if $t1 is negative.

Then, this snippet should do the trick:

    bge $t1, $zero, is_positive
    jr $t2
is_positive:
    jr $t1

The first instruction branches to is_positive if $t0 is non-negative. The instruction at that label jumps to the address given by $t1. If the branch is not taken (i.e. $t0 is negative), then the following instruction is executed which will jump to the address given by $t2.

gusbro
  • 22,357
  • 35
  • 46
  • i need to implement it as a single instruction – zeta Apr 15 '15 at 17:04
  • 1
    There is no instruction in MIPS ISA which does what you want. You can emulate it for example with the snippet given in the answer. – gusbro Apr 15 '15 at 17:25