ensuring full cell arithmetics
[rrq/rrqforth.git] / stack.asm
1 ;;; Words for stack manipulations
2         
3         WORD p_dup, 'DUP',fasm
4         ;; ( v -- v v )
5         ;; Duplicate top ov stack value.
6         ;; push qword [rsp] ??
7         mov rax,qword [rsp]
8         push rax
9         next
10
11         WORD p_2dup, '2DUP',fasm
12         ;; ( x1 x2 -- x1 x2 x1 x2 )
13         ;; Duplicate cell pair x1 x2.
14         push qword [rsp+8]
15         push qword [rsp+8]
16         next
17
18         WORD p_drop, 'DROP',fasm
19         ;; ( x -- )
20         ;; Remove x from the stack.
21         add rsp,8
22         next
23
24         WORD p_2drop, '2DROP',fasm
25         ;; ( x1 x2 -- )
26         ;; Drop cell pair x1 x2 from the stack.
27         add rsp,16
28         next
29
30         WORD p_over, 'OVER',fasm
31         ;; ( x1 x2 -- x1 x2 x1 )
32         ;; Place a copy of x1 on top of the stack. 
33         push qword [rsp+8]
34         next
35
36         WORD p_2over, '2OVER',fasm
37         ;; ( x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2 )
38         ;; Copy cell pair x1 x2 to the top of the stack.
39         push qword [rsp+24]
40         push qword [rsp+24]
41         next
42
43         WORD p_swap, 'SWAP',fasm
44         ;; ( x1 x2 -- x2 x1 )
45         ;; Exchange the top two stack items.
46         mov rax,qword [rsp]
47         mov rbx,qword [rsp+8]
48         mov qword [rsp+8],rax
49         mov qword [rsp],rbx
50         next
51
52         WORD p_2swap, '2SWAP',fasm
53         ;; ( x1 x2 x3 x4 -- x3 x4 x1 x2 )
54         ;; Exchange the top two cell pairs.
55         mov rax,qword [rsp]
56         mov rbx,qword [rsp+16]
57         mov qword [rsp], rbx
58         mov qword [rsp+16],rax
59         mov rax,qword [rsp+8]
60         mov rbx,qword [rsp+24]
61         mov qword [rsp+8], rbx
62         mov qword [rsp+24],rax
63         next
64
65         WORD p_rot, 'ROT',fasm
66         ;; ( x1 x2 x3 -- x2 x3 x1 )
67         ;; Rotate the top three stack entries.
68         mov rax,qword [rsp+16]
69         mov rbx,qword [rsp+8]
70         mov qword [rsp+16],rbx
71         mov rbx,qword [rsp]
72         mov qword [rsp+8],rbx
73         mov qword [rsp],rax
74         next
75
76         ;; ( xu xu-1 ... x0 u -- xu-1 ... x0 xu )
77         ;; Remove u. Rotate u+1 items on the top of the stack. An
78         ;; ambiguous condition exists if there are less than u+2 items
79         ;; on the stack before ROLL is executed.
80         WORD p_roll, 'ROLL',fasm
81         pop rcx
82         shl rcx,3
83         add rcx,rsp
84         mov rax,[rcx+8]
85 p_roll_loop:
86         cmp rcx,rsp
87         je p_roll_eq
88         mov rbx,[rcx]
89         mov [rcx+8],rbx
90         sub rcx,8
91         jmp p_roll_loop
92 p_roll_eq:
93         mov [rsp],rax
94         next
95
96         WORD p_nip, 'NIP',fasm
97         ;; ( x1 x2 -- x2 )
98         ;; Discard the second stack item. 
99         pop rax
100         mov qword [rsp],rax
101         next
102
103         WORD p_pick, 'PICK',fasm
104         ;; ( xu...x1 x0 u -- xu...x1 x0 xu )
105         ;; Remove u. Copy the xu to the top of the stack. An ambiguous
106         ;; condition exists if there are less than u+2 items on the
107         ;; stack before PICK is executed.
108         pop rax
109         shl rax,3               ; 8 bytes per index
110         push qword [rsp+rax]
111         next
112
113         WORD w6.2.2300, 'TUCK',fasm
114         ;; ( x1 x2 -- x2 x1 x2 )
115         ;; insert the top stack value into below second stack value.
116         pop rax
117         pop rbx
118         push rax
119         push rbx
120         push rax
121         next