Skip to content

Commit

Permalink
working on n>R, fixed pntUx pntUxSpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Rett Berg committed Oct 17, 2020
1 parent 6733b44 commit 955b091
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
57 changes: 41 additions & 16 deletions triforth.S
Expand Up @@ -233,7 +233,7 @@ panicStr: .ascii "!! PANIC ENCOUNTERED code=\0"
registersAdStr: .ascii "Registers e(a-d)x: \0"
registersDsStr: .ascii "e[ds]i: \0"
wordStr: .ascii "word: \0"
stackStr: .ascii "Stack< \0"
stackStr: .ascii "Stack<\0"
maxWordLenErrorStr: .ascii "Maximum word length exceeded\0"
_0xWordNeededStr: .ascii "Word needed for 0x\0"
_0xNotNumberStr: .ascii "Not a valid hex number: \0"
Expand Down Expand Up @@ -968,6 +968,12 @@ pntUx_zero:
call emit
ret

pntUxSpc:
call pntUx
dpush $' '
call emit
ret

ebx_toascii: # ( u -- c )
# convert a number (typically 1-16) into ascii equivalent
# This is used with pntUx (above)
Expand All @@ -990,11 +996,11 @@ ebx_toascii: # ( u -- c )

testpntUx:
dpush $1
call pntUx
call pntUxSpc
dpush $0x12
call pntUx
call pntUxSpc
dpush $0xDEADBEEF
call pntUx
call pntUxSpc
dpush $'\n'
call emit

Expand Down Expand Up @@ -1024,17 +1030,14 @@ testMUL:
pntStack: # ( -- )
# Forth .S, prints the current stack
dpush $stackStr
# "Stack< DEPTH > "
# "Stack<DEPTH> "
call pntSNt

call depth
call pntUx

dpush $'>'
call emit
dpush $' '
call emit

call depth # 4(%ebp) is depth
dpush $0 # (%ebp) is index
1:
Expand All @@ -1052,15 +1055,13 @@ pntStack: # ( -- )
# print the number in hex
movl (%ebx), %eax
dpush %eax
call pntUx
call pntUxSpc

addl $1, (%ebp) # increment index
jmp 1b # loop

2:
call _2drop
dpush $'\n'
call emit
ret

testPntStack:
Expand All @@ -1069,6 +1070,8 @@ testPntStack:
dpush $0xAA
dpush $0x1A2B
call pntStack
dpush $'\n'
call emit
call _2drop
call _2drop
dpush $testPntStackStr
Expand Down Expand Up @@ -1144,30 +1147,30 @@ dumpInfo:
movl $registerSnapshot, %ebx
movl (%ebx), %eax # eax
dpush %eax
call pntUx
call pntUxSpc

movl $registerSnapshot, %ebx
movl 4(%ebx), %eax # ebx
dpush %eax
call pntUx
call pntUxSpc

movl $registerSnapshot, %ebx
movl 8(%ebx), %eax # ecx
dpush %eax
call pntUx
call pntUxSpc

movl $registerSnapshot, %ebx
movl 12(%ebx), %eax # edx
dpush %eax
call pntUx
call pntUxSpc

movl $registerSnapshot, %ebx
dpush $registersDsStr
call pntSNt
movl $registerSnapshot, %ebx
movl 16(%ebx), %eax # edi
dpush %eax
call pntUx
call pntUxSpc

movl $registerSnapshot, %ebx
movl 20(%ebx), %eax # esi
Expand All @@ -1178,6 +1181,8 @@ dumpInfo:

# Stack
call pntStack
dpush $'\n'
call emit
# current word
dpush $wordStr
call pntSNt
Expand Down Expand Up @@ -1685,6 +1690,26 @@ def_asmword "R>", 2, 0, rpop
dpush %eax
NEXT

# ( aN aN-1 ... a0 N -- R: aN aN-1 ... a0 )
# \ put n values onto the return stack. This kind of operation is extremely
# \ unperformant in forth (requiring >12 calls) because it can be very hard
# \ to keep track of the values.
# ascii , len, flag, label
def_asmword "n>R", 3, 0, Nrpush
dpop %eax # N
call checkStackSize
call dbgexit
movl %eax, %ebx
shl $2, %ebx # ebx=ebx*4
movl %ebp, %ecx # cache dstack
addl %ebx, %ebp # move dstack pointer up (reduce dstack)
subl %ebx, %esp # move rstack pointer down (increase rstack)
dpush %ecx # src, previous dstack
dpush %esp # dst, new rstack
dpush %eax # N
callXt $xt_cellmove
NEXT

# ( -- addr ) \ get the return stack pointer
# ascii , len, flag, label
def_asmword "RSP@", 4, 0, rstackFETCH
Expand Down
5 changes: 3 additions & 2 deletions triforth.tf
Expand Up @@ -207,7 +207,7 @@ assertEmpty -test
: R@ ( -- u ) rsp@ cell + @ ; \ add cell to skip caller's address
: R@1 ( -- u ) rsp@ 0x 2 cells + @ ; \ 2 cells because we have to skip caller's address
: R@2 ( -- u ) rsp@ 0x 3 cells + @ ;
: 2>R ( u:a u:b -- R: a b ) IMM compile, swap compile, >R compile, >R ; \ R: ( -- a b)
: 2>R ( u:a u:b -- R: a b ) IMM compile, lit 0x 2 , compile, n>R ; \ R: ( -- a b)
\ TODO: Segfaults:
\ dumpInfo R@ RSP@ cell- cell- ! dumpInfo ( <- store caller's &code)
\ RSP@ cell- ! ( <-store b) RSP@ ! ( <-store a) RSP@ cell- cell- RSP! dbgexit ;
Expand Down Expand Up @@ -237,8 +237,9 @@ assertEmpty
: testR@ 0x 42 >R r@ 0x 42 assertEq R> 0x 42 assertEq ; testR@
: testR@1 0x 42 >R 0x 43 >R r@ 0x 43 assertEq r@1 0x 42 assertEq
R> 0x 43 assertEq R> 0x 42 assertEq assertEmpty ; testR@1
: test2>R 0x 42 0 2>R R@ 0 assertEq R@1 0x 42 assertEq
: test2>R 0x 42 0 0x 2 n>R R@ 0 dbgexit assertEq R@1 0x 42 assertEq
2R> 0 asserteq 0x 42 assertEq assertEmpty ; test2>R
dbgexit
: testR@2 0x 42 >R 0 >R 0 >R assertEmpty R@2 0x 42 assertEq 2Rdrop Rdrop
; testR@2
: test-R@ 0x 42 >R -R@ 0x 41 R> assertEq ; test-R@
Expand Down

0 comments on commit 955b091

Please sign in to comment.