ithread.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ;; Entrypoints are addresses referencing some code. They can reference native and directly
  2. ;; runnable code or a interpretable word. In the case of directly runnable code, they are meant
  3. ;; to internal interpreter use. They can be call-able (you start its execution with a `call`
  4. ;; instruction, they return to your code with a 'ret' instruction) or jmp-able (you start it
  5. ;; with a 'jmp' instruction, they will continue to execute internal interpreter's code).
  6. ;; EBP holds the return stack top. ESP holds the data stack top.
  7. _pushrst: ;Native call-able entrypoint to push ESI onto return stack
  8. lea ebp, [ebp-4]
  9. mov [ebp], esi
  10. ret
  11. _poprst: ;Native call-able entrypoint to pop the return stack into ESI
  12. mov esi, [ebp]
  13. lea ebp, [ebp+4]
  14. ret
  15. ;; Words are (potentially) user-accessible sets of instructions. Every word has a entrypoint and
  16. ;; a definition (its contents). They can be run by the interpreter, can call or be called by
  17. ;; other words through the interpreter (which supports the word's code controlling the return
  18. ;; stack for proper operation) and can be referenced at a dictionary.
  19. ;; Codewords are special entrypoints used as a pointer in the beggining of the definition of
  20. ;; every word. They implement the specific interpreter code needed to execute that word.
  21. _enter_native: ;Codeword for native words: words made of native machine code
  22. jmp esi
  23. _leave_routine: ;Word to end a virtual routine
  24. dd _enter_native
  25. _end_caller: ;Jmp-able entrypoint to end the caller routine
  26. call _poprst
  27. _next_routine: ;Jmp-able entrypoint to end the current native routine
  28. call _poprst
  29. add esi, 4
  30. _enter_routine: ;Codeword for virtual words: words made of other words entrypoints
  31. call _pushrst
  32. lodsd
  33. mov esi, eax
  34. _next: ;Entrypoint to jump into the next word's codeword
  35. lodsd
  36. jmp eax
  37. _halt:
  38. dd _enter_native
  39. mov eax, sys_exit
  40. mov ebx, 0
  41. int 80h
  42. ;; `_literal` word gets the value that should be the next word to be run in the current routine
  43. ;; and pushes this value at the data stack, skipping its execution.
  44. _literal:
  45. dd _enter_native
  46. add dword [ebp], 4 ;Return stack top points at the next word to be run
  47. mov eax, [ebp]
  48. push dword [eax]
  49. jmp _next_routine