Purpose of Branch Never?
Like a lot of CS students, I've been learning SPARC assembly lately and have been exposed to the Branch Never instruction, bn. I have searched through online sources, talked to instructors, grad students, and even read books - and while all will clearly and happily tell me exactly what bn does, not a single one of them has been able to answer my burning question, why does Branch Never exist? I apologize that this isn't an OpenSPARC discussion so much as a general assembly question, but the SPARC designers saw fit to include it so it seems like they must have had a reason, and that I think justifies this question being posed here. Thanks in advance for any help.
[678 byte] By [
Quasar84a] at [2007-11-26 22:21:06]

# 1
> why does Branch Never exist?
Branch Never does seem like an unnecessary instruction (kind of like an architectural "appendix" that might as well be removed, eh?).
Branch Never (BN) came "for free", in that out of the 16 possible logical encodings of branch conditions, it just was the natural result that fell out of the encoding of cond=0000.
The architects initially thought that implementations might eventually use it as an instruction prefetch opcode -- but it never ended up implemented that way.In UltraSPARC Architecture 2005, PREFECH fcn=0x11 ("prefetch to nearest unified cache") can be used for instruction prefetch. Note that PREFETCH 0x11 doesn't prefetch all the way to the Level-1 instruction cache, as BN would have, just to the nearest unified (I+D) cache (typically the L2 cache). PREFETCH 0x11 has an advantage over BN, in that it can target the entire 64-bit address space, whereas BN would have been limited to a PC-relative range of plus or minus 8MB (via a 22-bit word displacement).
So currently, there is no motivation to ever code Branch Never. Although it behaves like a NOP, it's better to simply code a NOP if that's what you need. On some implementations, BN might not execute as efficiently as a NOP.
# 2
There's also a branch never (brn) on the Motorola 9S12... it's effectively a two-byte no-op (but you knew that). They included it to be a compliment to the branch always (bra) instruction. They're both two-byte opcodes, so you can replace bra with brn when debugging without changing the byte offset of the rest of your assembly code.
According to the 9S12 family manual, some compiler implementations may find it useful to have a compliment for the branch always instruction. Why, I don't know. It probably also has implications in certain assembly language jokes that allude to the movie White Men Can't Jump.
# 3
Initially the branch never was implemented to save memory place at time when the cost of each memory byte was important (after 1975). Here is an example of usage of the branch never. Imagine you have an assembler routine that can terminate its job by putting into a register a result (i.e. 0 or -1).
myRoutine: ...
LDAA#0
RTS
l$:...
LDAA#-1
RTS
Let's assume some OP code
BRNopcode $23 --> BRN$4 --> $23, $04
LDAA opcode $96 --> LDAA#-1 --> $96, $FF
CLRA opcode $67 --> CLRA--> $67
Now I define a SKIP MACRO like this:
.MACRO SKIP
.byte$23; the opcode of the BRN
.ENDMACRO
Now I rewrite myRoutine like this:
myRoutine: ...
BEQl$
...
LDAA#-1
SKIP; the OP code of the BRN
l$:CLRA; CLRA or displacement of the BRN
RTS
If the result of the first BEQ is "equal", then we jump to the label l$, and we return from the routine with A = 0. If now the result of the BEQ is "not equal", then we set A to -1, and SKIP is the OP code of the branch never that in this case consider the OP code of the CLRA as its displacement. The Branch never ... never branch, this means that the CLRA is simply "absorbed" and we return from the routine with A = -1. It exists as well the LBRN (long never branch), used to absorb instruction with 2 bytes. It is clear that this is a bit tricky, but 30 years ago, if you could save many bytes using this method ... it was really useful at that time!
Regards, Edo.
EFRa at 2007-7-10 11:18:39 >

