Autres articles / Other articles

The extended assembler for FlashForth

published: 30 April 2021 / updated 1 May 2021

Lire cette page en français

 


The version of the FORTH assembler for AVR used in metacompilation has features allowing to clone certain directives and operators specific to the pure assembler.

The AVR macro-assembler code is available here:
FlashForth assembler for Atmega chips

Creating macro instructions

In the previous article, we discussed assembly guidelines including macro-instruction definition:

r24 .def tosl
r25 .def tosh
\ Macros
.macro poptos 
    tosl Y+ ld, 
    tosh Y+ ld, 
.endmacro

A macro-instruction makes it possible to group a series of instructions into a super instruction. A macro-instruction can also adapt the assembly according to certain settings.

Here is the out_ macro as defined in assembler to build FlashForth:

.macro out_ 
.if (@0 < $40)
  out @0,@1
.else
  sts @0,@1
.endif
.endmacro

We will use the local variables of gForth to write the macro in Forth assembler:

.macro out_  { @0 @1 -- } 
    @0 $40 < 
    if 
        @0 @1 out,  
    else 
        @0 @1 sts,  
    endif 
.endmacro 

In this code written in FORTH language, our macro-instruction out_ uses local variables @0 and @1. We could just as well have named these variables n0 and n1.

Here is another macro-instruction written in assembler that uses a computed expression:

.macro fdw 
  .dw ((@0<<1)+PFLASH) 
.endmacro 

And the FORTH assembly version:

.macro fdw { @0 -- } 
    @0 1 <<   PFLASH + 
    .dw 
.endmacro 

Even if here we only use one parameter, the interest of going through a variable locale allows you to write the FORTH code as close as possible to that of the assembly code pure. This allows easy code recovery.

Some assembly operators in FORTH

Our meta-assembler integrates operators that clone those existing in pure assembler.

Accès vers la documentation of these operators

Example in assembler:

    ldi     t1, (1<<PGERS) | (1<<SPMEN) ; Page erase

Will be rewritten in our macro-assembler:

    t1   PGERS 1 <<    SPEM 1 <<    ||   ldi,