Preparation of our first AVR8 code assembly
published: 30 March 2021 / updated 2 April 2021
We now have an assembler for ARDUINO compiled by gForth. It is necessary now test it to verify that it is working properly. Here are the steps of this control of proper functioning of our assembler: FlashForth assembler for Atmega chips
To use this assembler with gForth, download the file Xassembler.txt and compile its code with gForth.
The PLUS program
Here is the source version in AVR8 assembler as defined in the file ff-atmega.asm
the word+
:
PLUS: ld t0, Y+ ld t1, Y+ add tosl, t0 adc tosh, t1 ret
We copy and paste this code in a file plus.asm
PLUS code assembly
To test this assembly code, we will first complete the source code as following:
.include "m328pdef.inc" .include "macros.inc" .cseg .org 0x00 PLUS: ld t0, Y+ ld t1, Y+ add tosl, t0 adc tosh, t1 ret
On assemble ce code. Le résultat génère divers fichiers dont le résultat de l'assemblage dont en voici la partie qui nous intéresse:
PLUS: C:000000 9109 ld t0, Y+ C:000001 9119 ld t1, Y+ C:000002 0f80 add tosl, t0 C:000003 1f91 adc tosh, t1 C:000004 9508 ret
The second column contains the hexadecimal code of the result of the assembly of our source code in PLUS.asm. It is this hexadecimal code that goes to us allow to check the correct functioning of our AVR8 assembler written in FORTH language in the Xassembler.txt file.
Rewriting assembly code
Our AVR8 assembler written in FORTH language does not use the original syntax of the assembler. Example:
ld t0, Y+
must be rewritten:
t0 Y+ ld,
The assembler written in the FORTH language works like the other words in the FORTH language. You first type in the parameters before using the assembler mnemonic. Result of this rewrite:
r16 constant t0 r17 constant t1 r24 constant tosl r25 constant tosh : plus [ t0 Y+ ld, ] [ t1 Y+ ld, ] [ tosl t0 add, ] [ tosh t1 adc, ] [ ret, ] ;
We just copy and paste this code into gForth. Here is the result of this compilation:
r16 constant t0 ok r17 constant t1 ok r24 constant tosl ok r25 constant tosh ok : plus compiled [ t0 Y+ ld, ] 9109 compiled [ t1 Y+ ld, ] 9119 compiled [ tosl t0 add, ] 0F80 compiled [ tosh t1 adc, ] 1F91 compiled [ ret, ] 9508 compiled ; ok
We can compare line by line the hexadecimal code produced by gForth and that resulting from the assembly of PLUS.asm:
- assembler:
C:000000 9109 ld t0, Y+
- gForth:
[ t0 Y+ ld, ] 9109 compiled
These two tools produce the same hexadecimal code 9109.
Code verification on Arduino NANO
It is possible to verify the execution of this hexadecimal code on an ARDUINO card
on which FlashForth is installed. Here is the source code of the word PLUS
which can be compiled without going through the assembler of the asm.fs file:
-xxx marker -xxx : PLUS [ $9109 i, ] [ $9119 i, ] [ $0f80 i, ] [ $1f91 i, ] [ $9508 i, ] ;
Running PLUS
on an ARDUINO card:
3 5 PLUS . \ display 8 3 -5 PLUS . \ display -2
We just passed our very first FlashForth ARDUINO code meta-assembly without using an assembler for FlashForth. In conclusion, we have an assembler AVR8 which is operational for gForth and which generates executable binary code on our ARDUINO card.
But before we get to a real metacompilation, we need to consolidate the development environment usable with Xassembler. This is what we will see in the next article.