zeta
zeta

Reputation: 23

how would I implement a certain instruction in MIPS?

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.

Upvotes: 1

Views: 102

Answers (1)

gusbro
gusbro

Reputation: 22585

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.

Upvotes: 1

Related Questions