Appendix G

The Wedge


One of the best reasons to learn machine language is that it can improve your BASIC programming significantly. There are two main ways that machine language can assist BASIC programming: adding commands to BASIC itself and replacing parts of a BASIC program with a high-velocity machine language subroutine. To add an ML subroutine to a BASIC program, you SYS, USR, or CALL (from Microsoft, Atari, or Apple BASICs respectively). That's fairly straightforward. To make changes to the BASIC language itself, however, we need to wedge into BASIC somehow.

     You can make BASIC a customized language with a wedge. Do you want auto-numbering when writing a program in BASIC? Add it. Does your BASIC lack a RENUMBER facility? You can give it one. Do you want all your BASIC programs to contain a REM line with your name in it? This could be automatically put into each of your programs if you know machine language. Using a wedge to a machine language program, you can communicate directly to your machine, bypass BASIC's limitations, and do pretty much what you want to do.


How To Wedge In

Adding commands to BASIC is a matter of interrupting a loop. This is often referred to as adding a wedge into BASIC. Under the control of the BASIC language, the computer is looking to see if a BASIC word has been typed in, followed by a hit on the RETURN key. Or, during a RUN, the computer examines the program in memory to see what you want accomplished.

     These, then, are the two contexts in which the computer analyzes a BASIC word: in a program or in "direct mode." In direct mode, you can type the word "LIST" onto the screen and hit the RETURN key. The computer looks up the meaning of "LIST" in a table of words which includes the addresses of the appropriate ML subroutines. It then JSR's (Jumps to a SubRoutine) somewhere in the vast ML of your computer's BASIC. This subroutine performs the actions necessary to provide you with a listing of the program in your computer's memory. If you could add some additional words to this table, you could add to BASIC. You could customize it.

     Here's how. When you first turn on a computer which uses Microsoft BASIC, one of the first things that happens is that the operating system puts some important ML instructions into a zone in the first 256 memory locations (this area of RAM is called zero page). These instructions are put into zero page to handle the loop - often called the CHRGET loop (which means "character get") - where the operating system will forever after jump while power is on. This location is of great importance to BASIC; it is the "did they type any BASIC into the computer?" subroutine. It's where BASIC analyzes what it finds on screen or in a program, looking at something character by character to see what it adds up to.

      If you type "LIST," this little zero page ML subroutine looks at the "L" then the "I" and so on. The exact location of CHRGET differs on the various computers:

PET (Original BASIC):             decimal address
PET/CBM (Upgrade & 4.0):  
VIC:    
64:    
Apple:    
194-217
112-135
115-138
115-138
177-200

     The CHRGET ML program looks like this:

0070 E6 77    INC $77
0072 D0 02    BNE $0076
0074 E6 78    INC $78
0076 AD 03 02 LDA $0203
0079 C9 3A    CMP #$3A
007B B0 0A    BCS $0087
007D C9 20    CMP #$20
007F F0 EF    BEQ $0070
0081 38       SEC
0082 E9 30    SBC #$30
0084 38       SEC
0085 E9 D0    SBC #$D0
0087 60       RTS

     This is put into your zero page RAM within the first few seconds after you turn on the computer. You can change it (RAM memory can be changed) to jump (JMP) to your own ML program by replacing the first three bytes of code. In our example above, we will replace the three bytes at hexadecimal location 0070 (the exact address will vary according to the CHRGET location as listed above for the different computers). Here is how the replacement looks in the example CHRGET routine:


0070 4C 00 75 JMP $7500
0073 02       ???
0074 E6 78    INC $78
0076 AD 02 02 LDA $0202
0079 C9 3A    CMP #$3A
007B B0 0A    BCS $0087
007D C9 20    CMP #$20
007F F0 EF    BEQ $0070
0081 38       SEC
0082 E9 30    SBC #$30
0084 38       SEC
0085 E9 D0    SBC #$D0
0087 60       RTS

     The effect that this has is dramatic. Whenever the computer looks for a character in BASIC mode, it will jump first (because you forced it to) to your personal ML "wedged" routine located at $7500. The subroutine at $7500 could be anything you wanted it to be, anything you've put at address $7500. For an example, we've caused an "A" to appear on the PET/CBM screen:


7500 E6 77    INC $77
7502 D0 02    BNE $7506
7504 E6 78    INC $78
7506 A9 41    LDA #$41
7508 8D 00 80 STA $8000
750B 4C 76 00 JMP $0076

     Notice that we had to first perform the actions that the CHRGET would have performed. Before we can start our LDA #$41 to put an "A" on screen, we had to replace the early part of CHRGET that we wrote over (see 7500 to 7505 in Example 3). And, after we're done with our custom routine, we jump back into CHRGET at 750B.

     Adding a wedge to Atari BASIC is somewhat more involved. A clear and complete exposition of the techniques involved appears in an article by my colleague Charles Brannon, "The Atari Wedge" (COMPUTE! Magazine, November 1982).


Return to Table of Contents | Previous Chapter | Next: Download the software