a branching instruction
has one slot (one instruction) delay
The cause of this delay is the fact that the SPARC CPU is "pipelined" (more in CS355) to make the SPARC CPU execute instruction faster. |
What this means is:
The instruction that immediately follows a branching instruction in the SPARC assembler program is always fetched (and will be executed unless annulled/cancelled) |
You can compare this effect with a car that tries to make a turn when it is going at very high speed... the inertia will make it skid forward a little further before it could make the turn. --- So also, a SPARC CPU that is running at extreme speed will not be able to branch to a different location right away - it first picks up one extra instruction (the one following the branching instruction) and then branches to the target address.
To counter this "delayed" branching effect, always insert a nop operation immediate after each breach instruction |
subcc %r1, %r2, %g0 Computes the difference r1 - r2 and set the status flags (N,Z,V,C) according to the outcome of the subtraction The effect is to compare the value in register r1 against the value in r2 Register g0 cannot be updated, so only the N, Z, V, C flags are updated |
All we need to do is to make the assembler translate
cmp %r1, %r2 ----> subcc %r1, %r2, %g0
cmp %r1, %r2 ----> subcc %r1, %r2, %g0 |
What that means is you can write "cmp %r1, %r2" in your assembler program !
Just like M68000, you will need to follow the "cmp" instruction with a conditional branch/jump instruction ! (discussed now)
bl LABEL If the status flags, at the moment that this instruction is executed, indicates that the cmp operation resulted in a "LESS THAN" condition, the CPU is instruction to "branch" or "jump" to the memory location marked by LABEL. But due to "pipelining" inside the SPARC CPU, the instruction following the bl LABEL instruction will be executed before the CPU can do the branch If the status flags indicates the otherwise (i.e., "NOT LESS THAN"), then the CPU will continue the execution with the next instruction (i.e., the instruction that follows the bl LABEL instruction)
sethi %hi(A), %l0 ld [%l0 + %lo(A)], %l0 // l0 = A sethi %hi(B), %l1 ld [%l1 + %lo(A)], %l1 // l1 = B cmp %l0, %l1 // Compare l0 (A) against l1 (B) bl L1 // Branch to location L1 if A < B nop // Always put a nop after a branch ! add %g0, 1, %g0 .... .... .... L1: add %g0, 2, %g0 ....
Be very careful with the order of the operands in CMP !
The meaning of the jump is changed if you flip the operands:
Notice also that the meaning of the cmp instruction in SPARC and M68000 is "opposite" (because the operands are swapped in the subtract operation).
M68000 SPARC ============ ============== cmp.L x, y cmp x, y blt L1 bl L1 -- Branch to L1 if -- Branch to L1 if -- y < x -- x < y
M68000 SPARC ============ ============== cmp.L x, y cmp x, y computes: y - x computes: x - y
And if you understood the techniques for if, if-else, while, and for-statements for M68000, you will find the next few pages very boring....
We will examine each control statement for SPARC again with the same examples as in M68000...