retroforth/example/Save_and_Restore_Stack.forth
crc 87a29262f5 new example of saving & restoring the stack
FossilOrigin-Name: a04800a5ceee2398f2b2a0dcffe9b5867177c4e1eb2cd0a3d81be5e06d180f84
2019-01-18 17:58:40 +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
```