retroforth/example/SaveAndRestoreStack.forth
crc 1745020426 remove underscores from example filenames
FossilOrigin-Name: 662ed33a030013c06ec45fe1dddff9d47440dd53db1a3abdb16884ddca5fae17
2019-02-18 12:48:49 +00:00

22 lines
383 B
Forth

# Save and Restore Stack
It's sometimes useful to temporarily save and restore the entire
stack. These two words allow for this.
## The Code
~~~
:stack:save (-a)
here [ depth dup , &, times ] dip ;
:stack:restore (a-)
&reset dip
dup fetch over + swap fetch [ dup fetch swap n:dec ] times drop ;
~~~
## Test Case
```
#1 #2 #3 #4 #5
stack:save #3 swap stack:restore
```