Basil’s Compendium: Go To/Go Sub

Author(s): John (Basil) Wentworth
Publication: SyncWare News
Pub Details: SyncWare News v4 n3 (Volume 4, Issue 3)
Date: Jan-Feb 1987
Pages: 19-20

GO TO / GO SUB

This chapter will introduce the concepts of JUMP and CALL, the machine code analogs of BASIC’s GO TO and GO SUB.

Practitioners of machine code have their own names for everything, it seems. When it comes to branching, they use the term JUMP, rather than GO TO, and CALL instead of GO SUB. Maybe it sounds more dynamic that way.

Shakespeare used the term “Go to!” by itself. (From which speakers of Hebrew will understand why MacBeth said “Lay on, MacDuff!”) In later days, everyone, including computer programmers, has found it advisable to follow the expression with the name of a destination–albeit sometimes one that is physically unreachable. A GO TO or GO SUB expression in BASIC has to be followed by a line number. (Or by an expression that the computer can interpret as a line number. For instance, GO TO A is a perfectly acceptable instruction, if A has been defined as a number that the computer could reasonably interpret as a line number.) In the case of machine code, where there are no line numbers, the JUMP address is given in terms of bytes. The address can be expressed either as

1. The byte number (memory location) to which the program is to JUMP, or

2. The number of bytes (either forward or back) that the program is to JUMP.

In either case, you will want to have the program laid out neatly, so that you can count the bytes. I find it easiest to write my rough drafts of a program on ruled paper, one byte per line, as shown in Figure 11-1. If you want to save paper by arranging the chart in two (or more) columns, go ahead, as long as the paper is wide enough for you to read all the columns without confusion. You’ll recognize this program as our old friend 2+2, expressed in a new way.

16514   62  Y       LD A,2
16515    2  Gr 2
16516   60  W       INC A
16517   60  W       INC A
16518    6  Gr T    LD B,0
18519    0
16520   79  ?       LD C,A
16521  201  TAN     RET
Figure 11-1.  2+2 in Machine Code

Once you get the code into P$ at line 100, and RUN the program, the proofreading stage will give you just such a listing, although without the mnemonics. Then you can insert a JUMP command wherever you want it, followed by the JUMP destination in 256-imal (taking into account the presence of the JUMP instruction bytes, of course). For instance, if you want to skip one of the INC A stages of Figure 11-1, your program could look like Figure 11-2.

I repeat–note that the calculation of your JUMP destination must take into account the three bytes of the JUMP instruction itsef–unless, of course, you’re JUMPing backwards.

A word of caution: when you do choose to JUMP backwards, be careful that you don’t fall into an endless loop. If Fig. 11-2 had said JP 16516 or JP 16517, for instance, the program would have crashed. Do you see why? Follow it through, byte by byte, for a couple of cycles, and you’ll get the idea.

16514   62  Y       LD A,2
16515    2  Gr 2
16516   60  W       INC A
16517  195  ?       JP 16521
16518  137  ?
16519   64  RND
16520   60  W       INC A
16521    6  Gr T    LD B,0
16522    0
16523   79  ?       LD C,A
16524  201  TAN     RET
Figure 11-2.  Skipping one of the INCs

The example in Fig. 11-2 is absurd, of course. It would have been far easier to delete the extra INC byte than to JUMP over it. But my purpose at this stage is to show you how to do things when you want to. I offer no guarantee that I’ll give you a good reason to want to do them.

A slightly more rational program can be used to demonstrate the CALL instruction. Like its BASIC analog, GO SUB, this instruction is most often used when you hve to repeat an operation several times. Suppose your program had called for 2+2+2+2+2. You could have inserted the INC instruction 8 times. However, you could also do it by putting the +2 operation into a subroutine, as in Fig. 11-3.

16514   62  Y       LD A,2
16515    2  Gr 2
16516  205  LN      CALL 16532
16517  148  Inv =
16518   64  RND
16519  205  LN      "
16520  148  Inv =
16521   64  RND
16522  205  LN      "
16523  148  Inv =
16524   64  RND
16525  205  LN      "
16526  148  Inv =
16527   64  RND
16528    6  GrT     LD B,0
16529    0
16530   79  ?       LD C,A
16531  201  TAN     RET (to
                    BASIC)
16532   60  W       INC A
16533   60  W       INC A
16534  201  TAN     RET (from
                    subroutine)
Figure 11-3.  The Subroutine

Notice that the RETURN from a subroutine (201 in byte 16534) is the same instruction as the RETURN to BASIC (byte 16531). Notice also that you need both of them. Unless you manage to make one of them do double duty, as in Fig. 11-4.

16514    1  Gr 1    LD BC,2
16515    2  Gr 2
16516    0
16517  205  LN      CALL 16526
16518  142  Inv :
16519   64  RND
16520  205  LN      "
16521  142  Inv :
16522   64  RND
16523  205  LN      "
16524  142  Inv :
16525   64  RND
16526    3  Gr 7    INC BC
16527    3  Gr 7    "
16528  201  TAN     RET
Figure 11-4.  Doubling up the RETURN

The JUMP RELATIVE instruction (JR) works very much like the JUMP, except that it tells the program how far to JUMP, rather than giving it a destination. Think of it as a local resident giving instructions to a visiting motorist. He could say “Turn right on Main Street” (JUMP), or he could tell you to “Go East for five blocks, and then turn right” (JUMP RELATIVE). The JR instruction takes up only two bytes instead of the three required for JP, and it has the advantage that subsequent changes in the program are not so likely to mess things up as they are with JUMP. Moreover, you can more easily relocate the whole routine if you want to, if you’ve used JR’s.

But don’t get overconfident. Any time you make ANY change in the program, check it over to make sure that JUMP destinations and JR distances are still good. Just as you would check all GOTO and GOSUB addresses if you change any line numbers in BASIC.

The JR instruction is 24, followed by one byte giving the DISPLACEMENT, or the distance to jump. The jump can be either forward (positive) or back (negative). For negative numbers, we use the convention mentioned earlier: 255=-1, 254=-2, etc. Obviously, the positive and negative numbers are going to meet at 128, so don’t plan to jump more than 127 bytes in a single hop. If you ever do have to make a JR more than 127 bytes long, you can do it in 2 or more hops, jumping first 100 bytes, for example, landing you on an instruction to JR an additional 50. (Sounds a little like a game of Monopoly.)

One instruction to avoid at all costs is JR 254 (=JR-2). Do you see why? (HINT: remember that the displacement is measured from the end of the two-byte instruction.)

Using JR in the inane program cited above, Figure 11-2 would be rewritten to look like Figure 11-5.

16514   62   LD A,2
16515    2
16516   60   INC A
16517   24   JR,1
16518    1
16519   60   INC A
16520    6   LD B,0
16521    0
16522   79   LD C,A
16523  201   RET
Figure 11-5.  Substituting JR in Fig. 11-2

Unfortunately, there is no instruction for CALL RELATIVE. (Except for the telephone, of course.) I have encountered situations where I would have liked to have such an instruction available.

The complete list of JP and JR instructions will be tabulated in the next chapter.

That’s all for now. There will be more.