Classic Computer Magazine Archive COMPUTE! ISSUE 30 / NOVEMBER 1982 / PAGE 232

The FORTH Page

Disk Management

Michael Riley
Philadelphia

The words SERT and TRADE allow you to move and mass-move screens around on a disk without fear of accidentally erasing something of value. No matter how you use these commands, no screens are ever overwritten. They simply end up somewhere else.

SERT (N1 N2 N3 —)

Deletes the group of screens numbered N1 through N2 and reinserts them between the screens numbered N3 and N3 + 1.

TRADE (N1 N2 N3 —)

Swaps a group of screens numbered N1 through N2 with a group of screens starting at N3. The two groups are assumed to be the same size.

The algorithm for SERT is a bit tricky, but it is an effective solution to a common problem. The problem with most block-move algorithms is that they need a large buffer area to store the block while moving the text that lies between the block and the destination point. Other algorithms move the block one small piece at a time, and the entire text in between must be repositioned once for each move.

The algorithm in SERT does a little more calculating in order to save space and time. SERT picks up the first screen that needs to be moved and calculates its destination position. Next, it picks up a second screen at that position before setting down the first screen. The destination of the second screen is then calculated, and so on.

The following words were used to implement SERT and TRADE and would not normally be used for editing:

(SERT)(N1 N2 N3 —)

Swaps two adjacent groups of screens. The two groups need not be the same size. Screens N1 through N2-1 are swapped with N2 through N3-1.

FX (N1 N2 — N1 N2 or N2 N1)

Two numbers are swapped if N1 is larger. "fix"

PAD-SWAP (N1 —)

Swaps the contents of block # N1 with 1024 bytes at pad.

B-DIST (variable)

The total number of screens to be moved. "big distance"

LIM (variable)

The highest screen to be moved plus one.

CNT (variable)

The number of screens moved. "count"

S-DIST (variable)

The number of screens in the highest numbered group. "small distance"

STRT (variable)

The first screen to be moved. "start"

Program 1.

SCR # 122
 0 ( PAD-SWAP B-DST CNT S-DEST STRT )
 1 : PAD-SWAP ( N1 --- / SWAP SCR W/ PAD )
 2	 DUP BLOCK PAD 1024 + 1024 CMOVE
 3 	 PAD SWAP BLOCK 1024 CMOVE UPDATE
 4 	 PAD 1024 + PAD 1024 CMOVE ;
 5 0 VARIABLE B-DST 0 VARIABLE LIM 0 VARIABLE CNT
 6 0 VARIABLE S-DST 0 VARIABLE STRT
 7 :  (SERT) ( N1 N2 N3 --- )
 8	 DUP LIM ! SWAP - S-DST ! STRT !
 9	 LIM @ STRT @ - B-DST ! 0 CNT ! STRT @
10	 BEGIN DUP PAD-SWAP
11	   BEGIN S-DST @ + DUP LIM @ > 0 =
12	   IF B-DST @ - THEN
13	   DUP PAD-SWAP 1 CNT +! DUP STRT @ = 	
14	 UNTIL CNT @ B-DST @ <
15	WHILE 1 STRT +! 1+ REPEAT DROP ; -->

Program 2.

SCR # 123 0 ( FX SERT TRADE RANGE? ROOM? ) 1 : ROOM? ( / IF ERROR, PRINT ‘NO ROOM IN DICT’ ) 2 PAD 2048 + FIRST > 14 ?ERROR ; 3 ( ‘ FIRST > ’ IS INSTALATION DEPENDENT ) 4 : RANGE? ( N1 --- N1 / LEGAL SCREEN NUMBER? ) 5 1 OVER > OVER 300 ( 4040 PET ) > OR 6 ?ERROR; 6 : FX ( N1 N2 --- N2 N1 [OR] N1 N2 ) 7 OVER OVER > IF SWAP THEN ; 8 : SERT ( 1ST LAST DEST --- / REINSERT SCRS AT DEST ) 9 ROOM? 1+ RANGE? SWAP 1+ RANGE? ROT RANGE? 10 FX > R FX R > FX (SERT) ; 11 : TRADE ( IST LAST DEST --- / TRADE WITH SCRS AT DEST ) 12 ROOM? ROT > R R SWAP - B-DST ! ( LAST ) 13 1+ R > ( LAST + 1 1ST) 14 DO I RANGE? I B-DST @ - RANGE? ( I I + DST) 15 OVER PAD-SWAP PAD-SWAP PAD-SWAP LOOP DROP ;