added STREAM macro
[rrq/rrqforth.git] / logic.asm
1 ;;; Logic words
2
3         WORD p_and, 'AND',fasm
4         ;; ( x1 x2 -- x3 )
5         ;; x3 is the bit-by-bit logical "and" of x1 with x2.
6         pop rax
7         and qword [rsp], rax
8         next
9
10         WORD p_or, 'OR',fasm
11         ;; ( x1 x2 -- x3 )
12         ;; x3 is the bit-by-bit inclusive-or of x1 with x2. 
13         pop rax
14         or qword [rsp],rax
15         next
16
17         WORD p_xor, 'XOR',fasm
18         ;;  ( x1 x2 -- x3 )
19         ;; x3 is the bit-by-bit exclusive-or of x1 with x2.
20         pop rax
21         xor qword [rsp],rax
22         next
23
24         WORD p_not, 'NOT',fasm
25         ;; ( x -- v )
26         ;; v = 0 if x is non-zero and -1 otherwise
27         not qword [rsp]
28         next
29
30         WORD p_false, 'FALSE',fasm
31         ;; ( -- 0 )
32         ;; Push a false flag, 0.
33         push qword 0
34         next
35
36         WORD p_true, 'TRUE',fasm
37         ;; ( -- true )
38         ;; Return a true flag, -1. (non-zero)
39         push qword -1
40         next
41
42 pop_false_next:
43         mov qword [rsp],0
44         next
45
46 pop_true_next:
47         mov qword [rsp],-1
48         next
49
50         WORD p_within, 'WITHIN',fasm
51         ;; ( n1 n2 n3 -- flag )
52         ;; Push true if n2 <= n1 and n1 < n3 and false otherwise.
53         xor rcx,rcx
54         pop rax
55         pop rbx
56         cmp qword [rsp],rbx
57         jl p_within_not
58         cmp qword [rsp],rax
59         jge p_within_not
60         not rcx
61 p_within_not:
62         next
63         
64         WORD p_0less, '0<',fasm
65         ;; ( n -- flag )
66         ;; flag is true (non-zero) if and only if n is less than zero.
67         pop rax
68         cmp rax,qword [rsp]
69         jl pop_true_next
70         jmp pop_false_next
71
72         WORD p_0equal, '0=',fasm
73         ;; ( x -- flag )
74         ;; flag is true if x is equal to zero otherwise false.
75         cmp qword [rsp],0
76         je pop_true_next
77         jmp pop_false_next
78         
79         WORD p_lessthan, '<',fasm
80         ;; ( n1 n2 -- flag )
81         ;; flag is true if and only if n1 is less than n2.
82         pop rax
83         cmp rax,qword [rsp]
84         jl pop_true_next
85         jmp pop_false_next
86
87         WORD p_lessequal, '<=',fasm
88         ;; ( n1 n2 -- flag )
89         ;; flag is true if and only if n1 is less than n2.
90         pop rax
91         cmp rax,qword [rsp]
92         jle pop_true_next
93         jmp pop_false_next
94
95         WORD p_equal, '=',fasm
96         ;; ( x1 x2 -- flag )
97         ;; flag is true if and only if x1 is bit-for-bit the same as
98         ;; x2.
99         pop rax
100         cmp rax,qword [rsp]
101         je pop_true_next
102         jmp pop_false_next
103
104         WORD p_unequal, '!=',fasm
105         ;; ( x1 x2 -- flag )
106         ;; flag is true if and only if x1 is bit-for-bit the same as
107         ;; x2.
108         pop rax
109         cmp qword [rsp], rax
110         jne pop_true_next
111         jmp pop_false_next
112         
113         WORD p_greaterthan, '>',fasm
114         ;; ( n1 n2 -- flag )
115         ;; flag is true if and only if n1 is greater than n2.
116         pop rax
117         cmp rax,qword [rsp]
118         jg pop_true_next
119         jmp pop_false_next
120
121         WORD p_greaterequal, '>=',fasm
122         ;; ( n1 n2 -- flag )
123         ;; flag is true if and only if n1 is greater than n2.
124         pop rax
125         cmp rax,qword [rsp]
126         jge pop_true_next
127         jmp pop_false_next