Basil’s Compendium

Author(s): Basil Wentworth
Publication: SyncWare News
Pub Details: SyncWare News v4 n4 (Volume 4, Issue 4)
Date: March-April 1987
Pages: 22-23

This chapter will tell you about conditional instructions, and will discuss the behavior of various flags.

IF… THEN

If I was asked to name the single most powerful attribute of a computer, I think that I would say it’s the ability to make decisions–to vary performance with varying conditions. Conditional instructions in machine code can take the following forms, with the analogies of BASIC given in parentheses: 1. IF…THEN JUMP; or IF…THEN JUMP RELATIVE (GOTO) 2. IF…THEN CALL (GOSUB) 3. IF…THEN RETURN (RETURN) The respective instructions for these operations are listed in Table 12-1. The “IFness” of the situation is measured by the conditions of various flags. You will remember that the flags reflect the results of the most recent operations of the program. A sample situation calling for a conditional JUMP would be as follows: what is the largest number that is a power of two, but is not greater than 1000? A BASIC solution to this problem is shown in Figure 12-1. If you can’t visualize how this program works, then put it into the computer and RUN it. (Adjust the line numbers so it won’t interfere with any program material you already have there, and be sure to delete those lines when you’ve finished, to avoid possible trouble later on.)

Figure 12-1. The solution in BASIC

1010 LET N=2
1020 LET N=2*N
1030 IF 2*N>=1000 THEN GOTO 1050
1040 GOTO 1020
1050 PRINT N

Figure 12-2 shows how to do the same thing in machine code. Notice that the RETURN (201) is not needed, PROVIDED: 1. that some other form of RETURN instruction, such as RET NC, is included, AND 2. that the conditions for RETURN will indeed be met during the running of the program. WATCH OUT there! Both conditions must be met. It’s oh so easy to go to great pains to meet condition 1, and then forget to make sure that condition 2 is met.

Figure 12-2. The machine code approach

16514  33 5      LD HL,2
16515   2 Gr 2
16516   0
16517  17 )
16518 232 CONT
16519   3 Gr 7
16520  68 ?      LD B,H
16521  77 ?      LD C,L
16522  41 D      ADD HL,HL
16523 237 GOSUB  SBC HL,DE
16524  82 ?
16525 208 SQR    RET NC
16526  25 ;      ADD HL,DE
16527  24 /      JR,-9 **
16528 247 RUN

**NOTE that JP 16520 could have
been used instead of JR,-9

It may seem a little bizarre to subtract DE from HL and then add it right back again, but that’s what line 1030 of the BASIC program did in Figure 12-1. However, you can avoid this extra step by using the instruction COMPARE (mnemonic: CP).

Compare

CP makes the comparison without actually changing the contents of the register being compared. This is a very useful instruction, and it can save you a lot of grief in some situations, but it is subject to two limitations: 1. CP will compare a specified quantity with the contents of Register A only; and 2. therefore, you can compare only a single-byte quantity (0-255) in a single step. Of course, you can compare a two-register variable in two successive steps, if you’re willing to give it a little thought. See if you can figure out how to do it after you read the following few paragraphs. If you’re still stumped, the ANSWER section at the end of this chapter will help you. To watch CP at work, let’s set up a very simple multiplication problem. Again in BASIC, use the program shown in Figure 12-3. Do you see how this program multiplies 2*3? (Again doing it the hard way, but never mind that for a moment.) Now, how would you do the same thing in machine code?

Figure 12-3. 2*3 in BASIC

1010 LET N=2
1020 LET A=1
1030 LET N=N+2
1040 LET A=A+1
1050 IF A=3 THEN GOTO 1070
1060 GOTO 1030
1070 PRINT N

Figure 12-4 gives a possible set of mnemonics for the solution. Try working out the code for yourself, and compare your solution with that shown in Figure 12-5. Your solution may be somewhat different. There’s room for individual style in machine code, just as there is in BASIC. You may even hit on an entirely different approach. If it works, it’s good. If it is shorter than mine, or runs faster, or is easier to understand, then it’s more elegant.

Figure 12-4. The mnemonics alone

 LD A,3
 LD H,1
*ADD BC,2
 INC H
 CP H
 JR NZ,...or JP NZ...
 RET

*There is no instruction for
ADD BC,2. Try INC BC two times.

Figure 12-5. The machine code for Fig. 12-4

16514  62 Y      LD A,3
16515   3 Gr 7
16516  38 A      LD H,1
16517   1 Gr 1
16518   1 Gr 1   LD BC,2
16519   2 Gr 2
16520   0 space
16521   3 Gr 7   INC BC
16522   3 Gr 7   INC BC
16523  36 8      INC H
16524 188 Inv W  CP H
16525  32 4      JP NZ,-6
16526 250 IF
16527 201 TAN    RET

Always try for those three objectives: economy in use of memory, speed of operation, and clarity. The first two usually go hand-in-hand. Usually, but not always. If the three objectives conflict with one another, you’ll have to make a judgement as to which is the most important in the application you have in mind.

Flags

Figure 12-6 gives a little program that will help you to find out what conditions SET and RESET the various flags (i.e., make them equal to 1 or 0, with 1 meaning SET). For the moment, you’ll be concerned with three flags: S (=1 if the last operation resulted in a positive number, =0 if the result was negative); Z (=1 if that result was zero, otherwise =0), and C (=1 if the operation resulted in a carry). The S, Z, C information is carried in the F register, in the following manner:

S: in the 128 byte
Z: in the 64 byte
C: in the 2 byte

The program itself lies in lines 1-95. The rest of the lines can be omitted, if you use the EL MINIMO system or any other means of writing your machine code into line 1. Lines 510-530, by the way, serve the purpose of the FLASH instruction that is found on some other computers. It’s a spectacular way of attracting attention. The five bytes already shown in line 1 (represented by PRINT, AT, Gr T, space, and TAN) stand for

PUSH AF (245)
POP BC (193)
LD B, 0 (6,0)
RET (201)

These five bytes make USR 16514 calculate the value held in Register F. The spaces in front of this sequence are nulls, to be filled in with whatever operations you want to test. Zeroes are used for these nulls so that they won’t interfere with the operation of the program if one or more of them is inadvertently left in.

Table 12-1. The conditional instructions

Condition:  ALL   Z  NZ   C  NC   P   M

  JP,NN*    195 202 194 218 210 242 250
  JP(HL)    233
  JR,DIS     24  40  32  56  48
  CALL,NN*  205 204 196 220 212 244 252
  RET       201 200 192 216 208 240 248

*NOTE: NN refers to a two-byte piece of data, LSB first

FIGURE 12-6. THE FLAGS

   1 REM Y COPY W   PRINT AT ▒ TAN
5 FAST
10 REM [illegible]
15 PRINT
20 LET N=USR 16514
30 LET S=INT (N/128)
40 IF NOT S THEN PRINT "- "
50 IF S THEN PRINT "+ "
60 LET N=N-128*S
70 LET Z=INT (N/64)
80 IF Z THEN PRINT "Z ";
90 IF 2*INT (N/2)<>N THEN PRINT "C"
95 GOTO 9000
100 REM [illegible]
105 FAST
110 CLS
120 PRINT ,,"BE SURE THERE ARE ENOUGH NULLS FOR YOUR MACHINE CODE"
130 PRINT
140 LET A=16514
150 PRINT A;" ";
160 IF PEEK A=118 THEN GOTO 500
170 INPUT B
175 IF B>255 THEN GOTO 9000
180 POKE A,B
190 PRINT PEEK A
200 LET A=A+1
210 GOTO 150
500 SLOW
510 PRINT AT 10,10;"NOT ENOUGH NULLS"
520 PRINT AT 10,10;"[illegible]"
530 GOTO 510
8999 STOP
9000 REM [illegible]
9020 PRINT ,,,,
9030 LET F=16514
9040 PRINT F;" ";PEEK F
9050 LET F=F+1
9060 IF PEEK F=118 THEN STOP
9070 GOTO 9040

Answer Section

If you weren’t able to work out the answer to the problem posed above (use CP to find whether X is greater than 1000), try this: 1. Let’s assume that X is stored in register BC, so that X=C+256*B. 2. IS B greater than 3? If so, then X is greater than 1023. 3. Is B less than 3? If so, then X is less than 768. 4. If B=3, then is C greater than 232? If so, then X is greater than 1000. That’s all for now. There will be more.