slight polishing
[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_negate, 'NEGATE',fasm
36         ;; ( n1 -- n2 )
37         ;; Negate n1, giving its arithmetic inverse n2. 
38         xor rax,rax
39         sub rax,qword [rsp]
40         mov qword [rsp],rax
41         next
42
43         WORD p_divmod,'/MOD',fasm
44         ;; ( x y -- q r )
45         ;; divide signed x/y giving quotient q and remainder r
46         pop rbx
47         pop rax
48         xor rdx,rdx
49         idiv rbx
50         push rax
51         push rdx
52         next
53
54         WORD p_div,'/',fasm
55         ;; ( x y -- q )
56         ;; divide signed x/y giving quotient q and discard remainder
57         pop rbx
58         pop rax
59         xor rdx,rdx
60         idiv rbx
61         push rax
62         next
63