SHOWING THE FLAG By Jacques Groslouis 1747 Riverbank Drive, Bathurst New Brunswick E2A 4L1 Canada Email jgroslou@nbnet.nb.ca This is not an article about the correct protocol to follow when displaying the Maple Leaf or the Stars and Stripes but offers some tips on using flags in Extended Basic programs. As used in this article, a flag is a variable to which a numeric value has been assigned. For example. 'A' in the expression A=7 can be considered a flag because you can write an Extended Basic statement such as IF A=7 THEN "do something" and thus change the program flow. This is a simple sort of flag and this article will deal with the more complex variety where you can set a number of values to the same numeric variable. This will use the Extended Basic logical expressions described on pages 42 to 44 of the Extended Basic manual. Flags can be very useful when writing programs in which you want to test for the existence of a certain condition and to take a different course of action. You then SET a flag to identify the condition you wish to TEST using IF THEN statements. Having set a flag you might want to CLEAR the flag when you no longer wish to use it. How to SET, TEST and CLEAR flags is explained below. How to SET a flag. A flag is set by using the following line in your Extended Basic program: IF 'condition to be tested' THEN A = A OR 'value' The value in this line should be 1, 2, 4, 8, 16, 32, 64 and 128 thus allowing you to set up to eight flags for each numeric variable such as an 'A'. To use any other values would cause the flag to respond to more than one value for 'A'. For example, setting the flag 'A' to 49 would allow the flag to respond to a test when 'A' is equal to 1, 16, or 32. Although you might not want to initially set a flag to say 49, you will in effect be doing this when you first set a flag to 1 for condition #1, and then set 'A' to 16 for condition #2 and finally set 'A' to 32 for condition #3. In other words the setting of flags is cumulative and you can see this if you do a 'PRINT A' after setting 'A' to these 3 flags, the screen would display 49. Various numeric variables including numeric arrays can be used as flags but be sure that these variables are not used for other purposes in your program, such as counters in FOR NEXT loops. The choice of conditions to be tested will depend on your program. Adventure type programs make use of a lot of flags to determine contents of rooms and to hold a list of items which the adventurer is carrying. The author has a very large program which sets over 30 flags and tests them over a 100 times. This program attempts to convert XB tokens to c99 source code. How to TEST a flag. The existence of the condition to be tested is done by including the following line: IF A AND 'value' THEN 'do something' The value to be used is the same value that was used when the flag was SET (see above). To test for more than one condition the flags can be added together provided the same variable is being used as a flag. For example; IF A AND 49 THEN would take a certain action if the three conditions described in the previous section were present. If different flag variables were used the testing line would have to be somewhat as follows: IF (A AND 1) AND (B AND 16) THEN... Use of brackets will ensure that the various logical expressions are properly tested. You might be tempted to use the following line: IF A=49 THEN... This will only return a true condition if and only if the 3 flags described above have been set. If the flag 'A' had also been set to 2 then 'A' now totals to 51 and the condition would be false although testing the condition by using IF A AND 49 would still be true. Generally do not mix these two methods of testing unless you want to test that only certain flags were set and no others. How to CLEAR a flag. A specific flag can be cleared by using the following statement line: A = A AND 255 - 'value' Continuing our previous example where the flag 'A' has been set 4 times and now contains a cumulative value of 51, the statement A = A AND 239 would clear the flag set by using A = A OR 16. The cumulative value of 'A' would now be 35 being 1 + 2 + 32, the remaining flags. You can clear all the flags that 'A' represents by using A = 0. This can be useful if you want to rerun the program from its beginning without first exiting. Other programming languages contain WHILE and DO WHILE statements which are not present in TI Extended Basic. By the creative use of flags together with a GOTO statement to create a loop, you should be able to accomplish similar results. Other articles on this subject often try to explain the use of flags by getting the reader to AND and OR the bits in an example variable. This articles avoids this approach by providing examples which you can follow when you wish to SET, TEST or CLEAR a flag. The following program demonstrates the examples explained above and itself shows many ways that you can use flags in a program. RETURN 100 ! SAVE DSK3.FLAG-FUN 3400 ACCEPT AT(22,19)VALIDAT 110 B=0 :: CALL CHAR(126,"00 E(DIGIT)SIZE(-3)BEEP:C :: A, 00FF") B,T,TT,C=0 :: GOSUB 4000 :: 1000 CALL CLEAR :: CALL BLUE GOSUB 2000 :: RETURN :: DISPLAY AT(1,10):"FLAG F 3500 CALL HCHAR(10,1,32,9*32 UN":" By Jacques Grosloui ):: DISPLAY AT(18,6):"FALSE s":RPT$("~",28) - NO FLAG FOUND" :: RETURN 1100 GOSUB 2000 4000 GOSUB 4100 :: IF A AND 1200 GOSUB 2500 :: GOTO 1200 1 THEN DISPLAY AT(10,8):"A = 1999 END 1" :: IF T AND 1 THEN DISPL 2000 DISPLAY AT(4,1):"1- SET AY AT(10,16):"<<< TRUE" A = A OR ";B:RPT$("~",28) 4010 IF A AND 2 THEN DISPLAY 2010 DISPLAY AT(6,1):"2- TES AT(11,8):"A = 2" :: IF T AN T #1 IF A AND ";T D 2 THEN DISPLAY AT(11,16):" 2020 DISPLAY AT(7,1):"3- TES <<< TRUE" T #2 IF A = ";T:RPT$("~",28 4020 IF A AND 4 THEN DISPLAY );" THEN" AT(12,8):"A = 4" :: IF T AN 2030 DISPLAY AT(19,1):RPT$(" D 4 THEN DISPLAY AT(12,16):" ~",28):"4- CLEAR A = A AND 2 <<< TRUE" 55 -";C:RPT$("~",28) 4030 IF A AND 8 THEN DISPLAY 2040 DISPLAY AT(22,1):"5- CL AT(13,8):"A = 8" :: IF T AN EAR ALL A = 0":RPT$("~",28) D 8 THEN DISPLAY AT(13,16):" :: A=0 <<< TRUE" 2050 DISPLAY AT(24,2)BEEP:"C 4040 IF A AND 16 THEN DISPLA HOOSE 1,2,3,4,5 OR Q?:1" :: Y AT(14,8):"A = 16" :: IF T RETURN AND 16 THEN DISPLAY AT(14,16 2500 CALL KEY(3,K,S):: ACCEP ):"<<< TRUE" T AT(24,25)VALIDATE(DIGIT,"Q 4050 IF A AND 32 THEN DISPLA ")SIZE(-1)BEEP:S$ :: IF S$=" Y AT(15,8):"A = 32" :: IF T Q" THEN END AND 32 THEN DISPLAY AT(15,16 2505 S=VAL(S$):: IF S<1 OR S ):"<<< TRUE" >5 THEN 2500 4060 IF A AND 64 THEN DISPLA 2510 ON S GOSUB 3000,3100,32 Y AT(16,8):"A = 64" :: IF T 00,3300,3400 :: RETURN AND 64 THEN DISPLAY AT(16,16 3000 ACCEPT AT(4,20)VALIDATE ):"<<< TRUE" (DIGIT)SIZE(-3)BEEP:B :: IF 4070 IF A AND 128 THEN DISPL B>255 THEN 3000 AY AT(17,8):"A = 128" :: IF 3010 A=A OR B :: GOSUB 4000 T AND 128 THEN DISPLAY AT(17 :: RETURN ,16):"<<< TRUE" 3100 ACCEPT AT(6,23)VALIDATE 4090 DISPLAY AT(18,2):"Total (DIGIT)SIZE(-3)BEEP:T :: IF is:";A :: RETURN T>255 THEN 3100 :: IF A AND 4100 CALL HCHAR(10,1,32,9*32 T THEN GOSUB 4000 ELSE GOSUB ):: RETURN 3500 :: RETURN 29505 SUB BLUE 3200 T=0 :: V 29510 ! SWITCHES DISPLAY TO ALIDATE(DIGIT)SIZE(-3)BEEP:T WHITE ON BLUE; JLS 7/88 T :: IF A=TT THEN GOSUB 4000 29515 CALL SCREEN(5):: FOR L ELSE GOSUB 3500 :: RETURN =0 TO 14 :: CALL COLOR(L,16, 3300 ACCEPT AT(20,26)VALIDAT 1):: NEXT L :: SUBEND E(DIGIT)SIZE(-3)BEEP:C :: A= A AND(255-C):: GOSUB 4000 :: To assist in debugging an Extended Basic program which uses numerous flags, I also have written a program named FLAGGER. This program will identify the line numbers of various flags and identify whether the line SETs, TESTs or CLEARS the flag. If you want a copy drop me a line and I would be pleased to send you a copy.