Beyond Basic - Part 3

Downloads|Programming|Beyond Basic

Written By: Tony Cruise

First Published: Micro’s Gazette – Issue 000 (November/December 1988)

 This issue I will explain about the Hook HGONE which can be used to add extra commands to your Basic programs.  This eliminates the need to use USR statements which can be confusing to use for people who are not familiar with machine code.  It also limits the number of parameters to one.

The Hook HGONE is called by the Basic ROM before each statement is processed.  This allows you to add a routine that checks if the next Basic statement is one that you have added.  For the ease of checking I will use the ‘[‘ character to identify any commands that we add.

The following routine creates two new Basic commands called [BANG and [ZAP.  Each time you use these statements they will produce a sound, [BANG an explosion sound and [ZAP a laser firing sound.  These new commands can be used in your Basic programs.

e.g.

10 PRINT “THIS IS A LASER...”:[ZAP

20 FOR A=1 TO 500:NEXT A

30 PRINT “THIS IS AN EXPLOSION...”:[BANGMachine Code Program

0000 1 ORIGIN ; Example of Hook HGONE
  2 ;
D000 3ORG 0D000H 
  4 ;
  5 HGONEEQU 0FF43H; Hook Jump – Basic handler
  6 ;
D000F37 STARTDI 
D001210ED08LD HL,CHECK; Address to jump to
D0042244FF9LD (HGONE+1),HL; Load into Hook
D0073EC310LD A,0C3H; Value for JUMP
D0093243FF11LD (HGONE),A; Load into Hook
D00CFB12EI 
D00DC913RET 
  14 ;
D00EFE5B15CHECK CP “[“; Is next statement
D010C016RET NZ; one of our commands
D011F317DI;
D012E518PUSH HL; Save pointer
D0131149D019LD DE,TABLE; Table of new commands
D016060020LD B,0; Loop counter
D0182321 LOOP1INC HL; Increment pointer
D0191A22LD A,(DE);
D01AFE0023CP 0;
D01C200224JR NZ,LOOP2; End of Table?
D01E184225JR EXIT; Yes – Back to Basic
D020FE2E26 LOOP2CP “.”; End of statement
D022201527JR NZ,LOOP3; No keep going
D0243328INC SP; Get rid of old
D0253329INC SP; pointer
D026E530PUSH HL; save new pointer
D027DD2153D031LD IX,JMPTBL;
D02B160032LD D,0;
D02D5833LD E,B;
D02ECB2334SLA E;
D030DD1935ADD IX,DE;
D032DD6E0036LD L,(IX+0); Calculate jump
D035DD660137LD H,(IX+1); address
D038E938JP (HL);
D0394E39 LOOP3LD C,(HL); Get next character
D03A1340INC DE; Set DE for next loop
D03BB941CP C; Match?
D03C28DA42JR Z,LOOP1; Yes, go again
D03E0443INC B; Next statement
D03FE144POP HL;
D040E545PUSH HL; Restore pointer
D0411A46 LOOP4LD A,(DE);
D0421347INC DE; Move DE to next
D043FE2E48CP “.”; statement
D04528D149JR Z,LOOP1; Loop again
D04718F850JR LOOP4;
  51 ;
D04942414E4758 TABLEDEFM “BANG.”; Statement table
D04D2E   
D04E5A41592E59DEFM “ZAP.”;
D0520060DEFB 0;
  61 ;
D05374D062 JMPTBLDEFW BANG; Jump Table
D0558CD063DEFW ZAP;
  64 ;
D0571A65 SOUNDLD A,(DE); Get next value
D058FEFF66CP 255; End of List?
D05AC867RET Z; Yes – Return
D05BD3A068OUT (0A0H),A; Send value
D05D1369INC DE; Increment pointer
D05EFE0770CP 7; Is it register 7
D0601A71LD A,(DE);
D061200672JR NZ,SNDLP1; No - Continue
D0634773LD B,A;
D064DBA274IN A,(0A2H); Get current value
D066E6C075AND 192;
D0688076ADD A,B; Combine values
D069D3A177 SNDLP1OUT (0A1H),A; Send value
D06B1378INC DE; Increment pointer
D06C18E979JR SOUND;
  80 ;
D06EE181 EXITPOP HL; Restore pointer
D06F7E82LD A,(HL); Restore A
D0703383INC SP; Get rid of
D0713384INC SP; Basic return
D072FB85EI;
D073C986RET;
  87 ;
D074117CD088 BANGLD DE,BANG1; Set pointer
D077CD57D089CALL SOUND; Call sound routine
D07A18F290JR EXIT; Exit to Basic
  91 ;
D07C0910020892 BANG1DEFB 9,16,2,8,3,5,7,40
D08003050728   
D08406140C0A93DEFB 6,20,12,10,13,0,255,255
D0880D00FFFF   
  94 ;
D08C1194D095 ZAPLD DE,ZAP1; Set pointer
D08FCD57D096CALL SOUND; Call sound routine
D09218DA97JR EXIT; Exit to Basic
  98 ;
D094091002C899 ZAP1DEFB 9,16,2,200,3,0,12,6 
D09803000C08   
D09C07380D04100DEFB 7,56,13,4,255,255 
D0A0FFFF   
D0A2 101 END  

Basic Loader

10 CLS:CLEAR 200,&HCFFF:DEFINTA-Z:A=&HD000

20 READ A$:IF A$ <> “@” THEN POKE A, VAL(“&H”+A$):A=A+1:GOTO 20

30 PRINT”  INSERT TAPE/DISK TO SAVE PROGRAM”

40 PRINT”       AND PRESS ANY KEY”

50 A$=INPUT$(1):PRINT:PRINT “  SAVING ....”

60 BSAVE”CHECK”,&HD000,A-1

100 DATA F3,21,0E,D0,22,44,FF,3E,C3,32,43,FF,FB,C9,FE,5B

110 DATA C0,F3,E5,11,49,D0,06,00,23,1A,FE,00,20,02,18,4E

120 DATA FE,2E,20,15,33,33,E5,DD,21,53,D0,16,00,58,CB,23

130 DATA DD,19,DD,6E,00,DD,66,01,E9,4E,13,B9,28,DA,04,E1

140 DATA E5,1A,13,FE,2E,28,D1,18,F8,42,41,4E,47,2E,5A,41

150 DATA 50,2E,00,74,D0,8C,D0,1A,FE,FF,C8,D3,A0,13,FE,07

160 DATA 1A,20,06,47,DB,A2,E6,C0,80,D3,A1,13,18,E9,E1,7E

170 DATA 33,33,FB,C9,11,7C,D0,CD,57,D0,18,F2,09,10,02,08

180 DATA 03,05,07,28,06,14,0C,0A,0D,00,FF,FF,11,94,D0,CD

190 DATA 57,D0,18,DA,09,10,02,C8,03,00,0C,06,07,38,0D,04

200 DATA FF,FF,@

Next issue I will cover how to read command line variables and start on the new commands.