Skip to content

Instantly share code, notes, and snippets.

@mgcaret
Last active November 9, 2021 19:41
Show Gist options
  • Save mgcaret/c8505fc29f21c296b8bd5f8bd1ce6b26 to your computer and use it in GitHub Desktop.
Save mgcaret/c8505fc29f21c296b8bd5f8bd1ce6b26 to your computer and use it in GitHub Desktop.
DOES> example/implementation in OF816.

References/notes: DOES> compilation semantics: https://github.com/mgcaret/of816/blob/master/asm/forth-dictionary.s#L5835-L5848

DOES> execution semantics: https://github.com/mgcaret/of816/blob/master/asm/forth-dictionary.s#L5797-L5831

JSL = jump to subroutine long, leaves a 24-bit return address on the return stack

Execution semantics for some words have headers omitted in OF816 to save memory. SEE replaces the name with ^address.

Each word starts with machine code. All secondaries start with a JSL opcode (0x22)

OF816 by M.G.

: dodoes ." doing does!" does> ." code after does" ;
 OK
see dodoes
Flags: 0
: dodoes             \ first JSL ENTER is replaced by : <name> by SEE, start executing Forth code
81058B "doing does!" \ pretty printing by SEE
81059E TYPE
8105A2 ^2001CE       \ ;CODE - exit interpreter and resume machine code execution
8105A6 ^204E4022     \ JSL to DOES> execution semantics at 204E40
8105AA ^20016E22     \ JSL to ENTER at 20016E - start executing Forth code
8105AE ^202389       \ RPLUCKADDR ( R: 24-bit-addr 32-bit-addr -- extended-24-bit-addr | R: 32-bit-addr )
8105B2 1+            \ 65xx return addresses on the stack are one less than the real address
8105B6 "code after does"
8105CD TYPE
; OK
create foo
 OK
' foo 1+ @ U.        \ we can't SEE word that doesn't start with ENTER... so look at the first cell
2002FB22  OK         \ this is a JSL to the executon semantecs of a CREATE or VARIABLE word, a routine at 2002FB.
dodoes
doing does! OK
' foo 1+ @ u.
8105AA22  OK.       \ this is a JSL to 8105AA, which we can see is the ENTER in up above in dodoes
foo
code after does OK

Note that there is a double JSL, first from the CREATEd word, then to the ENTER in the DOES> portion of dodoes. The address of the word body is at the return address of the first JSL. ENTER swaps the 24-bit machine return address at the top of the return stack for the 32-bit Forth return address. So after ENTER there's a 32-bit address at the top of the return stack, followed by the 24-bit machine return address that happens to point to the body of the CREATEd word (less 1). RPLUCKADDR pulls that 24-bit address out of the machine stack and puts it into the data stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment