>>>>> On 2026-11-16, albe…@spenarnc.xs4all.nl wrote:
>>>>> Ivan Shmakov <iv…@siamics.netREMOVE.invalid> wrote:
>> I've decided to write an AVR disassembler in Forth, and while I'm
>> reasonably confident that I do most of the actual decoding correctly
>> by now, I'm much less confident of my, so to say, "design choices,"
>> and would appreciate constructive criticism on that matter.
> You may be interested in https://github.com/albertvanderhorst/ciasdis
> (8080 8086 8038 pentium pentium64 dec-alpha riscv 6809)
Cloned, thanks.
> There I define assembler parts (mnemonics, parameters) in a table.
> These objects can be used to assemble as well as disassemble.
> The principle is explained in
> https://home.hccnet.nl/a.w.m.van.der.horst/postitfixup.html
Neither this document nor forthassembler.html seems to describe
the data structures used. Do you perchance have them documented
somewhere? (I haven't yet delved that deep into the code.)
> The 8080 definition is a mere 30 lines.
(FWIW, I've counted 40, including 5 comments.)
as80> 00 00 00 T!
as80> 08 07 8 1FAMILY, RLC RRC RAL RAR DAA CMA STC CMC
as80> 08 E3 4 1FAMILY, XTHL XCHG DI EI
as80> 10 E9 2 1FAMILY, PCHL SPHL
as80> 08 C7 8 1FAMILY, RST0 RST1 RST2 RST3 RST4 RST5 RST6 RST7
So, I see that there're two instructions, PCHL and SPHL, defined
with opcodes E9 and F9 (10 E9 2 - 2 opcodes from E9 at stride 10.)
I presume executing the above results in the words PCHL and SPHL
(among others) added to the Forth's vocabulary for the assembler
part of the code. I guess they're added to some kind of a lookup
table, too, at this point, right? So that opcode E9 is mapped to
string "PCHL" in the disassembler.
What kind of a table is that?
As an aside, one of the objectives listed for ciasdis is that the
"assembler" and "disassembler" should be reverse transformations
of one another (you can disassemble your assembled binary
back into its source, and vice versa.) Which is something I
should think about - at a later date.
Right now, one of my objectives is to disassemble code assembled
with the Avra assembler - and get something I can compare back
to the source, so I can verify the correctness of my code.
The thing is, Avra follows Atmel conventions, which may have their
own idiosynchrasies I'll need to replicate, whether I like it or not.
I mean, consider that you might want to adapt your as80.frt to
interoperate with an existing Z80 assembler, where the "PCHL"
instruction is spelled "JP (HL)". Is it something that could
be done with ciasdis?
FTR, for my disassembler, I use a number of "flat" tables (the
"op" part of the opcode is the index in the table), some of them
(DISA...TAB) for choosing an appropriate decoding routine, and
the rest (DISA~...) for the mnemonics.
What I take from ciasdis is that I probably can have better words
for creating those lookup tables. Will need to think about it
for a while. So far, my best idea is to keep the data structures
I use (see below), and find better ways of populating them.
Consider, e. g., that I have DISA-RR for decoding opcodes 0400 ..
2FFF, and DISA-0 for decoding of opcodes 0000 .. 0FFF (it's the
routine that gets called via DISA-0TAB, where index is the most
significant nibble of the opcode.) The ranges overlap, so the
latter word calls the former as a special case:
CREATE DISA-0TAB
' DISA_NOP , ' DISA_MOVW , ' DISA_MULS , ' DISA_UNK ,
: DISA-0 ( pc op - )
( Call DISA-RR iff the opcode is within 0400 .. 0FFF )
DUP $ C00 AND IF ( x != 0 ) DISA-RR EXIT THEN
( Call one of DISA-0TAB words otherwise: 0000 .. 03FF )
NIP ( pc not needed )
DUP $ 8 RSHIFT 3 AND ( op y ) CELLS
( op off ) DISA-0TAB + @ EXECUTE ;
Mostly, aside of the special case call, it's 4 instructions
starting with 0000 at stride 0100. Alternatively, 0000 NOP
/might/ be a special case (depending on how the decoding works
in the MCU itself), and 0300 /is/ a special case, as it's in
fact 4 instructions (according to Atmel conventions):
( x = 0 x = 1 )
( 0300 .. 0377 MULSU 0308 .. 037F FMUL y = 0 )
( 0380 .. 03F7 FMULS 0388 .. 03FF FMULSU y = 1 )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 0 0 0 0 0 0 1 1 x [ d ] y [ r ] )
DISA-RR is simpler: it basically corresponds to defining 0B
opcodes starting with 0400 at stride 0400:
( opcode# 0 1 2 3 4 5 6 7 8 9 A )
CREATE DISA~RR ," CPC SBC ADD CPSECP SUB ADC AND EOR OR MOV "
: DISA-RR ( pc op - )
NIP ( pc not needed )
DUP $ A RSHIFT 1- DISA~RR 4 DISA-TYOP
( we get Rd with an RSHIFT , getting Rr is harder )
3 SPACES DUP 4 RSHIFT
( op op-rsh-4 ) DUP $ 1F AND ." r" 1 U.R
( op op-rsh-4 ) ." , r" 1 RSHIFT $ 10 AND
( op op-rr-hi ) SWAP $ F AND OR 1 U.R ;
Here I use 4 characters for all the opcodes, allowing me to
print the string with just ( index ) 4 * DISA~RR 1+ + 4 TYPE
to save CPU cycles, while also not wasting too much memory.
It doesn't work too well for the 10 9508 10 instructions, though:
CREATE DISA~X8
( op# 0 1 2 3 4 5 6 7 8 9 A B C D E F"
," RET RETI??2 ??3 ??4 ??5 ??6 ??7 ?SLP??9 WDR ??B LPM ??D SPM ??F "
SLEEP is 5 characters, so I can either have HEX 5 10 * => 50
bytes table with 5 characters per mnemonic, or I can save
HEX 10 bytes (8 words) by using stride 4 for this table, but
I then need to special-case op# 8 SLEEP in the code.