This is the fourth part in this series, and will cover flow control instructions.
You can find the first post here, the second here, and the third here.
Prerequisites
None of the flow control operations use the data stack, and only two make use of the return address stack, so knowing about stacks is less important in this post than in the others. Knowledge of other assembly language variants would definitely be an asset, as these follow the same patterns found in other processors.
Last Time
The last post covered arithmetic and logic unit (ALU) instructions, which modify the condition code (CC) register. Many of the flow control instructions make use of the CC flags to determine whether or not to move execution flow elsewhere.
Jumps
Jump (jmp, jsr, rts) instructions change the program counter (PC) to an absolute address in memory. They are all unconditional, that is, the change in the PC happens regardless of the system's state.
jmp changes the PC to an address specified as immediate operand to the instruction without side effects.
jsr changes the PC, but pushes the address of the next instruction after jsr to the return address stack. This is paired with rts which pulls the PC from the top of the return address stack. These allow for subroutine calls and returns and allow for more structured programming than jmp or any of the branching instructions.
Branches
Branch instructions use a single byte, twos-complement signed address offset as their operand. This allows for faster changes to the PC, as a single byte can be loaded faster by the CPU. Additionally, except for the unconditional b branch, each branch instruction will check against some CC flag or flags.
Conditional Branches
- beq - branch on equal
- branches when the zero flag on the CC is set
- blt - branch on less-than
- branches when the negative flag on the CC is set
- ble - branch on less-than-or-equal
- branches when either the zero or negative CC flags are set
- bcr - branch on carry
- branches when the carry flag on the CC is set
- bov - branch on overflow
- branches when the overflow flag on the CC is set
- bdiv0 - branch on divde-by-zero
- branches when the divide-by-zero flag on the CC is set
At the moment there is no branch-on-underflow instruction, or any greater-than branches. There's space to put them, but I haven't gotten around to it yet.
The End?
That concludes the instruction set portion of these deep dives. Next up is the assembler itself. Included will be the well loved Hello World! example, among others. Full knowledge of all the system calls is not necessary, each one used will be explained as it's used.
Comments