Program flow control commands
are important in every programming language since they enable the programmer to make a decision and change the flow
of the program based on this decision. PicBasic language supports the following program flow
control commands:
BRANCH BUTTON
CALL FOR…NEXT
GOSUB…RETURN GOTO
IF…THEN
We
shall now see what the functions of these commands are and how to use them in programs.
BRANCH
BRANCH offset, (Label0,
Label1,…)
When
this command is executed,
the program will jump to the program label based
on the value of offset. Offset is actually
a program value and if offset is zero, the program
jumps to the first label, if offset is one, the program
jumps to the second label, and so on.
Example:
BRANCH B2, (Lbl1, Lbl2,
Lbl3) ‘ If B2 0 then goto Lbl1
‘ If B2 1 then goto Lbl2
‘ If B2 2 then goto Lbl3
BUTTON
BUTTON Pin, Down, Delay, Rate, Var, Action, Label
This command is used to check the status of a switch. The command operates
in a loop and con- tinuously samples the pin, debouncing it and comparing the number of
iteration performed with the switch closed. The parameters are
Pin Pin number (0 to 7). PORTB pins only
Down State of pin when button is pressed (0 or 1)
Delay Delay before auto-repeat begins (0 to 255). If 0, no debounce or
auto-repeat is performed. If 255, only debounce, but no auto-repeat is performed
Rate Auto-repeat rate (0 to 255)
Var Byte variable
used for delay/repeat countdown. Should be initialised to 0 before use
Action State of pin to perform goto (0 if not pressed, 1 if pressed)
Label Program
execution
continues at this label if Action is true
Figure
4.1 shows the two types of switches that can be used with this command.
For example,
the following command checks for a switch pressed on pin 2 (of PORTB) and jumps
to Loop if it is not pressed (this command assumes that the port pin will be logic 0 when the switch is pressed, i.e. the figure on
the left in Figure 4.1):
BUTTON 2, 0, 255, 0, B0, 0, Loop
Switches that can be used for the Button command
The following command checks for a switch pressed on pin 2 as above, but jumps to Loop if the
switch is pressed:
BUTTON
2, 0, 255, 0, B0, 1, Loop
CALL
CALL Label
This command executes the assembly
language subroutine named Label.
For example,
the com- mand calls to assembly language routine with the name calculate.
CALL calculate
FOR…NEXT
FOR index Start TO End (STEP (
) Inc)
(body)
NEXT index
This
command is used to perform iterations in a program. Index is
a program variable which holds
the initial value of the iteration
count Start. End is the final
value of the iteration count. STEP
is the value by which the index is incremented at each iteration. If no STEP
is specified, the index is incremented by 1. The iteration
repeats until index End
and then execution
continues with the next
instruction following the NEXT. Index can be
a byte (0 to 255), or a word (0 to 65535).
In the following example, the two statements enclosed within the FOR…NEXT
are executed 10 times.
FOR B0
1 TO 10
B1 B1
1
B2 B2
1
NEXT B0
or
in the following example, the index is incremented by 2 in each iteration.
FOR B0 1 TO 100 STEP 2
B1 B0
2
NEXT B0
GOSUB…RETURN
GOSUB Label
This program calls a subroutine starting at Label. It is like a GOTO command, but here the program returns when the
RETURN statement is reached,
and continues with the instruction
after the GOSUB. The RETURN statement has no parameters. A subroutine has the following characteristics:
● A
label to identify the starting point
of the subroutine
● Body
of the subroutine where the required
operation is performed
● RETURN
statement to exit the subroutine and
return to the main calling program
Subroutines can be nested
in PicBasic where a subroutine can call to other subroutines. The nest- ing should
be restricted to no more than four levels deep. In the following example,
the subrou- tine labelled INC increments variable B1 by one and then returns to the main program. On return
to the main program, the statement B2 B1 is executed.
B0 0
B1 1
GOSUB INC ‘
Jump to subroutine INC B2 B1 ‘ Subroutine returns here
………
………
INC: ‘
Start of the subroutine B1 B1
1
‘ Body of the subroutine RETURN ‘ End of the subroutine
GOTO
GOTO Label
This command causes the program execution to jump to the statement
beginning at Label.
For example,
GOTO Loop
………
………
………
Loop:
IF…THEN
IF Comp (AND
/ OR Comp) THEN Label
This statement is used to perform comparisons (Comp) in a program. If the result of the compari- son is true then the program jumps to the statement at Label,
otherwise execution resumes with the statement following IF…THEN.
A comparison can relate to a variable, to a constant, or to other variables.
All comparisons are unsigned and the following comparison operators can be used:
less
than
less
than or equal
equal
not
equal
greater than or equal
greater than
Additionally, logical operators AND and OR can be used in a comparison operation. For example, IF B0 10 THEN
CALC ‘ Jump to CALC if B0 10
…………………..
..............................
CALC:
Another example
is given below. In this example,
if B2 is greater than 40 and at the same time B3 is less than 20 then the program
jumps to the statement at label EXT.
Otherwise, execution con- tinues with the statement after the IF…THEN.
IF B2 40 AND B3
20 THEN
EXT
………..........
……….......... EXT:
It is important to be careful that only a Label
can be used after the THEN
statement.

Comments
Post a Comment