Search code examples
forthgforth

How to dump a word in Forth?


The aim is to create a Forth word in the dictionary and then print out the memory content of that word. The first step is easy. The : main word is created like described in tutorial. After creating the word, the dictionary pointer is moved to the next position, so I have to subtract some items from it and now it's possible to dump out the memory area in which the word was created:

\ create a dummy routine
: main 1 dup + . ;

\ put dictionary pointer to the stack
\ and subtract 50 indizies
dp 50 - 

\ print out 20 cells to the screen from dp-50 until dp+50
\ but where is my newly created routine ": main"?
100 dump

bye
------
gforth "a.fs"

7F81DF443FC6: 00 00 20 17  12 AF 60 55 - 00 00 04 00  00 00 00 00  .. ...`U........
7F81DF443FD6: 00 00 A0 58  14 AF 60 55 - 00 00 10 00  00 00 00 00  ...X..`U........
7F81DF443FE6: 00 00 FF FF  FF FF FF FF - FF FF 00 00  00 00 00 00  ................
7F81DF443FF6: 00 00 50 82  49 DF 81 7F - 00 00 F8 3F  44 DF 81 7F  ..P.I......?D...
7F81DF444006: 00 00 08 82  49 DF 81 7F - 00 00 F0 81  49 DF 81 7F  ....I.......I...
7F81DF444016: 00 00 32 00  00 00 00 00 - 00 00 00 00  00 00 00 00  ..2.............
7F81DF444026: 00 00 00 00 

Unfortunately, the memory region shown by the dump command doesn't have a section which starts with : main and the statements inside the word 1 dup ... are also missing. So where exactly is the newly created word? How can I see the raw bytes in the memory?


Solution

  • 1) There is a predefined word see which should do what you want and looking into the respective sources should give you a hint.

    2) Your approach only works for a certain memory model, i. e. putting names and code into the same region, which is far from guaranteed. (I'm not sure, whether it would work for Gforth). From dup, 1, +, etc. you would recognize nothing, since they are replaced by the corresponding addresses or call instructions.