aforth

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

main.s (2131B)


      1 .include	"macros.i"
      2 
      3 # "REGISTERS"
      4 
      5 	.text
      6 
      7 	.set	TOS,	%r15
      8 	.set	TOSB,	%r15b
      9 	.set	SP,	%r14
     10 	.set	IP,	%r13
     11 	.set	WP,	%r12
     12 	.set	ACC,	%r11
     13 	.set	ACCB,	%r11b
     14 
     15 	.set	CMD,	%rax
     16 	.set	ARGA,	%rdi
     17 	.set	ARGB,	%rsi
     18 	.set	ARGC,	%rdx
     19 	.set	ARGD,	%r10
     20 	.set	ARGE,	%r8
     21 	.set	ARGF,	%r9
     22 
     23 # Kernel
     24 
     25 	.global _start
     26 
     27 _start:	movq	%rsp,	rspbk
     28 	mov	$_cold,	IP
     29 next:
     30 	mov	(IP),	WP
     31 	advance	IP
     32 	mov	$0x0000FFFFFFFFFFFF,	ACC
     33 	and	(WP),	ACC	# Strip flags off the codefield
     34 	jmp	*ACC
     35 
     36 enter:
     37 	push	IP
     38 	mov	WP,	IP
     39 	advance	IP
     40 	jmp	next
     41 
     42 # "DICTIONARY"
     43 
     44 	.data
     45 
     46 	.quad	0
     47 7:	strlit	"COLD"
     48 COLD:	forthword
     49 _cold:	do	RESETDATA
     50 	set	LAST	dictionaryhead
     51 	set	here	dictionaryend
     52 	escape	0
     53 	escape	96
     54 	saycr	"aFORTH alpha"
     55 	set	numtib	0
     56 	do	QUIT
     57 
     58 verb	forth	ABORT
     59 	do	RESETDATA
     60 	do	QUIT
     61 
     62 verb	forth	QUIT
     63 	do	RESETRETURN	# was commented out?
     64 	do	modeI
     65 2:	do	TIB
     66 	const	256	#80
     67 	escape	93
     68 	say	"> "
     69 	do	ACCEPT
     70 	escape	0
     71 	test	equal	0	1f
     72 		set	numtib
     73 		do	INTERPRET
     74 		goto	2b
     75 1:	escape	96
     76 	saycr	"Done."
     77 	escape	0
     78 	do	HALT
     79 
     80 .include "stack.s"
     81 .include "memory.s"
     82 .include "boolean.s"
     83 .include "maths.s"
     84 .include "input.s"
     85 .include "dictionary.s"
     86 .include "interpreter.s"
     87 .include "compiler.s"
     88 .include "output.s"
     89 .include "extras.s"
     90 
     91 
     92 # "CODEWORDS"
     93 
     94 verb	code	HALT
     95 _halt:	xor     ARGA,	ARGA	# default return code 0
     96 	sub	$stack, SP
     97 	jz	1f
     98 		mov	TOS,	ARGA
     99 1:	mov     $60,	CMD	# system call 60 is exit
    100 	syscall
    101 
    102 #	Do Stuff
    103 
    104 docon:		codeword
    105 	_dup
    106 	mov	(IP),	TOS
    107 	advance	IP
    108 	jmp	next
    109 
    110 dovar:		codeword
    111 	_dup
    112 	mov	IP,	TOS
    113 	advance	IP
    114 	jmp	next
    115 
    116 dostr:		codeword
    117 	_dup
    118 	mov	IP,	TOS
    119 	add	(IP),	IP
    120 	advance	IP
    121 	jmp	next
    122 
    123 dogoto:		codeword
    124 	mov	(IP),	IP
    125 	jmp	next
    126 
    127 dobranch:	codeword
    128 	minstk	1
    129 	cmp	$0,	TOS
    130 	je	__brk
    131 	mov	(IP),	IP
    132 	jmp	_drop
    133 __brk:	advance	IP
    134 	jmp	_drop
    135 
    136 dountil:	codeword
    137 	minstk	1
    138 	cmp	$0,	TOS
    139 	jne	__brk
    140 	sub	(IP),	IP
    141 	jmp	_drop
    142 
    143 #	Kernel
    144 
    145 verb	code	EXIT
    146 	pop	IP
    147 	jmp	next
    148 
    149 dictionaryhead:
    150 verb	code	EXECUTE
    151 	minstk	1
    152 	mov	TOS,	WP
    153 	mov	(SP),	TOS
    154 	advance	SP
    155 	mov	$0x0000FFFFFFFFFFFF,	ACC
    156 	and	(WP),	ACC	# Strip flags off the codefield
    157 	jmp	*ACC
    158 dictionaryend:
    159 
    160 # "STACK"
    161 
    162 	.skip	1048576
    163 stack:	.quad	0
    164 
    165 # "CORE VARIABLES"
    166 
    167 rspbk:	.quad	0
    168 
    169 buff:	.quad	0