forth: consolidation
This commit is contained in:
parent
f4b969986d
commit
ac914c3847
131
forth/forth.asm
131
forth/forth.asm
@ -168,12 +168,11 @@ INTERPRET:
|
|||||||
.dw FIND_
|
.dw FIND_
|
||||||
.dw CSKIP
|
.dw CSKIP
|
||||||
.dw FBR
|
.dw FBR
|
||||||
.db 34
|
.db 32
|
||||||
; It's a word, execute it
|
; It's a word, execute it
|
||||||
.dw FLAGS_
|
.dw FLAGS_
|
||||||
.dw FETCH
|
.dw FETCH
|
||||||
.dw NUMBER
|
.dw ONE ; Bit 0 on
|
||||||
.dw 0x0001 ; Bit 0 on
|
|
||||||
.dw OR
|
.dw OR
|
||||||
.dw FLAGS_
|
.dw FLAGS_
|
||||||
.dw STORE
|
.dw STORE
|
||||||
@ -186,11 +185,11 @@ INTERPRET:
|
|||||||
.dw FLAGS_
|
.dw FLAGS_
|
||||||
.dw STORE
|
.dw STORE
|
||||||
.dw BBR
|
.dw BBR
|
||||||
.db 41
|
.db 39
|
||||||
; FBR mark, try number
|
; FBR mark, try number
|
||||||
.dw PARSEI
|
.dw PARSEI
|
||||||
.dw BBR
|
.dw BBR
|
||||||
.db 46
|
.db 44
|
||||||
; infinite loop
|
; infinite loop
|
||||||
|
|
||||||
; *** Collapse OS lib copy ***
|
; *** Collapse OS lib copy ***
|
||||||
@ -212,12 +211,6 @@ intoHL:
|
|||||||
pop de
|
pop de
|
||||||
ret
|
ret
|
||||||
|
|
||||||
intoDE:
|
|
||||||
ex de, hl
|
|
||||||
call intoHL
|
|
||||||
ex de, hl ; de preserved by intoHL, so no push/pop needed
|
|
||||||
ret
|
|
||||||
|
|
||||||
; add the value of A into HL
|
; add the value of A into HL
|
||||||
; affects carry flag according to the 16-bit addition, Z, S and P untouched.
|
; affects carry flag according to the 16-bit addition, Z, S and P untouched.
|
||||||
addHL:
|
addHL:
|
||||||
@ -231,22 +224,13 @@ addHL:
|
|||||||
; Copy string from (HL) in (DE), that is, copy bytes until a null char is
|
; Copy string from (HL) in (DE), that is, copy bytes until a null char is
|
||||||
; encountered. The null char is also copied.
|
; encountered. The null char is also copied.
|
||||||
; HL and DE point to the char right after the null char.
|
; HL and DE point to the char right after the null char.
|
||||||
strcpyM:
|
strcpy:
|
||||||
ld a, (hl)
|
ld a, (hl)
|
||||||
ld (de), a
|
ld (de), a
|
||||||
inc hl
|
inc hl
|
||||||
inc de
|
inc de
|
||||||
or a
|
or a
|
||||||
jr nz, strcpyM
|
jr nz, strcpy
|
||||||
ret
|
|
||||||
|
|
||||||
; Like strcpyM, but preserve HL and DE
|
|
||||||
strcpy:
|
|
||||||
push hl
|
|
||||||
push de
|
|
||||||
call strcpyM
|
|
||||||
pop de
|
|
||||||
pop hl
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
; Compares strings pointed to by HL and DE until one of them hits its null char.
|
; Compares strings pointed to by HL and DE until one of them hits its null char.
|
||||||
@ -273,20 +257,20 @@ strcmp:
|
|||||||
; early, set otherwise)
|
; early, set otherwise)
|
||||||
ret
|
ret
|
||||||
|
|
||||||
; Compares strings pointed to by HL and DE up to A count of characters. If
|
; Compares strings pointed to by HL and DE up to NAMELEN count of characters. If
|
||||||
; equal, Z is set. If not equal, Z is reset.
|
; equal, Z is set. If not equal, Z is reset.
|
||||||
strncmp:
|
strncmp:
|
||||||
push bc
|
push bc
|
||||||
push hl
|
push hl
|
||||||
push de
|
push de
|
||||||
|
|
||||||
ld b, a
|
ld b, NAMELEN
|
||||||
.loop:
|
.loop:
|
||||||
ld a, (de)
|
ld a, (de)
|
||||||
cp (hl)
|
cp (hl)
|
||||||
jr nz, .end ; not equal? break early. NZ is carried out
|
jr nz, .end ; not equal? break early. NZ is carried out
|
||||||
; to the called
|
; to the called
|
||||||
cp 0 ; If our chars are null, stop the cmp
|
or a ; If our chars are null, stop the cmp
|
||||||
jr z, .end ; The positive result will be carried to the
|
jr z, .end ; The positive result will be carried to the
|
||||||
; caller
|
; caller
|
||||||
inc hl
|
inc hl
|
||||||
@ -414,17 +398,6 @@ parseDecimal:
|
|||||||
ret
|
ret
|
||||||
|
|
||||||
; *** Support routines ***
|
; *** Support routines ***
|
||||||
; Sets Z if (HL) == E and (HL+1) == D
|
|
||||||
HLPointsDE:
|
|
||||||
ld a, (hl)
|
|
||||||
cp e
|
|
||||||
ret nz ; no
|
|
||||||
inc hl
|
|
||||||
ld a, (hl)
|
|
||||||
dec hl
|
|
||||||
cp d ; Z has our answer
|
|
||||||
ret
|
|
||||||
|
|
||||||
; Find the entry corresponding to word where (HL) points to and sets DE to
|
; Find the entry corresponding to word where (HL) points to and sets DE to
|
||||||
; point to that entry.
|
; point to that entry.
|
||||||
; Z if found, NZ if not.
|
; Z if found, NZ if not.
|
||||||
@ -440,7 +413,6 @@ find:
|
|||||||
ex de, hl
|
ex de, hl
|
||||||
sbc hl, bc
|
sbc hl, bc
|
||||||
ex de, hl ; We're good, DE points to word name
|
ex de, hl ; We're good, DE points to word name
|
||||||
ld a, NAMELEN
|
|
||||||
call strncmp
|
call strncmp
|
||||||
pop de ; <-- lvl 1, return to wordref
|
pop de ; <-- lvl 1, return to wordref
|
||||||
jr z, .end ; found
|
jr z, .end ; found
|
||||||
@ -457,7 +429,9 @@ find:
|
|||||||
; Z is set if DE point to 0 (no entry). NZ if not.
|
; Z is set if DE point to 0 (no entry). NZ if not.
|
||||||
.prev:
|
.prev:
|
||||||
dec de \ dec de \ dec de ; prev field
|
dec de \ dec de \ dec de ; prev field
|
||||||
call intoDE
|
ex de, hl
|
||||||
|
call intoHL
|
||||||
|
ex de, hl ; de preserved by intoHL, so no push/pop needed
|
||||||
; DE points to prev. Is it zero?
|
; DE points to prev. Is it zero?
|
||||||
xor a
|
xor a
|
||||||
or d
|
or d
|
||||||
@ -637,7 +611,6 @@ numberWord:
|
|||||||
push de
|
push de
|
||||||
jp next
|
jp next
|
||||||
|
|
||||||
.db 0b10 ; Flags
|
|
||||||
NUMBER:
|
NUMBER:
|
||||||
.dw numberWord
|
.dw numberWord
|
||||||
|
|
||||||
@ -652,7 +625,6 @@ litWord:
|
|||||||
ld (IP), hl
|
ld (IP), hl
|
||||||
jp next
|
jp next
|
||||||
|
|
||||||
.db 0b10 ; Flags
|
|
||||||
LIT:
|
LIT:
|
||||||
.dw litWord
|
.dw litWord
|
||||||
|
|
||||||
@ -674,8 +646,7 @@ EXIT:
|
|||||||
.db 0
|
.db 0
|
||||||
QUIT:
|
QUIT:
|
||||||
.dw compiledWord
|
.dw compiledWord
|
||||||
.dw NUMBER
|
.dw ZERO
|
||||||
.dw 0
|
|
||||||
.dw FLAGS_
|
.dw FLAGS_
|
||||||
.dw STORE
|
.dw STORE
|
||||||
.dw .private
|
.dw .private
|
||||||
@ -961,7 +932,7 @@ SCPY:
|
|||||||
.dw nativeWord
|
.dw nativeWord
|
||||||
pop hl
|
pop hl
|
||||||
ld de, (HERE)
|
ld de, (HERE)
|
||||||
call strcpyM
|
call strcpy
|
||||||
ld (HERE), de
|
ld (HERE), de
|
||||||
jp next
|
jp next
|
||||||
|
|
||||||
@ -1090,8 +1061,7 @@ ISWS:
|
|||||||
.dw NUMBER
|
.dw NUMBER
|
||||||
.dw 33
|
.dw 33
|
||||||
.dw CMP
|
.dw CMP
|
||||||
.dw NUMBER
|
.dw ONE
|
||||||
.dw 1
|
|
||||||
.dw PLUS
|
.dw PLUS
|
||||||
.dw NOT
|
.dw NOT
|
||||||
.dw EXIT
|
.dw EXIT
|
||||||
@ -1139,27 +1109,27 @@ TOWORD:
|
|||||||
.db 0
|
.db 0
|
||||||
WORD:
|
WORD:
|
||||||
.dw compiledWord
|
.dw compiledWord
|
||||||
.dw WORDBUF_ ; ( a )
|
.dw NUMBER ; ( a )
|
||||||
|
.dw WORDBUF
|
||||||
.dw TOWORD ; ( a c )
|
.dw TOWORD ; ( a c )
|
||||||
; branch mark
|
; branch mark
|
||||||
.dw OVER ; ( a c a )
|
.dw OVER ; ( a c a )
|
||||||
.dw STORE ; ( a )
|
.dw STORE ; ( a )
|
||||||
.dw NUMBER ; ( a 1 )
|
.dw ONE ; ( a 1 )
|
||||||
.dw 1
|
|
||||||
.dw PLUS ; ( a+1 )
|
.dw PLUS ; ( a+1 )
|
||||||
.dw CIN ; ( a c )
|
.dw CIN ; ( a c )
|
||||||
.dw DUP ; ( a c c )
|
.dw DUP ; ( a c c )
|
||||||
.dw ISWS ; ( a c f )
|
.dw ISWS ; ( a c f )
|
||||||
.dw CSKIP ; ( a c )
|
.dw CSKIP ; ( a c )
|
||||||
.dw BBR
|
.dw BBR
|
||||||
.db 20 ; here - mark
|
.db 18 ; here - mark
|
||||||
; at this point, we have ( a WS )
|
; at this point, we have ( a WS )
|
||||||
.dw DROP
|
.dw DROP
|
||||||
.dw NUMBER
|
.dw ZERO
|
||||||
.dw 0
|
|
||||||
.dw SWAP ; ( 0 a )
|
.dw SWAP ; ( 0 a )
|
||||||
.dw STORE ; ()
|
.dw STORE ; ()
|
||||||
.dw WORDBUF_
|
.dw NUMBER
|
||||||
|
.dw WORDBUF
|
||||||
.dw EXIT
|
.dw EXIT
|
||||||
|
|
||||||
.wcpy:
|
.wcpy:
|
||||||
@ -1237,7 +1207,7 @@ ENTRYHEAD:
|
|||||||
pop hl
|
pop hl
|
||||||
ld de, (HERE)
|
ld de, (HERE)
|
||||||
call strcpy
|
call strcpy
|
||||||
ex de, hl ; (HERE) now in HL
|
ld hl, (HERE)
|
||||||
ld de, (CURRENT)
|
ld de, (CURRENT)
|
||||||
ld a, NAMELEN
|
ld a, NAMELEN
|
||||||
call addHL
|
call addHL
|
||||||
@ -1319,17 +1289,9 @@ PARSEPTR_:
|
|||||||
.dw sysvarWord
|
.dw sysvarWord
|
||||||
.dw PARSEPTR
|
.dw PARSEPTR
|
||||||
|
|
||||||
.db "(wbuf)"
|
|
||||||
.fill 1
|
|
||||||
.dw PARSEPTR_
|
|
||||||
.db 0
|
|
||||||
WORDBUF_:
|
|
||||||
.dw sysvarWord
|
|
||||||
.dw WORDBUF
|
|
||||||
|
|
||||||
.db "FLAGS"
|
.db "FLAGS"
|
||||||
.fill 2
|
.fill 2
|
||||||
.dw WORDBUF_
|
.dw PARSEPTR_
|
||||||
.db 0
|
.db 0
|
||||||
FLAGS_:
|
FLAGS_:
|
||||||
.dw sysvarWord
|
.dw sysvarWord
|
||||||
@ -1674,10 +1636,34 @@ XOR:
|
|||||||
push hl
|
push hl
|
||||||
jp next
|
jp next
|
||||||
|
|
||||||
|
; It might look peculiar to have specific words for "0" and "1", but although
|
||||||
|
; it slightly beefs ups the ASM part of the binary, this one-byte-save-per-use
|
||||||
|
; really adds up when we compare total size.
|
||||||
|
|
||||||
|
.db "0"
|
||||||
|
.fill 6
|
||||||
|
.dw XOR
|
||||||
|
.db 0
|
||||||
|
ZERO:
|
||||||
|
.dw nativeWord
|
||||||
|
ld hl, 0
|
||||||
|
push hl
|
||||||
|
jp next
|
||||||
|
|
||||||
|
.db "1"
|
||||||
|
.fill 6
|
||||||
|
.dw ZERO
|
||||||
|
.db 0
|
||||||
|
ONE:
|
||||||
|
.dw nativeWord
|
||||||
|
ld hl, 1
|
||||||
|
push hl
|
||||||
|
jp next
|
||||||
|
|
||||||
; ( a1 a2 -- b )
|
; ( a1 a2 -- b )
|
||||||
.db "SCMP"
|
.db "SCMP"
|
||||||
.fill 3
|
.fill 3
|
||||||
.dw XOR
|
.dw ONE
|
||||||
.db 0
|
.db 0
|
||||||
SCMP:
|
SCMP:
|
||||||
.dw nativeWord
|
.dw nativeWord
|
||||||
@ -1721,16 +1707,16 @@ CSKIP:
|
|||||||
jp z, next ; False, do nothing.
|
jp z, next ; False, do nothing.
|
||||||
ld hl, (IP)
|
ld hl, (IP)
|
||||||
ld de, NUMBER
|
ld de, NUMBER
|
||||||
call HLPointsDE
|
call .HLPointsDE
|
||||||
jr z, .isNum
|
jr z, .isNum
|
||||||
ld de, FBR
|
ld de, FBR
|
||||||
call HLPointsDE
|
call .HLPointsDE
|
||||||
jr z, .isBranch
|
jr z, .isBranch
|
||||||
ld de, BBR
|
ld de, BBR
|
||||||
call HLPointsDE
|
call .HLPointsDE
|
||||||
jr z, .isBranch
|
jr z, .isBranch
|
||||||
ld de, LIT
|
ld de, LIT
|
||||||
call HLPointsDE
|
call .HLPointsDE
|
||||||
jr nz, .isWord
|
jr nz, .isWord
|
||||||
; We have a literal
|
; We have a literal
|
||||||
inc hl \ inc hl
|
inc hl \ inc hl
|
||||||
@ -1752,6 +1738,17 @@ CSKIP:
|
|||||||
ld (IP), hl
|
ld (IP), hl
|
||||||
jp next
|
jp next
|
||||||
|
|
||||||
|
; Sets Z if (HL) == E and (HL+1) == D
|
||||||
|
.HLPointsDE:
|
||||||
|
ld a, (hl)
|
||||||
|
cp e
|
||||||
|
ret nz ; no
|
||||||
|
inc hl
|
||||||
|
ld a, (hl)
|
||||||
|
dec hl
|
||||||
|
cp d ; Z has our answer
|
||||||
|
ret
|
||||||
|
|
||||||
; This word's atom is followed by 1b *relative* offset (to the cell's addr) to
|
; This word's atom is followed by 1b *relative* offset (to the cell's addr) to
|
||||||
; where to branch to. For example, The branching cell of "IF THEN" would
|
; where to branch to. For example, The branching cell of "IF THEN" would
|
||||||
; contain 3. Add this value to RS.
|
; contain 3. Add this value to RS.
|
||||||
|
Loading…
Reference in New Issue
Block a user