Introducing the Instruction Set Part 3
This segment of the tutorial introduces branches, particularly conditional branches and function calls. This is Part 4 of a series. If you haven't yes, you may wish to review at least Part 1 and Part 2.
Contents
Unconditional Branches and Jumps
Unconditional branches are branches that are always taken. Jump instructions do essentially the same thing. The following table lists the instructions:
Mnemonic | Description | Cycles | Size |
---|---|---|---|
B | Branch to label | 9 | 2 words |
J | Jump to label | 12 | 3 words |
JD | Jump to label while disabling interrupts | 12 | 3 words |
JE | Jump to label while enabling interrupts | 12 | 3 words |
As you can see, the primary difference between branches and jumps is that branches are smaller and faster. Branches encode their "target address," the address being jumped to, as a relative displacement to the current address. Jumps, on the other hand, store the actual address of the target. In most cases, especially in a 16-bit ROM, there are few reasons to use a J instruction, although the combination instructions, JD and JE can be useful.
There is also a pseudo-instruction, JR, that allows "jumping to a location held in a register." It is really a pseudonym for "MOVR Rx, R7". Because it is a MOVR instruction, it will modify the Sign Flag and Zero Flag, which may be confusing if you're not expecting it.
Conditional Branches
Signed Comparisons
Unsigned Comparisons
Sign/Zero Comparisons
If-Then and If-Then-Else
Looping
Function Calls
Simple Call/Return
Nested Call/Return
Passing Arguments via Return Address
Indirect Branches and Jump Tables
"It was a 'Jump to Conclusions' mat. You see, it would be this mat that you would put on the floor... and would have different conclusions written on it that you could jump to." -- Tom Smykowski, Office Space
Indirect Branching: "Jump Vectors"
Simple Jump Tables
Adding to the Program Counter
Moving On
At this point, you may wish to move along to the last part, or review earlier parts of this tutorial:
- Introducing the Instruction Set Part 1: The CPU, Memory and Registers; Primary Instructions and Addressing Modes
- Introducing the Instruction Set Part 2: Single Register and Implied Operand Instructions
- Introducing the Instruction Set Part 4: Shift and Rotate Instructions
Or, you can return to the Programming Tutorials index.