Book creator (disable)

MIPS Assembly/Control Flow Instructions

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search


Contents

[edit] Jump Instruction

The jump instructions load a new value into the IP register. This causes the next instruction read from memory to be retrieved from the new location.

Instruction:
j
type:
J Type

The j instruction loads an immediate value into the IP register. This immediate value is either a numeric offset or a label (and the assembler converts the label into an offset).

Instruction:
jr
type:
R Type

The jr instruction loads the IP register with a value stored in a register. As such, the jr instruction can be called as such:

jr $t0

assuming the target jump location is located in $t0.

[edit] Jump and Link

Jump and Link instructions are similar to the jump instructions, except that they store a return address in the $ra ($31) register. This allows a subroutine to return to the main body routine after completion.

Instruction:
jal
type:
J Type

Like the j instruction, except that the return address is loaded into the $ra register.

Instruction:
jalr
type:
R Type

The same as the jr instruction, except that the return address is loaded into the $ra register.

[edit] Example

Let's say that we have a subroutine that starts with the label MySub. We can call the subroutine using the following line:

jal MySub
...

And we can define MySub as follows to return to the main body of the parent routine:

jr $ra

[edit] Branch Instructions

Instruction:
beq
type:
I Type
Instruction:
bne
type:
I Type
Instruction:
blez
type:
I Type
Instruction:
bgtz
type:
I Type
Instruction:
blt
type:
I Type
Instruction:
bgt
type:
I Type
Instruction:
ble
type:
I Type
Instruction:
bge
type:
I Type

[edit] Set Instructions

Instruction:
slt
type:
R Type
Instruction:
slti
type:
I Type
Instruction:
sltu
type:
R Type
Instruction:
sltiu
type:
I Type