added max and min
[rrq/rrqforth.git] / math.asm
1 ;;; Words doing arithmetics
2         
3         WORD p_plus, '+',fasm
4         ;; ( n1 n2 -- n3 )
5         ;; n3 is the sum of n1 and n2
6         pop rax
7         add qword [rsp],rax
8         next
9
10         WORD p_minus, '-',fasm
11         ;; ( n1 n2 -- n3 )
12         ;; n3 is the result of subtracting n2 from n1
13         pop rax
14         sub qword [rsp], rax
15         next
16
17         WORD p_mult, '*',fasm
18         ;; ( n1 n2 -- n3 )
19         ;; multiply n1 * n2 to n3 ignoring overflow
20         pop rax
21         pop rbx
22         imul rax,rbx
23         push rax
24         next
25
26         WORD p_abs, 'ABS',fasm
27         ;; ( n -- u )
28         ;; u is the absolute value of n.
29         cmp qword [rsp],0
30         jge p_abs_end
31         neg qword [rsp]
32 p_abs_end:
33         next
34
35         WORD p_max, 'MAX',fasm
36         ;; ( a b -- c )
37         ;; c is the least a and b
38         pop rax
39         cmp rax,qword [rsp]
40         jle p_max_end
41         mov qword [rsp],rax
42 p_max_end:
43         next
44
45         WORD p_min, 'MIN',fasm
46         ;; ( a b -- c )
47         ;; c is the least a and b
48         pop rax
49         cmp rax,qword [rsp]
50         jge p_min_end
51         mov qword [rsp],rax
52 p_min_end:
53         next
54
55         WORD p_negate, 'NEGATE',fasm
56         ;; ( n1 -- n2 )
57         ;; Negate n1, giving its arithmetic inverse n2. 
58         xor rax,rax
59         sub rax,qword [rsp]
60         mov qword [rsp],rax
61         next
62
63         WORD p_divmod,'/MOD',fasm
64         ;; ( x y -- q r )
65         ;; divide signed x/y giving quotient q and remainder r
66         pop rbx
67         pop rax
68         xor rdx,rdx
69         idiv rbx
70         push rax
71         push rdx
72         next
73
74         WORD p_div,'/',fasm
75         ;; ( x y -- q )
76         ;; divide signed x/y giving quotient q and discard remainder
77         pop rbx
78         pop rax
79         xor rdx,rdx
80         idiv rbx
81         push rax
82         next
83