This post describes BRA and GOTO Instructions for PIC Microcontroller with examples. BRA
and GOTO
instructions are unconditional branch / jump instructions that unconditionally jump to the given label.
Note: For this tutorial I am using MPLAB X v5.0 but you can use any version upto v5.35. Version 5.35 is last version that included MPASM i.e. the assembler we require to compile the assembly code used in this tutorial. Code given here may not work for later versions.
BRA: Unconditional short jump to given label
Syntax: BRA label
Length of the Instruction: 2 bytes
Instruction Cycles: 2 cycles are required since the previously fetched instruction is discarded and new instruction from target address must be fetched before execution.
Instruction Encoding: 1101 0nnn nnnn nnnn
Updated Flags: None
Target Address: The target label
is represented by an 11-bit relative address n
in the opcode. Target address, i.e. the new value of PC where the code jumps, is calculated by adding 2n
to the current value of program counter PC
Target Address = New PC
= Current PC
+ 2n
Description: BRA instruction unconditionally branches / jumps to the target address / label.
BRA instruction is a short jump
BRA instruction is a short jump and the target address, i.e. the new value of PC must be within 2048 bytes of the current value of the program counter (PC).
The second byte n
of the instruction is a relative address. Relative address can be negative or positive. Positive address means that the code jumps forward i.e. after current PC. If the address is negative then the code jumps backwards i.e. before current PC. Target address can be calculated using
Target Address = New PC
= Current PC
+ 2n
The relative address n
is represented by 11-bits. The largest -ve relative address can be -1024, and the largest +ve relative address can be +1023. As a result, the target address can be a maximum of 2048 bytes behind the current program counter, and a maximum of 2044 bytes forward from the current program counter.
Note: PC points to the next instruction (after the conditional branch) while calculating the target address. PC is always ahead since it is used to fetch the next instruction.
BRA Instruction Example:
Consider the case where we want to increment a file register continuously and send a value to PORTC
. If the number is positive then PORTC
is equal to the file register. But if the number is negative then PORTC
is equal to the complement of the file register. We can use the following code.
R1 EQU 0x01
R2 EQU 0x02
MOVLW 0x0 ; Copy 0x00 to WREG
MOVWF R1 ; Clear R1
CLRF TRISC ; Make PORTC an output
LOOP INCF R1, F ; Increment R1
BN NEG ; Branch to label NEG if N = 1
MOVFF R1, PORTC ; Copy R1 to PORTC if R1 is +ve
BRA LOOP ; Short jump to LOOP label
NEG MOVFF R1, R2 ; Copy R1 to R2 if R1 is -ve
COMF R2, F ; Complement R2
MOVFF R2, PORTC ; Copy R2 (Complemented R1) to PORTC
BRA LOOP ; Short jump to LOOP label
In each iteration, R1
is incremented and then BN
instruction is used to check whether N = 1
or not. If N = 1
, code jumps to NEG
label where R1
is copied to R2
, R2
is complemented and copied to PORTC
. If N = 0
, R1
copied to PORTC
. In both cases BRA
instruction is used to jump back to LOOP
label for next iteration.
BRA
instruction is suitable here since the LOOP
label is within 2048 bytes of the PC
. GOTO
instruction can also be used here since it can jump any where in PIC code ROM space. However, BRA
should be preferred as it requires 2 bytes instead of 4 bytes in ROM.
GOTO: Unconditional long jump to given label
Syntax: GOTO label
Length of the Instruction: 4 bytes
Instruction Cycles: 2 cycles are required since the previously fetched instruction is discarded and new instruction from target address must be fetched before execution.
Instruction Encoding: 1110 1111 kkkk kkkk 1111 kkkk kkkk kkkk
Updated Flags: None
Target Address: The target label
is represented by a 20-bit absolute address k
in the opcode. Target address, i.e. the new value of PC where the code jumps, is 2k
.
Target Address = New PC
= 2k
Description: GOTO instruction unconditionally branches / jumps to the target address / label.
GOTO instruction is a long jump
GOTO instruction is a long jump that can jump to any memory address of the 2M code space of PIC18 microcontroller. Target address i.e. the new 21-bit value of PC where the code jumps, is 2k
.
Note: PC points to the next instruction (after the conditional branch) while calculating the target address. PC is always ahead since it is used to fetch the next instruction.
GOTO Instruction Example:
Consider the following code
R1 EQU 0x01 ; loop counter
SETF TRISB ; Make PORTB an input
CLRF TRISC ; Make PORTC an output
MOVF PORTB, W ; Copy PORTB to WREG
ADDLW 0x80 ; ADD 0x80 to WREG
BOV OVERFLOW ; Branch to OVERFLOW label if OV = 1
GOTO EXIT ; Goto EXIT label
OVERFLOW
SETF PORTC ; Set all bits of PORTC
ORG 0A00H
EXIT
MOVLW 0xA0 ; Load 0xA0 to WREG
MOVWF PORTC ; Copy WREG (0xA0) to PORTC
PORTB
is copied to WREG
and then 0x80
is added. If overflow occurs then code jumps to OVERFLOW
label and all bits of PORTC
are set. If overflow doesn’t occur then the code jumps to EXIT
label and 0xA0
is sent to PORTC
.
However, using ORG
directive, the code at EXIT
label is placed at ROM address 0xA00
. It means that EXIT
label is more than 2048 bytes away from the previous PC
. BRA
instruction cannot be used here, so GOTO
instruction must be used. If you try to use BRA
instruction then MPLAB will fail to assemble the code and it will throw an error “Symbol ‘EXIT
‘ out of range of relative branch instruction”.
Conclusion
BRA and GOTO instructions for PIC microcontroller are unconditional branch / jump instructions, that jump to a target label. GOTO
instruction must be used when the label is more than 2048 bytes away from the previous PC
. When the label is within 2048 bytes of the previous PC
, we can use either. However, BRA
should be preferred as it requires 2 bytes instead of 4 bytes in ROM.