expand to handle core block based streams
[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 [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 [rsp], rax
15         next
16
17         WORD p_mult, '*',fasm
18         ;; ( n1 n2 -- n3 )
19         ;; Multiply n1 by n2 giving the product n3.
20         ;; [rsp{8}] * [rsp+8{8}]
21         ;; dd00 = [rsp+4{4}]*[rsp+12{4}] ignored
22         ;; needs checking !!
23         ;; 
24         ;; 0cc0 = [rsp{4}]*[rsp+12{4}]
25         mov eax, dword [rsp]
26         imul dword [rsp+12]
27         mov ebx,eax
28         ;; 0bb0 = [rsp+4{4}]*[rsp+8{4}]
29         mov eax, dword [rsp+4]
30         imul dword [rsp+8]
31         add ebx, eax
32         ;; 00aa = [rsp{4}]*[rsp+8{4}]
33         mov eax, dword [rsp]
34         imul dword [rsp+8]
35         add ebx, edx
36         shl rbx,32
37         mov eax,eax             ; ensure zero-extending eax
38         add rax, rbx
39         push rax
40         next
41
42         WORD p_abs, 'ABS',fasm
43         ;; ( n -- u )
44         ;; u is the absolute value of n.
45         cmp qword [rsp],0
46         jge p_abs_end
47         neg qword [rsp]
48 p_abs_end:
49         next
50
51         WORD p_negate, 'NEGATE',fasm
52         ;; ( n1 -- n2 )
53         ;; Negate n1, giving its arithmetic inverse n2. 
54         xor rax,rax
55         sub rax,qword [rsp]
56         mov qword [rsp],rax
57         next
58
59