more and better documentation and some reorganisation
[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',dovalue
31         ;; ( -- 0 )
32         ;; Push a false flag, 0.
33         dq 0
34
35         WORD p_true, 'TRUE',dovalue
36         ;; ( -- true )
37         ;; Return a true flag, -1. (non-zero)
38         dq -1
39
40 pop_false_next:
41         mov qword [rsp],0
42         next
43
44 pop_true_next:
45         mov qword [rsp],-1
46         next
47
48         WORD p_within, 'WITHIN',fasm
49         ;; ( n1 n2 n3 -- flag )
50         ;; Push true if n2 <= n1 and n1 < n3 and false otherwise.
51         xor rcx,rcx
52         pop rax
53         pop rbx
54         cmp qword [rsp],rbx
55         jl p_within_not
56         cmp qword [rsp],rax
57         jge p_within_not
58         not rcx
59 p_within_not:
60         next
61         
62         WORD p_0less, '0<',fasm
63         ;; ( n -- flag )
64         ;; flag is true (non-zero) if and only if n is less than zero.
65         pop rax
66         cmp rax,qword [rsp]
67         jl pop_true_next
68         jmp pop_false_next
69
70         WORD p_0equal, '0=',fasm
71         ;; ( x -- flag )
72         ;; flag is true if x is equal to zero otherwise false.
73         cmp qword [rsp],0
74         je pop_true_next
75         jmp pop_false_next
76         
77         WORD p_lessthan, '<',fasm
78         ;; ( n1 n2 -- flag )
79         ;; flag is true if and only if n1 is less than n2.
80         pop rax
81         cmp rax,qword [rsp]
82         jl pop_true_next
83         jmp pop_false_next
84
85         WORD p_lessequal, '<=',fasm
86         ;; ( n1 n2 -- flag )
87         ;; flag is true if and only if n1 is less than n2.
88         pop rax
89         cmp rax,qword [rsp]
90         jle pop_true_next
91         jmp pop_false_next
92
93         WORD p_equal, '=',fasm
94         ;; ( x1 x2 -- flag )
95         ;; flag is true if and only if x1 is bit-for-bit the same as
96         ;; x2.
97         pop rax
98         cmp rax,qword [rsp]
99         je pop_true_next
100         jmp pop_false_next
101
102         WORD p_unequal, '!=',fasm
103         ;; ( x1 x2 -- flag )
104         ;; flag is true if and only if x1 is bit-for-bit the same as
105         ;; x2.
106         pop rax
107         cmp qword [rsp], rax
108         jne pop_true_next
109         jmp pop_false_next
110         
111         WORD p_greaterthan, '>',fasm
112         ;; ( n1 n2 -- flag )
113         ;; flag is true if and only if n1 is greater than n2.
114         pop rax
115         cmp rax,qword [rsp]
116         jg pop_true_next
117         jmp pop_false_next
118
119         WORD p_greaterequal, '>=',fasm
120         ;; ( n1 n2 -- flag )
121         ;; flag is true if and only if n1 is greater than n2.
122         pop rax
123         cmp rax,qword [rsp]
124         jge pop_true_next
125         jmp pop_false_next