Jumping
Jumping is where we change the flow of our program by jumping to a different part of the code - we essentially ‘goto’ a certain line (or memory address) in our code.
Please have a look at the last chapter to see how labels work.
The j instruction
Section titled “The j instruction”The most basic instruction we can use to branch is j. It will always jump to
the label you give it, no matter what.
Syntax
Section titled “Syntax”The j instruction can be used like this:
j label_nameExample
Section titled “Example”For example, here, we jump to the stop label before we add 1 to the a0
register:
.textmain: li a0, 5 # Load 5 into a0 j stop # Jump to the 'stop' label addi a0, a0, 1 # This line will be skippedstop: li a7, 10 ecallThis program will load 5 into the a0 register, then jump to the stop
label, skipping the line that would have added 1 to a0. Finally, it will
exit the program.
The result in the registers will be:
a0: 5