home .. forth .. colorforth mail list archive ..

[colorforth] Segments and colorforth


Hi folks,

A segment in an assembler context is a range of memory
with a common location pointer.  2]
This is indispensible in more complicated assembly programming,
and disassembling e.g. Windows executables.
In behalf of colorforth and those other applications I have now added
segments to my ciasdis tool.

In assembling a colorforth definition data is added in three places,
in the normal executable code, in the huffman table, and in the
execution token table.

So the natural way to define ``emit'' in an assembler that features
segments is use three segments, like this:

<x> <y> <z> SEGMENT ForthHuff:
<a> <b> <c> SEGMENT ForthXT:
<k> <l> <m> SEGMENT Default:

....
:emit   ( 3] )
    ForthHuff: dl "emit" >CN    ( 1] )
    ForthXT:   dl emit
    default:
    call qcr
    push ESI
    push EDI
    push EDX
     imul EAX, 16*24/8
     lea  ESI, icons[EAX]
     call clip
     mov  EDX, fore
     mov  ECX, 24
     ...

With a suitable definition for ForthHeader,
this becomes

...
ForthHeader emit
    call qcr
    push ESI
    push EDI
    push EDX
     imul EAX, 16*24/8
     lea  ESI, icons[EAX]
     call clip
     mov  EDX, fore
     mov  ECX, 24
     ...


There is another set of segments needed for MacroHeader.

Within the context of ciasdis we can now define ForthHeader like this:

: ForthHeader
    NAME   "X_" PAD $!   2DUP PAD $+!  \ PAD now contains "X_emit"
    _AP_   ForthXT: AS-,               \ Assemble default program counter
    _AP_ ROT ROT LABELED               \ Add emit to the assembler labels.
    PAD $@ >CN ForthHuff: AS-,         \ Assemble colorforth name
    Default:                           \ Back to code segment
;

It may not be a metacompiler, but it is substantially less
cumbersome than

       ...
emit:
       ...
       dd (((4 shl 5+21o)shl 4+7)shl 4+2)shl 15 ; emit
       ..
       dd   ... emit ...        \ At the right place!
       ..


Adding segments took one evening, not bad. First I collected the
target start address, the buffer address and the program counter into
a segment class, and defined one object of that class. It still worked.
Then I added a bag to register all objects of the class segment, and
code in the end to write out all segments looping over the bag. It
still worked.
A segment has an extra property that was not present before. First
there was one codespace and it was implied that it would be written at
offset zero in the file. Now each segment has a method (my Forth
classes only have methods, no fields) that gives the file offset where
it has to be written.

1] >CN turns an address count string pair into a colorforth name, i.e.
one machineword.
2] This is different than Intel segments, though not totally
unrelated.
3] :name defines label ``name'' in ciasdis

Note:
This is very recent. There are no examples of this on my site yet.
The code given here has not been tested. Adapting the crawler to take
advantage of segments is still to be done.

Groetjes Albert

---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com