Section
05 Part 03 The BRA Instruction |
Behind me the branches of a wasted and sterile existence are
cracking. ~Gustav Mahler |
Introduction
BRA BRanch
Always
The destination operand is added to the PC, the 68k
will continue reading at the new offset held in PC.
Examples
This is
similar to JMP with a few minor differences.
Below is the same list of instructions that the JMP had, except this
time the JMP instruction has been replaced with a BRA instruction:
move.w d0,d1 add.w d1,d1 add.w d1,d0 bra.s SkipCode add.w d2,d3 asr.w #$04,d0 SkipCode: move.w d0,d2 |
Right
in the middle is the new BRA instruction:
bra.s SkipCode |
What
the instruction does is pretty much the same as JMP:
The
main difference between the two, is memory and processing time. The BRA instruction is faster than the JMP
instruction, and is smaller in size, thus saving you processing time and
memory.
Now I
know what youre thinking, if BRA is faster and smaller, why do we need JMP?
A
good question, but it can only be answered in memory terms:
Offset |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
A |
B |
C |
D |
E |
F |
00244200 |
32 |
00 |
D2 |
41 |
D0 |
41 |
60 |
04 |
D6 |
42 |
E8 |
40 |
34 |
00 |
00 |
00 |
00244210 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00244220 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00244230 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
etc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
As
you can see, the BRA instruction (6004)
is three times smaller than the JMP instruction. 60 is
the bra.s and 04 is
the value that is added to the PC.
The
add value is only a byte big, and it is a signed value. This means the maximum number of bytes it can
branch forwards by is 7E,
and the maximum it can branch backwards by is 80. So obviously bra.s cannot reach certain places where the JMP
instruction can.
This
is for bra.s
though, where the s stands for short. Another available size is bra.w where w stands for word:
move.w d0,d1 add.w d1,d1 add.w d1,d0 bra.w SkipCode add.w d2,d3 asr.w #$04,d0 SkipCode: move.w d0,d2 |
Offset |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
A |
B |
C |
D |
E |
F |
00244200 |
32 |
00 |
D2 |
41 |
D0 |
41 |
60 |
00 |
00 |
06 |
D6 |
42 |
E8 |
40 |
34 |
00 |
00244210 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00244220 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00244230 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
00 |
etc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In
this case, the branch instruction is now twice as big as it was before (60000006), yet still smaller and
faster than JMP. The 60 is the bra, the 00 after it indicates .w, and
the 0006 is the value that is added to
the PC.
The add
value here is a word big and is also signed.
This now means the maximum number of bytes it can branch forwards by is 7FFE, and the maximum it can
branch backwards by is 8000.
As
you can see, the range is larger than bra.s, but
still smaller than the JMP instruction.
Usage
In
due time youll be looking to use the quickest and smallest instructions you
possibly can, below is a table to help narrow out the relationship between the
two branch sizes and the jump:
Instruction |
Memory/Size |
Range |
|
|
|
bra.s |
60
?? |
7E
bytes forwards or backwards |
bra.w |
60
00 ?? ?? |
7FFE
bytes forwards or backwards |
jmp |
4E
F9 00 ?? ?? ?? |
000000
FFFFFF (Unlimited) |
The bra.s is the smallest and quickest of the three, yet you are
extremely limited in how far away from the instruction you can branch to. The jmp on the
other hand allows you to jump to anywhere you like, however, it takes up more
memory, and takes a little longer for the 68k to process.
Generally,
if youre looking to optimise your code, your priority is:
bra.s -> bra.w
-> jmp |
Use bra.s to begin with.
If the branch location is too far away, use bra.w. If the branch location for bra.w is still too far away, use jmp.