home *** CD-ROM | disk | FTP | other *** search
- \ Problem 4.18 04/15/90 19:33:49.25
-
- \ Create an array to be used for a 10 number stack.
- CREATE P-STACK 20 ALLOT \ Stack will hold 10 16bit numbers.
-
- VARIABLE P-INDEX \ Holds the value of user stack pointer.
-
- \ Clear and initialize the Personal user stack.
- : P-CLEAR ( -- D) ( ?? -- P)
- 0 P-INDEX ! P-STACK 20 ERASE ;
-
- \ Return number of elements on the user stack to Forth's data stack.
- : P-DEPTH ( -- n D) P-INDEX @ 2/ ;
-
- \ Increment user stack pointer with error checking.
- : P-INC ( -- D)
- P-INDEX @ 20 =
- IF ." P-OVERFLOW" P-CLEAR
- ELSE 2 P-INDEX +! THEN ;
-
- \ Decrement user stack pointer with error checking.
- : P-DEC ( -- D)
- P-INDEX @ 0=
- IF ." P-UNDERFLOW"
- ELSE -2 P-INDEX +! THEN ;
-
- \ Move number from Forth's data stack to the P-stack.
- : >P ( n -- D) ( -- n P)
- P-INC P-INDEX @ P-STACK + ! ;
-
- \ Copy current top number of P-stack to top of Forth's
- \ data stack. Note: P-stack is not changed.
- : P@ ( -- n D) ( n -- n P)
- P-INDEX @ P-STACK + @ ;
-
- \ Move number from top of P-stack to top of Forth's
- \ data stack. Note: Number is removed from P-stack.
- : P> ( -- n D) ( n -- P)
- P@ P-DEC ;
-
- \ Display all numbers on the P-stack.
- : .P ( -- )
- P-DEPTH ?DUP \ if P-stack is not empty duplicate the depth number
- IF 1+ 1 ?DO I 2* P-STACK + @ 8 .R LOOP
- \ if depth number is no zero do the depth number of
- \ times of following:
- \ calculate the address of the current stack,
- \ fetch the value from the address,
- \ print the value with right adjusted 8 positions.
- ELSE ." P-STACK EMPTY" THEN ;
- \ if depth number is zero say " P-STACK EMPTY ".
-
-