A Fig-Forth Utility
Jürgen Pfeifer
There are several versions of Forth. The most popular is the Implementation of the Forth-Interest-Group (FIG), the well known fig-Forth.. But there exists an improvement, the 79-Standard Forth, which is very close to fig-Forth.
The 79-Standard describes a very useful word; which doesn't exist in fig-Forth. It is the word "roll." The Forth stack notation for roll is:
roll n–
This word extracts the nth stack value to the top of stack, not counting n itself, moving the remaining values into the vacated position, n must be strictly positive.
Examples: 3 roll = rot (a fig-word). 1 roll = no operation.
The screens here contain a low-level definition of "roll" for a 6502 fig-Forth, using the Forth 6502 macroassembler.
As an application, the screens contain the definition of the signed double-integer multiplication operator "d*". Try to define it without roll!
SCR # 106 0 ( ROLL 1 CODE ROLL ( N --- ) JPF JUL82 ) 2 1 # LDA, SETUP JSK, XSAVE STX, N LDA, SEC, 1 # SBC, CS 3 IF, 0= NOT IF, 4 TAY, .A ASL, CLC, XSAVE ADC, TAX, 5 BOT LDA, PHA, BOT 1+ LDA, PHA, 6 BEGIN, DEX, DEX, 7 BOT LDA, SEC STA, 8 BOT 1+ LDA, SEC 1+ STA, 9 DEY, 0= 10 UNTIL, PLA, PUT JMP, 11 THEN, 12 THEN, NEXT JMP, END-CODE 13 (ROLL : EXTRACT THE N-TH STACK VALUE TO THE TOP OP STACK, NOT 14 COUNTING N ITSELF, MOVING THE REMAINING VALUES INTO THE 15 VACATED POSITION. N>0 ) SCR # 107 0 ( ROLL APPLICATION JPF JUL82 ) 1 : PICK 2* SP@ + @ ; 2 (N1 --- N2 : RETURN THE CONTENTS OF THE N1-TH STACK VALUE, 3 NOT COUNTING N1 ITSELF. N>0 ) 4 5 : 2SWAP ROT >R ROT R> ; 6 ( D1 D2 --- D2 D1 : EXCHANGE THE TOP TWO DOUBLE NUMBERS 7 ON THE STACK. ) 8 9 : D* OVER 5 PICK U* 6 ROLL 4 ROLL * + 2SWAP * + ; 10 ( D1 D2 --- D3 : LEAVES THE ARITHMETIC PRODUCT OF THE 11 DOUBLE PRECISION INTEGERS D1 AND D2 ) 12 13 14 15 |