Classic Computer Magazine Archive ANTIC VOL. 1, NO. 3 / AUGUST 1982

Assembly Language
Basic Range Delete

By Adrian Dery

Getting tired of deleting BASIC statements one line at a time? This is an Assembly Language program that will delete any number of lines by simply giving it the FIRST and LAST line numbers in the range.

The BASIC program in Listing 1 is easy to use and requires no memory other than Page Six. Just load and run it, first thing after turning on the computer and the Range Delete function is available as long as the power stays on. System Reset, Break and CLOAD's do not affect it, but be careful not to load anything else in Page Six.

The next step is to set up an easy method to access the Assembly program, by entering a line something like:

32767 INPUT FIRST, LAST:X=USR(1536,FIRST,LAST)

Then, from immediate mode, enter:

DEL = 32767

Remember that LOAD's and CLOAD's will wipe out line 32767 and a RUN will reset DEL to zero. Therefore you will have to repeat one or both of the above as necessary.

Complete the above steps and you are ready to do any kind of BASIC editing. To delete any number of lines in a range, enter G. DEL from immediate model and BASIC will respond with the ? prompt. Enter the FIRST and LAST Line numbers in the range and all the lines in between will disappear.

Another useful feature of this program is that the USR call can be dynamically executed from a running BASIC program. This makes it a super memory saver if you have a program that uses a lot of memory to initialize strings or memory from literals or DATA statements. Put a USR call at the end of the initialization routine with the FIRST and LAST line numbers of the routine as parameters and the whole routine will be automatically deleted. Just make sure that the USR call, if it's going to be deleted, is on a line by itself or is the last statement on the last line.

One precaution is necessary. The USR call must be on a line number equal to or higher than the highest line to be deleted. The reason is that memory taken up by the deleted lines is reclaimed by the lines above the deleted section. Not only do the lines above the deleted section get moved down into lower memory, but also, a few BASIC internal pointers have to get adjusted by the amount deleted. One of them is the pointer to the current statement being executed, which is the USR call. The amount of Assembly code needed to adjust the BASIC pointer did not seem in line with the advantages. Therefore, the Delete Program assumes that this pointer has to be adjusted and does it.

0100  *=  1536
0101 STMTAB =   136
0105 STMCUR =   138
0110 MEMTOP =   144
0115 HIADR =   203
0120 LOADR =   205
0200 ;
0201 ;GET FIRST LINE NUMBER
0202 ;
0205  CLD
0210  PLA
0215  PLA
0220  STA LINNBR+1
0225  PLA
0230  STA LINNBR
0235 ;
0236 ;LOCATE ADDRESS OF
0237 ;FIRST LINE NUMBER
0238 ;
0240  LDA STMTAB
0241  STA HIADR
0242  LDA STMTAB+1
0243  STA HIADR+1
0245  JSR SRCHLN
0260 ;
0261 ;SAVE ADDRESS OF FIRST
0262 ;LINE NUMBER
0263 ;
0265  LDA HIADR
0270  STA LOADR
0275  LDA HIADR+1
0280  STA LOADR+1
0300 ;
0301 ;GET LAST LINE NUMER
0302 ;PLUS 1
0303 ;AND IT'S ADDRESS
0304 ;
0305  PLA
0310  STA LINNBR+1
0315  PLA
0320  CLC
0325  ADC #1
0330  STA LINNBR
0335  BCC GETTO
0340  INC LINNBR+1
0345 GETTO JSR SRCHLN
0360 ;
0361 ;LENGTH TO MOVE =
0362 ;MEMTOP -
0363 ;ADDRESS OF LAST LINE
0364 ;
0365  SEC
0370  LDA MEMTOP
0375  SBC HIADR
0380  STA MOVLEN
0385  LDA MEMTOP+1
0390  SBC HIADR+1
0395  STA MOVLEN+1
0400 ;
0401 ;LENGTH OF POINTER
0402 ;ADJUSTMENT =
0403 ;HIADR - LOADR
0404 ;
0405  SEC
0410  LDA HIADR
0415  SBC LOADR
0420  STA MEMADJ
0425  LDA HIADR+1
0430  SBC LOADR+1
0435  STA MEMADJ+1
0445 ;
0446 ;REMOVE DELETED SPACE
0447 ;
0450  LDY #0
0452  LDX MOVLEN+1
0454  BEQ NOPG
0456 NXT256 LDA (HIADR),Y
0458  STA (LOADR),Y
0460  INY
0462  BNE NXT256
0464  INC HIADR+1
0466  INC LOADR+1
0468  DEX
0470  BNE NXT256
0472 NOPG CPY MOVLEN
0474  BEQ ADJPNT
0476  LDA (HIADR),Y
0478  STA (LOADR),Y
0480  INY
0482  BNE NOPG
0500 ;
0501 ;ADJUST BASIC POINTERS
0502 ;
0505 ADJPNT LDX #0
0510  LDY #4
0515 ADJLOP SEC
0520  LDA STMCUR,X
0525  SBC MEMADJ
0530  STA STMCUR,X
0535  LDA STMCUR+1,X
0540  SBC MEMADJ+1
0545  STA STMCUR+1,X
0550  INX
0555  INX
0560  DEY
0565  BNE ADJLOP
0566 ;
0567 ;RETURN TO USER
0568 ;
0570  RTS
0700 ;
0701 ;SEARCH BASIC'S
0702 ;STATEMENT TABLE FOR A
0703 ;LINE NUMBER => LINNBR
0704 ;
0705 SRCHLN LDY #2
0710  LDA (HIADR),Y
0715  STA LINLEN
0720  DEY
0725  LDA (HIADR),Y
0727  DEY
0730  CMP #128
0735  BEQ LINRET
0740  CMP LINNBR+1
0745  BEQ LOOKLO
0750  BCS LINRET
0755  BCC BMPLIN
0760 LOOKLO LDA (HIADR),Y
0765  CMP LINNBR
0770  BCS LINRET
0775 BMPLIN CLC
0785  LDA LINLEN
0790  ADC HIADR
0795  STA HIADR
0800  BCC SRCHLN
0805  INC HIADR+1
0810  BNE SRCHLN
0820 LINRET RTS
0959 ;
0960 MOVLEN .BYTE 0
0961  .BYTE 0
0965 LINNBR .BYTE 0
0966  .BYTE 0
0970 MEMADJ .BYTE 0
0971  .BYTE 0
0975 LINLEN .BYTE 0
0999  .END

Listing: RANGEDEL.SRC Download / View