We talked a little bit about control transfer instructions in Chapter 16 on the SPARC assembly language, specifically regarding the delay instruction. Now let's talk about them in more detail. Control transfer instructions perform the following tasks :
We will start with the test and branch or conditional branch instructions. They all have the same general instruction format, as shown here: instruction{,a} label {,a} represents an option that each conditional branch instruction offers and refers to the annul bit. If , a is appended to the instruction, the annul bit is set. We will talk about the annul bit again later on. Branch on integer condition codes instructionsThe next table shows the conditional test and branch instructions for the integer unit. The third column shows which conditions the PSR icc bits must satisfy in order for the branch to be taken. As a reminder, the icc bits are:
Here are the branch on integer condition codes instructions: Table B-12. Branch on integer condition codes instructions
Here is a snippet of assembly code that demonstrates how a branch instruction might be used. main+0x34: ld [%l4 - 0x8], %l0 main+0x38: subcc %l0, 0x6, %g0 main+0x3c: bne main + 0x5c main+0x40: nop main+0x44: ld [%fp - 0x8], %l0 A value is read in from memory and placed into local register %l0 . Using the "subtract & modify icc " instruction, we subtract 6 from the value in %l0 . No result is stored because the dreg or destination register is the /dev/null of registers, %g0 ; however, the icc bits are modified as appropriate. Using the "branch on not equal" instruction, we test the setting of the Z bit. If it is set to 1, the values were equal; the value in memory was a 6. If the Z bit is clear, the values were not equal and we jump to location main+0x5c . While doing the branch, the delay instruction, nop , gets executed. If the branch is not taken, we still execute the nop instruction and then continue to the following load instruction at location main+0x44 . The branch on integer condition codes instructions do not generate traps. Branch on FPU condition codes instructionsThe fcc field of the floating-point status register, %fsr , is updated by the floating-point compare instructions. The branch on floating-point condition codes instructions test the fcc field and branches accordingly . As a reminder, here are the fcc codes.
If a floating-point unit exists, the following branch instructions can be executed: Table B-13. Branch instructions, FPU
These commands can generate fp disabled and fp exception traps, neither of which should cause panics. When one of these traps does occur, the operating system, not the hardware, is responsible for performing the floating-point operation. Branch on coprocessor condition codes instructionsWhen this book was being written, there were no SPARC processor implementations that had yet incorporated a coprocessor. Even so, The SPARC Architecture Manual, Version 8 does define recommended instructions specific to the coprocessor. The branch on coprocessor condition codes instructions assume there are two bits in the coprocessor status register that are used to represent conditions. The possible values for these two bits are: 0, 1, 2, and 3. Here are the branch on coprocessor condition codes instructions. Table B-14. Branch on coprocessor condition codes
Like the floating-point unit branch instructions, these instructions can cause coprocessor disabled and coprocessor exception traps. Both of these traps should not cause system panics and should instead cause the operating system to intervene and perform the required task. The annul bitThe delayed transfer control instructions that we've seen all have an optional annul flag or bit which can be specified in the instruction by appending ,a to the instruction opcode. When set, the annul bit says to execute the delay instruction only if we take the branch. If we don't take the branch, a set annul bit annuls or nullifies the execution of the delay instruction; the delay instruction is not executed. Here is the same snippet of assembly code we used earlier to demonstrate how a branch instruction might be used. This time, the bne instruction has been changed to bne,a. main+0x34: ld [%l4 - 0x8], %l0 main+0x38: subcc %l0, 0x6, %g0 main+0x3c: bne, a main + 0x5c main+0x40: nop main+0x44: ld [%fp - 0x8], %l0 This time, if the value in %l0 is not equal to 6, we will jump to main+0x5c while executing the nop instruction. Conversely, if the value in %l0 is equal to 6, we will skip the delay instruction all together and move on to the load instruction. Usually the delay instruction consists of something that does real work, instead of a nop . Unoptimized compiler output will often contain nop instructions. For the "branch always" and "branch never" instructions, the delay instruction is executed if the annul bit is not set, and it is not executed when the annul bit is set. Unconditional branchesTwo instructions perform unconditional branches. They are: Table B-15. Unconditional branch instructions
The call instruction transfers control to an address relative to the current PC , whereas the jump and link, jmpl , instruction performs a register-indirect control transfer. The call instruction places the current value of the Program Counter into register %o7 , whereas the jmpl instruction allows the programmer to specify in which register to store the PC . The call instruction does not generate any traps. The jmpl instruction generates a "memory address not aligned" trap when address is not word aligned. Trap on integer condition codes instructionsWe talk about traps in detail in another chapter. For now, let's just say that normally a trap is any condition in the hardware that shouldn't have occurred. Generalizing, we could say when an instruction is executing, if something goes wrong, the instruction gets stuck or trapped and can't finish its job. When this happens, the hardware suddenly switches to "Plan B," records the current Program Counter and jumps to a special trap handler. The operating system gets involved and may decide to panic, kill the offending program, or simply provide assistance to the hardware via software routines. The SPARC processor offers the programmer a way to force a trap condition. Using the PSR icc field, a routine can test the condition codes and based on the results, force a trap. When the trap is "taken," the hardware still switches to "Plan B," records the PC , and jumps into the trap handler, specifically to the section set aside for software trap number specified as sw_trap_num . This is the way a user program actually issues a system call ” by trapping into the kernel with a specific code. Here are the trap on integer condition codes instructions. You'll see that they are quite similar to the branch on icc instructions. Table B-16. Trap on integer condition codes instructions
The trap on integer condition codes instructions never execute the delay instruction. These instructions all generate a trap instruction trap. Orderly return from a trapAfter a trap condition is handled by the operating system and everyone is happy again, instruction execution returns to the previously scheduled program that had caused the condition. The instruction, rett or return from a trap, does this control transfer.
Under various conditions, rett can cause any of the following traps. Table B-17. rett traps
rett is a privileged instruction. |