6.
PROGRAMMING
TECHNIQUES
This chapter describes some techniques that may be of help to the programmer.
BRANCH TABLES PSEUDO-SUBROUTINE
S:.Jppose
a program consists
of
several separate routines, any of which may
be
executed depending upon some
initial condition (such
as
a number passed
in
a register). One
way
to code this would
be
to check each condition
sequentially and branch to the routines accordingly
as
follows:
CONDITION =
CON
DillON
1?
IF
YES
BRANCH
TO
ROUTINE 1
CONDITION::: CONDITION
2?
IF
YES
BRANCH
TO ROUTINE 2
BRANCH TO ROUTINE N
A sequence
as
above
is
inefficient, and can
be
improved
by
using a branch table.
The logic at the beginning of the branch table program loads the starting address
of
the branch table into the H
and L registers. The branch table itself consists of a list
of
starting addresses for the routines to
be
branched to.
Using
the
Hand
L registers
as
a pointer, the branch table program loads the selected routine's starting address
into the program counter, thus effecting a jump to the desired routine. For example, consider a program that
executes one of eight routines depending on which bit of the accumulator
is
set:
Jump to routine 1
if
the accumulator holds 00000001
2"
" " 0000001 0
3 "
" "
00000100
4 " "
"
00001000
5 "
" "
00010000
6 "
" "
00100000
7 "
" "
01000000
8 " "
"
10000000
A program that provides such logic follows. The program
is
termed a 'pseudo-subroutine' because it
is
treated
as
a
subroutine
by
the programmer (i.e.,
it
appears just once
in
memory), but
is
entered
via
a regular
JUMP
instruction
rather than
via
a CALL instruction.
6-1