Positron Compiler Documentation

Branch

Source: Positron8 Compiler User Manual, PDF page 296

Syntax

Branch Index, [Label1 {,...Labeln }]

Overview

Cause the program to jump to different locations based on a variable index. On a PICmicro™ device with only one page of memory.

Parameters

Index is a constant, variable, or expression, that specifies the address to branch to. Label1,...Labeln are valid labels that specify where to branch to. A maximum of 255 labels may be placed between the square brackets, 256 if using an 18F device.

Example

  Device = 16F1829
  Dim Index as Byte
Start:
  Index = 2
                      ' Assign Index a value of 2
  Branch Index,[Lab_0, Lab_1, Lab_2] ' Jump to Lab_2 because Index = 2
Lab_0:
  Index = 2
                      ' Index now equals 2
  GoTo Start
Lab_1:
  Index = 0
                      ' Index now equals 0
  GoTo Start
Lab_2:
  Index = 1
                      ' Index now equals 1
  GoTo Start

The above example we first assign the index variable a value of 2, then we define our labels. Since the first position is considered 0 and the variable index equals 2 the Branch command will cause the program to jump to the third label in the brackets [Lab_2].

Notes

Branch operates the same as On x GoTo. It's useful when you want to organise a structure such as: -

If Var1 = 0 Then GoTo Lab_0  ' Var1 =0: go to label "Lab_0"
If Var1 = 1 Then GoTo Lab_1  ' Var1 =1: go to label "Lab_1"
If Var1 = 2 Then GoTo Lab_2  ' Var1 =2: go to label "Lab_2"

You can use Branch to organise this into a single statement: -

Branch Var1, [Lab_0, Lab_1, Lab_2]

This works exactly the same as the above If...Then example. If the value is not in range (in this case if Var1 is greater than 2), Branch does nothing. The program continues with the next instruction..

The Branch command is primarily for use with devices that have one page of memory (0- 2047). If larger devices are used and you suspect that the branch label will be over a page boundary, use the BranchL command instead.