aforth

FORTH for Linux x86-64, written in assembly.
git clone git://henryandlizzy.uk/aforth
Log | Files | Refs | README

maths.s (1407B)


      1 #	Maths
      2 
      3 verb	code	plus	"+"
      4 	minstk	2
      5 	add	TOS,	(SP)
      6 	jmp	_drop
      7 
      8 verb	code	minus	"-"
      9 	minstk	2
     10 	sub	TOS,	(SP)
     11 	jmp	_drop
     12 
     13 verb	code	inc	"1+"
     14 	minstk	1
     15 	inc	TOS
     16 	jmp	next
     17 
     18 verb	code	dec	"1-"
     19 	minstk	1
     20 	dec	TOS
     21 	jmp	next
     22 
     23 verb	code	incaddr	"@1+"
     24 	minstk	1
     25 	incq	(TOS)
     26 	jmp	_drop
     27 
     28 verb	code	decaddr	"@1-"
     29 	minstk	1
     30 	decq	(TOS)
     31 	jmp	_drop
     32 
     33 verb	code	NEGATE
     34 	minstk	1
     35 	neg	TOS
     36 	jmp	next
     37 
     38 verb	code	mult	"*"
     39 	minstk	2
     40 	mov	(SP),	%rax
     41 	mul	TOS
     42 	mov	%rax,	(SP)
     43 	jmp	_drop
     44 
     45 verb	code	divmod	"/%"
     46 	minstk	2
     47 	xor	%rdx,	%rdx
     48 	mov	(SP),	%rax
     49 	div	TOS
     50 	mov	%rax,	(SP)
     51 	mov	%rdx,	TOS
     52 	jmp	next
     53 
     54 verb	forth	divide	"/"
     55 	do	divmod
     56 	do	DROP
     57 	endword
     58 
     59 verb	forth	mod	"%"
     60 	do	divmod
     61 	do	SWAP
     62 	do	DROP
     63 	endword
     64 
     65 verb	code	muldivmod	"*/%"
     66 	minstk	3
     67 	mov	(SP),	%rax
     68 	mul	TOS
     69 	divq	-8(SP)
     70 	mov	%rax,	-8(SP)
     71 	mov	%rdx,	(SP)
     72 	jmp	_drop
     73 
     74 verb	forth	muldiv	"*/"
     75 	do	muldivmod
     76 	do	DROP
     77 	endword
     78 
     79 #	Functions
     80 
     81 verb	forth	MIN
     82 	do	dup2
     83 	do	less
     84 	if	1f
     85 		do	SWAP
     86 1:	do	DROP
     87 	endword
     88 
     89 verb	forth	MAX
     90 	do	dup2
     91 	do	greater
     92 	if	1f
     93 		do	SWAP
     94 1:	do	DROP
     95 	endword
     96 
     97 #	Comparison
     98 
     99 verb	forth	iszero	"0="
    100 	do	FALSE
    101 	do	equal
    102 	endword
    103 
    104 truecmp:
    105 	movq	$-1,	(SP)
    106 	jmp	_drop
    107 
    108 compare	je	equal	"\="
    109 compare	jne	nequal	"<>"
    110 compare	jg	greater	">"
    111 compare	jl	less	"<"
    112 compare	jge	gequal	">="
    113 compare	jle	lequal	"<="
    114 compare	ja	above	"S>"
    115 compare	jb	below	"S<"
    116 compare	jae	aequal	"S>="
    117 compare	jbe	bequal	"S<="
    118 
    119 cmpaddr	je	indeq	"@="
    120 cmpaddr	jne	indneq	"@<>"