The Assembly 101 series touches upon the various isntructions that the NES’ 6502 assembler uses. These instructions make up the program language that is Assembly. In this post, we take a look at the various types of branches that exist within Assembly.
What is a branch?
Whenever you’re writing code for your game, you will need to do things based on conditions. For example, if your player takes a hit from an enemy, you’ll want to check whether your player has enough health to take the hit. If the player’s health is enough, he can keep on playing. If not, the player will lose a life. This is where you will use a branch in your code:
Player health is zero? Branch out to taking a life If not, continue playing
Branch if (not) equal
In assembly, you can check whether the accumulator is zero or not. There are two branch instructions for this:
BEQ – Branch if equal. If the value of the accumulator is zero, this will branch out.
LDA player_health ;; load the player's health into the accumulator BEQ +isZero ;; Branch out to +isZero if the accumulator equals zero .... ;; Code here will be executed if the accumulator is NOT zero +isZero: ;; This is where the above code will branch out
BNE – Branch if not equal. If the value of the accumulator is NOT zero, this will branch out.
LDA player_health ;; load the player's health into the accumulator BNE +notZero ;; Branch out to +notZero if the accumulator is not zero .... ;; Code here will be executed if the accumulator IS zero +notZero: ;; This is where the above code will branch out
What is a compare?
You don’t always want to check against zero. For instance, if you want your game to open a certain door after collecting three keys, you’ll want to compare against a value of three instead of zero. This is what you can use the CMP (Compare) instruction for. Here’s an example:
LDA collected_keys ;; Load the number of keys collected into the accumulator CMP #$03 ;; Compare the value against three BEQ +isThree ;; Branch out if the value is three .... ;; Code here will be executed if the value is NOT three +isThree: ;; This is where the above code will branch out
Other branch instructions
Besides BEQ and BNE, there are a few more branch instructions. These instructions check whether the value of the accumulator is larger, or smaller, than the value it’s compared with.
BCC – Branch on carry clear. This will branch out the code when the carry is zero, which means that the accumulator is smaller than the compared value.
LDA score ;; Load the player score into the accumulator CMP #50 ;; Check the accumulator against #50 BCC +lessThanFifty ;; Branch out if the accumulator is less than #50 .... ;; Code here will be executed if the accumulator is equal to, or greater than, #50 +lessThanFifty: ;; This is where the above code will branch out
BCS – Branch on carry set. This does the exact opposite of BCC. If the accumulator is equal to or greater than its compared value, it will branch out.
LDA score ;; Load the player score into the accumulator CMP #50 ;; Check the accumulator against #50 BCS +isFiftyOrMore ;; Branch out if the accumulator is equal to, or greater than #50 .... ;; Code here will be executed if the accumulator is less than #50 +isFiftyOrMore: ;; This is where the above code will branch out
Finally, there are two more branch instructions: BPL (Branch on plus) and BMI (Branch on minus). These do the same as BCS and BCC, but for signed integers instead of unsigned integers.