dup 
Author Message
 dup

I'm new to forth, What does the dup, rot and swap functions do in forth?


Sat, 21 Sep 2002 03:00:00 GMT  
 dup
Given a forth stack, these are some of the basic operations to shuffle data
on the stack.

These effect of these operations can be expressed in Forth stack notation.

Stack effect notation shows how the stack looks before and after an
operation, but does not imply the operation performed.  Only the quantity
and type of numbers on the stack are represented.  The notation is often
placed in Forth code, but is really just a comment since the operators: (  )
are used exclusively for comments.

Where n, n1, etc. represent integer numbers.

DUP  ( n -- n n )  DUPlicates the top number.
ROT  ( n1 n2 n3 -- n2 n3 n1 ) ROTates.  Pulls the third deep number off the
stack and pushes it on top.
SWAP ( n1 n2 -- n2 n1 ) SWAPs the top two numbers.

example code for a function called as "double-a-number":

: double-a-number ( n -- n )
  dup +
;

Carl Vogt

Quote:

>I'm new to forth, What does the dup, rot and swap functions do in forth?



Sat, 21 Sep 2002 03:00:00 GMT  
 dup

Quote:

> I'm new to forth, What does the dup, rot and swap
> functions do in forth?

They are auxiliary words that bind together useful words.
Like English words "am", "in", "to".
Read the following and see how the operation
is performed with *last* values. And what "last" is.
[The Forth system that I use shows numbers that it remembers
in parens after "ok".]

1 2 + .
3  ok
1 2 - .
-1  ok
1 2 SWAP - .
1  ok
1 2 . .
2 1  ok
1 2 SWAP . .
1 2  ok
1 2
 ok( 1 2 )
.
2  ok( 1 )
.
1  ok
1 2
 ok( 1 2 )
SWAP
 ok( 2 1 )
.
1  ok( 2 )
.
2  ok
10 2 3 * + .
16  ok
1 2 3
 ok( 1 2 3 )
*
 ok( 1 6 )
+
 ok( 7 )
.
7  ok
10 2 3 ROT * + .
32  ok
10 2 3
 ok( 10 2 3 )
ROT
 ok( 2 3 10 )
*
 ok( 2 30 )
+
 ok( 32 )
.
32  ok
3 DUP + .
6  ok
3 DUP * .
9  ok
3
 ok( 3 )
DUP
 ok( 3 3
*
 ok( 9 )
.
9  ok
1 2 3 + . .
5 1  ok
1 2 3
 ok( 1 2 3 )
+
 ok( 1 5 )
.
5  ok( 1 )
.
1  ok



Sun, 22 Sep 2002 03:00:00 GMT  
 dup
Thanks a lot for the help.


Quote:
> Given a forth stack, these are some of the basic operations to shuffle data
> on the stack.

> These effect of these operations can be expressed in Forth stack notation.

> Stack effect notation shows how the stack looks before and after an
> operation, but does not imply the operation performed.  Only the quantity
> and type of numbers on the stack are represented.  The notation is often
> placed in Forth code, but is really just a comment since the operators: (  )
> are used exclusively for comments.

> Where n, n1, etc. represent integer numbers.

> DUP  ( n -- n n )  DUPlicates the top number.
> ROT  ( n1 n2 n3 -- n2 n3 n1 ) ROTates.  Pulls the third deep number off the
> stack and pushes it on top.
> SWAP ( n1 n2 -- n2 n1 ) SWAPs the top two numbers.

> example code for a function called as "double-a-number":

> : double-a-number ( n -- n )
>   dup +
> ;

> Carl Vogt


> >I'm new to forth, What does the dup, rot and swap functions do in forth?



Sun, 22 Sep 2002 03:00:00 GMT  
 dup

Quote:

>I'm new to forth, What does the dup, rot and swap functions do in forth?

What's your experience in other languages?

If you're familiar with functional languages, those words are
"combinators", which act to provide dataflow to functions.

If you're not, then those words are simple functions which take as input a
stack, and pproduce as output a modified stack.

dup takes as input a stack which has at least one element.  The returned
stack is like the input stack, except the top element is duplicated.

And so on.

--
-William "Billy" Tanksley



Sun, 22 Sep 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. +R as a name for DUP >R

2. UNDROP an alternative to ?DUP

3. DUP >R

4. Memory behavior of String#dup

5. dup, clone, and "deep copy"

6. self being altered even when .dup'ed

7. dup vs clone

8. 5 dup ($)

9. Resb and dup in NASM

10. Problem with DUP

11. Resb and dup in NASM

12. DUP

 

 
Powered by phpBB® Forum Software