SB BBC @ GBR Oldie hints/tips #014-016 Hints and tips from the archives of Wakefield BBC Micro User Group... 14. Star commands in a program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It is sometimes useful to be able to use the 'Star' Machine Operating System's commands from inside a program; eg *KEY, *DISC, *CAT etc. These routines, written as Procedures, will allow you to do just that. Line 10 is only to demonstrate the Procedure, which you would call from your main program. (The best way to do this to test for the '*' key being pressed. The Procedure itself does not require this, as "CAT" is assumed to be "*CAT".) If you want to *CAT a cassette, then you would have to press to finish, so you need to use ON ERROR GOTO to avoid coming out of the program altogether. The LOCAL statement is so that you can use the variables X%, Y% and Z% in the rest of your program, without any clash. However, it might be wise to avoid them anyway, as Escaping from the Procedure could cause odd things to happen. You could substitute any integer variable for Z%, but you must use X% and Y%. The Procedure as it stands works on all BASIC versions, except BAS128 *, and with the alterations shown to the right, it works on all BASIC versions except BASIC-1. * NB: BAS128 is for the B+/Master/Compact only. 10 PROCcommand:PRINT"Done it!":END 20 : 1000 DEFPROCcommand:LOCAL X%,Y%,Z% DEFPROCcommand:LOCAL C$ 1010 DIM Z%-1:PRINT"Enter MOS command" PRINT"Enter MOS command" 1020 INPUT"*"$Z% INPUT"*"C$ 1030 X%=Z%:Y%=Z%DIV256:CALL &FFF7 OSCLI(C$) 1040 ENDPROC ENDPROC 15. Alphabetic sort ~~~~~~~~~~~~~~~ This is a very fast BASIC Sort Procedure, known as a Shell-Metzner Sort. It is far faster than a simple Bubble Sort on long lists, and can sort a several hundred names in only a few seconds. It can just be beaten by the Quicksort published in the November 1982 Beebug. However, the Quicksort uses recursion, which means you can't easily add any bits of your own in the Procedure without the routine hanging up, so I prefer to stick with this one. It assumes that you have a list of names, or whatever, in an array called file$, (or A$ etc. if you prefer). If you have DIMensioned the array as file$(300), and it's always full up, then you would use PROCsort(300), otherwise substitute a variable giving the last element with anything in it. If you want the list in descending alphabetical order, then change the "<=" in line 160 to ">=". As usual, all the variables except file$ itself are LOCAL, so can be used elsewhere without any clash. Unless you 'fill up' the whole array initially with strings of the maximum length likely to be encountered, then the Sort is bound to gobble up memory in use. This isn't the fault of the Procedure, but is a quirk of BBC BASIC, known as poor 'Garbage Collecting'. The Procedure can easily be adapted to sort numbers instead of strings, in which case this problem doesn't arise. 100 DEFPROCsort(extent%) 110 LOCAL I%,J%,K%,L%,M%,swap$:M%=extent% 120 M%=M%DIV2:IF M%=0 THEN 200 130 K%=extent%-M%:J%=1 140 I%=J% 150 L%=I%+M% 160 IF file$(I%)<=file$(L%) THEN 190 170 swap$=file$(I%):file$(I%)=file$(L%):file$(L%)=swap$ 180 I%=I%-M%:IF I%>0 THEN 150 190 J%=J%+1:IF J%>K% THEN 120 ELSE 140 200 ENDPROC 16. Program size ~~~~~~~~~~~~ To see how long your BASIC program, excluding variable space etc., type PRINT TOP-PAGE , or PRINT~TOP-PAGE if you want it in Hex. This also works on an Arc, with the addition that you can use END-PAGE if you want to include any variable space used by the program after it has been run. 73 Rick G4BLT @ GB7WRG