Classic Computer Magazine Archive COMPUTE! ISSUE 124 / DECEMBER 1990 / PAGE G-21

FEEDBACK

QUESTIONS FROM OUR READERS

A Dynamic Loader

Recently, I was going through one of your back issues and found a program similar to the one below.

CE 10 POKE 184, 2: POKE 185,0: POKE 186,8
KG 20 INPUT "FILENAME" ; N$ : IFLE N(N$)> 16THEN20
FQ 30 INPUT "STARTING ADDRESS" ; S : IFS<0ORS>65535THEN30
KG 40 POKE 183, LEN(N$) : POKE 18 7, 0 : POKE 188, 2
XK 50 FOR I=1 TO LEN(N$): POKE {SPACE} 511+I,ASC(MID$(N$,I,1)) : NEXT I
XK 60 POKE 780, 0 : POKE 781, S-IN T (S/256) * 256 : POKE 782,IN T(S/256)
CE 70 SYS 65493 : IFST <> 64THENPRINT"FILE NOT FOUND"

It POKEs a few numbers into memory and then SYSs to an address. When I run it, it loads a program into a selected memory area.

I've seen programs similar to this one in other magazines. Could you explain to me the principles behind this program and how it might be used?

JERRY HALLETT
STOCKTON, NY

The program uses the Kernal LOAD routine to load a file beginning at a specific address. The three POKES in line 10 set the logical file number, secondary address, and device number as you would for an OPEN command. (The equivalent OPEN command would be OPEN 2,8,0.) Lines 20 and 30 request the filename and the file's starting address from the user. The first POKE in line 40 sets the length of the filename, and the other two represent the address (512) of the filename in low-byte/high-byte format. Line 50 actually stores the filename in locations 512–527.

Line 60 sets the 6502's registers to prepare for calling the Kernal LOAD routine. The first POKE sets the accumulator to 0 to tell the routine to perform a load. (Placing a 0 in the accumulator would cause the routine to verify.) The next two POKEs store the low and high bytes of the starting address into the X and Y registers, respectively. Line 70 calls the Kernal routine and then checks for an error after it has returned.

You didn't mention a save routine in your letter, but we've included one to demonstrate how to call the Kernal's SAVE routine from BASIC.

QH 10 DV = 8 : INPUT "FILENAME" ; N$ : OPEN15, DV, 15 : OPEN1, DV, 1, N$ + ", P, W"
JA 20 INPUT # 15, EN : IFEN <> 0THENPRINT "DISK ERROR" : GOTO 70
FQ 30 INPUT "STARTING ADDRESS" ; S : IFS <0ORS> 65535THEN30
SR 40 INPUT "ENDING ADDRESS" ; E : IFE <SORE> 65535THEN40
KS 50 POKE 193,S-INT (S/256) * 256 : POKE 194, INT (S/256)
PE 60 POKE 174,E-INT (E/256) * 256 : POKE 175, INT (E/256) : SY S62957
KM 70 CLOSE1 : CLOSE15

Line 10 inputs the filename from the user and attempts to open it. Line 20 checks to see whether the open was successful. Lines 30 and 40 get the starting and ending addresses for the file from the user. Line 50 sets a pointer to the starting address while line 60 sets the pointer to the ending address and calls the routine. Line 70 closes the file after the save.