From Newsgroup: comp.lang.forth
On 2026-11-19, mikael wrote:
On Sun, 15 Nov 2026 22:55:25 +0000, Ivan Shmakov wrote:
I've decided to write an AVR disassembler in Forth,
You can look at fairly complete AVR assembler/dissasembler for
FlashForth.
https://github.com/oh2aun/flashforth/blob/master/avr/forth/asm2.fs https://github.com/oh2aun/flashforth/blob/master/avr/forth/see2.fs
I shouldn't, though: I'm aiming at creating an independent
implementation, hence I should, as a rule, avoid looking at
existing code.
Starting this discussion, I hoped for an exchange of ideas more
than exchange of code. I provide examples of my own code here
mainly as evidence that my ideas do, in fact, work.
Right now, some unrelated problems I'm working on must take
priority for a week or two, and I doubt I'd be releasing the
entire code before I've got them resolved.
So far, I have used some Perl to extract opcode bit patterns out
of the 0856J AVR Instruction Set Manual, as well as a more recent
(but, so far as I can tell, largely unchanged in substance)
corresponding document from Microchip, and by the looks of it,
the only AVRe instructions I do not currently decode are in the
9000 .. 93FF range, except for plain LDS / STS (i. e., LSN = 0)
that are special-cased due to being 32-bit.
In particular, I've been able to disassemble a chunk of 256 bytes
out of /dev/urandom , and assemble it back into those same bytes
with Avra. (Found a couple of bugs in the process, too - one
missing call to DISAVR/RD , and one extra call to the same.)
Some editing of the disassembled listing was necessary, as I
currently print relative program counter values "as is," while
Avra of course expects absolute values (that it then transforms
into relative ones internally.)
A test suite and documentation are still missing, though.
Still, just in case anyone's curious, I'm going to detail some
of the recent improvements below.
First of all, I've got rid of "nested" tables indexed with
portions of the opcode, replacing them with a single ordered
list and a binary search routine:
CREATE DISAVR-TAB
$ 0000 , ' _NOP , $ 0001 , ' DISAVR_PUNK ,
$ 0100 , ' _MOVW ,
$ 0200 , ' _MULS , $ 0300 , ' _FMULSU ,
$ 0400 , ' _RR , ( op Rd, Rr )
$ 3000 , ' _RI , ( op R{d + $10}, K )
$ 8000 , ' _LS , ( LD LDD ST STD )
$ 9000 , ' DISAVR_PUNK , ( ^LS FIXME: only 9..0 opcodes )
$ 9400 , ' _R , ( op Rd and others )
$ 9600 , ' _XW , ( ADIW and SBIW )
$ 9800 , ' _XB , ( Bit operations )
$ 9C00 , ' _MUL , ( MUL Rd, Rr )
$ A000 , ' _LS , ( LD LDD ST STD )
$ B000 , ' _IO , ( IN and OUT )
$ C000 , ' DISAVR_PUNK , ( special case: -RE )
$ E000 , ' _RI , ( LDI R{d + $10}, K )
$ F000 , ' DISAVR_PUNK , ( special case: -BRB )
$ F800 , ' _BT , ( BLD/ST SRBC/S )
HERE DISAVR-TAB - 2 CELLS / CONSTANT DISAVR-TLE
ALIGN
: DISAVR-BSEA2 ( value base count - addr )
( Perform binary search over an ordered key value list )
DUP 1 RSHIFT ( value base count count/2 )
?DUP 0= IF DROP NIP EXIT THEN
DUP 2 CELLS * ( value base count count/2 off )
3 PICK + @ ( value base count count/2 cur )
4 PICK >
IF ( value base count count/2 )
NIP
ELSE TUCK - ( value base count/2 count' )
-ROT 2 CELLS * + ( value count' base' )
SWAP
THEN RECURSE ;
I wanted to have a more general version, with an arbitrary
"stride" parameter, but I'm accessing deep into data stack
as it is, so going to stick to the above for now.
I've learned of pForth's PRIVATE{ PRIVATE} and PRIVATIZE ,
so now use shorter "private" words. And since I don't use
my prefix all over the place now, I've switched to a longer,
clearer one: DISAVR (was: DISA.)
Most of the decoding words only need the opcode parameter,
so that's what DISAVR- passes to _-words found in DISAVR-TAB .
The calls to ^JC ^LS and -RE -BRB are special-cased:
: DISAVR- ( addr - addr' )
DUP 2+ SWAP DUP DISAVR@ ( a' a op )
( FIXME: we only pass to DISAVR-PPC the 16-bit opcode )
( of 32-bit instructions )
SWAP DISAVR-ORG @ - SWAP ( a' pc op ) DISAVR-PPC
DUP DISAVR/WI
IF ( a' pc op )
NIP ( pc not needed here )
OVER DISAVR@ SWAP ( a' k op )
( 1 0 0 1 0 0 o . . . . . 0 0 0 0 LDS STS )
( 1 0 0 1 0 1 0 . . . . . 1 1 o . JMP CALL )
DUP $ 400 AND IF ^JC ELSE ^LS THEN
2+ EXIT THEN
DUP $ E000 AND $ C000 =
IF ( a' pc op )
( 1 1 0 o [ k ] RJMP RCALL )
-RE EXIT THEN
DUP $ F800 AND $ F000 =
IF ( a' pc op )
( 1 1 1 1 0 o [ k ] [ s ] BRBS BRBC )
-BRB EXIT THEN
NIP DUP DISAVR-TAB DISAVR-TLE DISAVR-BSEA2
( a' op ptr ) CELL + @ EXECUTE ;
Special-casing -RE and -BRB is not strictly necessary, but
allows me to pass "program counter" to them, so they print
the target location, like (BRBC 4 = BRSC, "branch if sign
flag is cleared"):
; pc opc assembly rel abs flag
DE F6DC BRBC 4, -25 ; 96 ; S
^JC and ^LS require an additional value on stack, containing the
rest of the 32-bit instruction. (So far as I can tell from
reading the Manual, LDS STS JMP CALL are the only instructions
having opcodes that aren't 16-bit.)
For customizability, I've added the following DEFERred words:
DEFER DISAVR-PFL ( n - )
( Default: print a comment for the flag used by instruction )
DEFER DISAVR-PPC ( pc opcode - pc opcode )
( Default: print program counter and opcode in hex )
DEFER DISAVR-PR ( n - )
( Default: print register as r{decimal} regardless of BASE )
DEFER DISAVR-PREL ( pc off sign - )
( Default: print relative offset, then absolute address comment )
DEFER DISAVR_PUNK ( op - )
( Default: print raw opcode in hex - used if decoding fails )
For instance, the parts of the example above are printed with:
DE F6DC BRBC 4, -25 ; 96 ; S
---- (DISAVR-PFL)
-------- (DISAVR-PREL)
---------- -BRB ( private )
--------------- (DISAVR-PPC)
It is possible to use ' NOOP IS DISAVR-PPC and ' DROP IS DISAVR-PFL
to omit the respective portions of the output. It is also possible
to use DISAVR-PPC to alter pc and opcode early in the processing,
though I couldn't think of any good use case for this feature.
(DISAVR-PREL) currently does double-duty. Namely, it sign-extends
the given offset (in 16-bit words), prints it, then also prints the
corresponding absolute offset (in octets) as a comment:
: (DISAVR-PREL) ( pc off sign - )
( Print relative offset, then absolute address comment )
OVER AND 1 LSHIFT - DUP .
( NB: pc is in octets, off is in 16-bit words )
." ; " 1 LSHIFT + 2+ 1 U.R ;
' (DISAVR-PREL) IS DISAVR-PREL
I guess I'd need to split the last part into its own word, and
keep only that later word DEFERred.
Or perhaps I should rather print absolute addresses as operands
and the actual offsets as comments, for better interoperability
with Avra?
Also, recall that I was printing registers with ." r" 1 U.R -
using whatever base in effect, resulting in, say, r1F in HEX.
This is no longer the case, though with DISAVR-PR being DEFERred,
it's possible to revert to the previous behavior.
: (DISAVR-PR) ( r - )
( Print register as r{decimal} regardless of BASE )
[CHAR] r EMIT
$ 1F AND $ A /MOD
?DUP IF [CHAR] 0 + EMIT THEN
[CHAR] 0 + EMIT ;
' (DISAVR-PR) IS DISAVR-PR
Thoughts?
--- Synchronet 3.21e-Linux NewsLink 1.2