Positron Compiler Documentation

Branch

Source: Positron16 Compiler User Manual, PDF page 303

Syntax

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

Overview

Cause the program to jump to different locations based on a variable index, where the destination is within 32768 bytes from the source.

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 65536 labels may be placed between the square brackets.

Example

  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 32
  Dim Index as Byte
  CLKDIV = 0
                                     ' CPU peripheral clock ratio set to 1:1
  Write_OSCCONH(%00010000)
                                     ' Enable PLL
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
'
' Config fuses for internal oscillator with PLL
'
  Config Config1 = JTAGEN_OFF, GCP_OFF, GWRP_OFF, BKBUG_OFF, COE_OFF,_
                   ICS_PGx1, FWDTEN_OFF, WINDIS_OFF, FWPSA_PR128, WDTPOST_PS256
  Config Config2 = IOL1WAY_OFF, COE_OFF, IESO_OFF, FNOSC_FRCPLL,_
                   FCKSM_CSDCMD, OSCIOFNC_ON, POSCMOD_NONE

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 less code memory. If larger devices are used and you suspect that the branch label will be over the 32768 boundary, use the BranchL command instead.