Multiple choice transfers of control are one of the most time-honored
facilities in programming languages, be they conventional or scripting,
interpreted or compiled, assembler or very high level. It is often
mistakenly believed that indirection is needed for implementation,
but this is not the case. Perfectly serviceable multi-way branches
can be implemented without indirection. All that is necessary is
some method of re-computing the location of the next instruction
(or line in a scripting language) to be executed.
The syntax of multi-way transfers of control depend on the individual syntax of
the underlying programming language, and may take several forms, including:
- Computed GOTO (FORTRAN)
- switch/case (C/C++/JAVA)
- GOTO DEPENDING ON (COBOL)
- ON GOTO (BASIC)
- Branch/Jump Tables (assembler)
Beyond the most basic ability to compute the next statement to be executed,
the most important requirement in implementing a multi-way branch is
ensuring that the branch value is within the appropriate range. Indeed,
the VAX
architecture contains the CASE machine instruction to
implement bounds checking and transfer of control via an inline table of offsets.
At its most basic, a multi-way transfer of control depends on an integer index.
This is the most basic form, and it is shared by most assembler and third
generation languages. C/C++/JAVA enums are integers, for most intents
and purposes. DCL, PERL, EnScript® and other interpreted scripting languages
often extend this concept to strings. This extension transforms the range
check from an integer bounds check to a check based on the presence of
an index string value within an enumeration of potential values. This
check can be most simply implemented by using a string index function.
In DCL the string index operation is provided by the F$LOCATE lexical function.
However, some caution is needed when using the F$LOCATE function, or for
that matter, any index function to locate a substring within the context of a
larger string. Consider an index string that can contain the values
“THEE”, and “EEM”. Admittedly, this selection of values
has more than a small potential for confusion, but it is a useful example.
Simply checking that the index string is contained within a list of possible strings is
insufficient (e.g., the string “THE” is not a valid index, yet is within the valid
index string “THEE”). The solution is to ensure that the string containing
the valid values starts and ends with the delimiter character. This technique was
discussed in a previous column entitled:
“Pitfalls
of F$LOCATE and other functions”.
Using the extra delimiters, the validation code is simplicity itself, to wit
(INDEX_VALUE contains the index string):
$ VALID_INDICES = “/ONE/TWO/THREE/FOUR/”
$ IF F$LOCATE(“/”+indexvalue+”/”, VALID_INDICES) -
.EQ. F$LENGTH(VALID_INDICES) THEN {error processing}
|
Once the string index has been validated, the actual transfer of control consists of
combining a simple GOTO statement with symbolic substitution, to wit:
$ GOTO TARGET_’INDEXVALUE’
$ TARGET_ONE:
$ …
$ GOTO EXITTARGET
$ TARGET_TWO:
$ …
$ GOTO EXITTARGET
$ TARGET_THREE:
$ …
$ GOTO EXITTARGET
$ TARGET_THREE:
$ …
$ GOTO EXITTARGET
$ TARGET_FOUR:
$ …
$ EXITTARGET:
$ …
|
This approach is far safer than attempting to nest IF-THEN-ELSE conditionals.
The option is processed directly. If an invalid index is presented, it is detected
immediately and dealt with. Adding a new possibility is simply a
matter of adding the new index possibility to VALID_STRING
and adding the appropriate processing code.
A segment of DCL implementing and demonstrating the above technique can be found on my www site
under "DCL GOTO" under the Demonstrations heading.
Reader questions about using DCL, or other questions relating to OpenVMS should
be addressed to the OpenVMSConsultant (at) OpenVMS.org. While I cannot promise to
answer all messages, I will try to respond as time allows.
The OpenVMS Consultant welcomes questions from readers about OpenVMS and related technologies.
Please submit your questions to the OpenVMS Consultant.
Biography:
Robert Gezelter, CDP, CSA, CSE, Software Consultant,
has more than 30 years of international
consulting experience in both the private and public sectors.
He is a regular guest speaker at technical conferences worldwide including the HP Technology Forum.
In 2004, the IEEE Computer Society appointed Mr. Gezelter to its Distinguished Visitors Program, which
provides meeting speakers for IEEE chapters throughout North America. He is also a published numerous
technical papers and book chapters, including two chapters in the recently released
Computer Security Handbook, 5th Edition,
Recently, he started
Ruminations
- An IT Blog
on IT topics not strictly related to OpenVMS.
His firm's practice emphasizes in-depth technical expertise in computer architectures,
operating systems, networks, security, APIs, and related matters. Mr. Gezelter has
worked with OpenVMS since the initial release of VAX/VMS in 1977.
His clients include small businesses to the Fortune 10, locally, nationally, and
internationally on matters spanning the range from individual telephone
questions to major projects.
He can be reached via his firm's web site at
http://www.rlgsc.com.