Jump anywhere: Difference between revisions

Content added Content deleted
Line 1,959:
 
=={{header|MIPS Assembly}}==
<code>j</code>, <code>jr</code>, <code>jal</code>, and <code>jalr</code> are limitlessable andto cantake literallyyou goto '''anywhere;''' usein themthe atCPU's youraddress own risk!space.
 
* &nbsp; <code>j 0xNNNNNNNN</code> sets the program counter equal to <code>0xNNNNNNNN</code>.
* &nbsp; <code>jr $NN</code> sets the program counter equal to the value in register <code>$NN</code>.
* &nbsp; <code>jal 0xNNNNNNNN</code> sets the program counter equal to <code>0xNNNNNNNN</code>, and moves the old program counter plus 8 into register <code>$ra</code>.
* &nbsp; <code>jalr $NN,0xNNNNNNNN</code> sets the program counter equal to <code>0xNNNNNNNN</code> and moves the old program counter plus 8 into register <code>$NN</code>.
 
Branches apply a signed offset to the current program counter. They are limited only by distance; scope does not exist in assembly. Typically you do not have calculate this offset yourself. The assembler will abstract this out of the hardware and let you use a label like you would with a <code>j</code>. The actual offset is calculated during the assembly process, which means you don't have to measure it yourself by counting bytes.