Thread View: comp.lang.forth
11 messages
11 total messages
Started by Ivan Shmakov
Sun, 15 Nov 2026 22:55
writing an AVR disassembler
Author: Ivan Shmakov
Date: Sun, 15 Nov 2026 22:55
Date: Sun, 15 Nov 2026 22:55
304 lines
11734 bytes
11734 bytes
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.
Two important points to stress out:
a. my first experience with Forth dates back to c. 1994, but
so far as I can recall, this is the first substantial code
that I ever wrote in the language; as such, I may miss any
number of common Forth coding (and formatting) idioms;
in this effort, I /am/ familiarizing myself with the AVR
machine code, but also with Forth itself, particularly
with the development of complex applications in it;
b. ultimately, I intend for the code to run on an AVR MCU
itself, on a suitable Forth system running there; I frankly
doubt a fully-featured ANS Forth implementation makes sense
on a 8-bit architecture, hence I expect something like cmForth
or Pygmy Forth there instead; for now, though, I use pForth,
which is described as "ANS-like"; (in particular, I treat my
cells as if they were 16 bit wide, though they are in fact
either 32 bit or 64 bit in the builds pForth I have handy.)
I prefix all the words of the disassembler proper with "DISA",
though for testing, I use several additional words not so
prefixed. Such as the words that "hex" memory and "unhex"
strings (note that $ is a word, not a prefix, in pForth):
1 CELLS .
VARIABLE DUMORG 0 DUMORG !
: DUMX ( a - a' )
( Dump $ 10 bytes from a in HEX; return a + $ 10 )
BASE @ >R HEX
CR DUP DUMORG @ - 4 U.R
3 SPACES
$ 10 0 DO SPACE DUP C@ 2 U.R 1+ LOOP
R> BASE ! ;
: DU ( a n - )
( Dump n lines $ 10 bytes each from a in HEX )
0 DO DUMX LOOP DROP ;
: 2C@ ( a - c0 c1 )
( Fetch two characters from a )
DUP C@ SWAP 1+ C@ ;
: UNNIBBLE ( c - n )
( Convert a hex digit character into a number )
[CHAR] 0 2DUP [CHAR] 9 BETWEEN IF - EXIT THEN DROP
[CHAR] A 2DUP [CHAR] F BETWEEN IF - $ A + EXIT THEN DROP
[CHAR] a 2DUP [CHAR] f BETWEEN IF - $ A + EXIT THEN DROP
ABORT" character not in the 0-9a-fA-F range" ;
: UNX- ( from len to - to' )
( Unhex a from, len string into memory at to )
BEGIN OVER 2 >= WHILE
ROT ( len to from )
DUP 2C@ UNNIBBLE SWAP UNNIBBLE 4 LSHIFT OR
( len to from n ) 2 PICK C!
( len to from ) 2+ ROT 2- ROT 1+
( from len to ) REPEAT
( from len to ) NIP NIP ;
: UNX ( from len to - ) ( like PLACE )
UNX- DROP ;
(As usual, I prefix the prose of my post with a TAB code,
so if you filter out lines like that, you can run the body
of this article as a program in pForth.)
I /think/ that UNNIBBLE in particular can be made simpler,
but it's not obvious to me how. (Or do I just use some
built-in word here instead?)
An example of how the above is used:
CREATE EXAMPLE
EXAMPLE DUP
" 05c000000000000000002ec0002708bb" COUNT ROT UNX-
" 0ce107bb02e003bf02e009bf97ee8ce4" COUNT ROT UNX-
" aa249924789400e205bfaa2029f4bb24" COUNT ROT UNX-
" b094a82eaa2071f0b3948894a79410f0" COUNT ROT UNX-
" b1f3facf3b2d330f330f3c7108b3037e" COUNT ROT UNX-
" 032b08bb00e8901628f0901a082f0795" COUNT ROT UNX-
" 979587958895e1cf1fb693941fbe1895" COUNT ROT UNX-
OVER - ALLOT
HERE CONSTANT EXAMPLE-END ( will need this later )
DUP DUMORG !
7 DU
Expected output:
0 5 C0 0 0 0 0 0 0 0 0 2E C0 0 27 8 BB
10 C E1 7 BB 2 E0 3 BF 2 E0 9 BF 97 EE 8C E4
20 AA 24 99 24 78 94 0 E2 5 BF AA 20 29 F4 BB 24
30 B0 94 A8 2E AA 20 71 F0 B3 94 88 94 A7 94 10 F0
40 B1 F3 FA CF 3B 2D 33 F 33 F 3C 71 8 B3 3 7E
50 3 2B 8 BB 0 E8 90 16 28 F0 90 1A 8 2F 7 95
60 97 95 87 95 88 95 E1 CF 1F B6 93 94 1F BE 18 95
Now as to disassembler proper. I'm not going to include the
entire source here - just the parts that decode opcodes in
the 0000 .. 2FFF and B000 .. BFFF ranges, as I believe they're
representative of the approach I follow.
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( . . . . . . . [ d ] . . . . )
: DISA/RD ( opc - d )
( Extract destination register from an opcode )
4 RSHIFT $ 1F AND ;
: DISA-TYOP ( num base stride - )
( Type a string number num out of a string table at base )
ROT ( base stride num )
OVER * ( base stride offset ) ROT + 1+ SWAP TYPE ;
( NB: we reserve DISA_ prefix for words of this signature )
: DISA_UNK ( op - )
( Type raw opcode in hex - used if decoding fails )
." ?" BASE @ HEX SWAP 1 U.R BASE ! ;
( 0000 : NOP )
: DISA_NOP ( op - )
?DUP IF DISA_UNK EXIT THEN
." NOP" ;
( 01.. : MOVW Rd, Rr )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 0 0 0 0 0 0 0 1 [ d ] [ r ] )
: DISA_MOVW ( op - )
." MOVW r" DUP 3 RSHIFT $ 1E AND 1 U.R
." , r" $ F AND 1 LSHIFT 1 U.R ;
Conventionally, I'd need to ensure I print all registers in
decimal (like MOVW r8, r30) - regardless of BASE. I've decided
/not/ to do that for the sake of simplicity of the code.
( 02.. : MULS Rd, Rr )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 0 0 0 0 0 0 1 0 [ d ] [ r ] )
: DISA_MULS ( op - )
." MULS r" DUP DISA/RD $ 10 OR 1 U.R
." , r" $ F AND $ 10 OR 1 U.R ;
( 03.. : F?MULS?U? Rd, Rr )
( Elided )
( 0400 .. 2FFF : op Rd, Rr )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 0 0 [ opc ] r^[ d ] [ r ] opc != 0 )
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 ;
( Test it: PC opcode )
CR ." ADD " $ 544E $ 0DB9 DISA-RR
Expected output (in DECIMAL and HEX):
ADD ADD r27, r9
ADD ADD r1B, r9
The program counter value is not used here, but is needed to
print absolute addresses in instructions that take relative
ones as operands - such as RJMP and RCALL.
( 0... : Miscellaneous opcodes with 0 in the MSN )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 0 0 0 0 [x] [y] )
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 ;
( Test it: PC opcode )
CR ." ADD " $ 544E $ 0DB9 DISA-0
CR ." NOP " $ E58C $ 0 DISA-0
CR ." ? " $ E58C $ 23 DISA-0
CR ." MULS " $ DD8E $ 02DF DISA-0
CR ." FMULS " $ D1E2 $ 03E4 DISA-0
Expected output (in DECIMAL):
ADD ADD r27, r9
NOP NOP
? ?23
MULS MULS r29, r31
FMULS ?3E4
Note that FMULS was not decoded as the respective entry
in DISA-0TAB was dummied out with ' DISA_UNK. (And as the
respective DISA_FMULSU word was elided from this post.)
The above is relatively straightforward. Printing the
disassembled IN Rd, A and OUT A, Rr instructions is a tad
more involved, and we need branching rather than just
printing a string from some DISA~IO table:
( B... : IN Rd, A and OUT A, Rr )
( F E D C B A 9 8 7 6 5 4 3 2 1 0 )
( 1 0 1 1 o [A^][ r/d ] [ A ] )
: DISA-IO
NIP ( pc not needed )
DUP DISA/RD ( op r# )
OVER DUP $ F AND SWAP 5 RSHIFT $ 30 AND OR
( op r# a ) ROT $ 800 AND ( r# a out? )
IF ." OUT " 1 U.R ." , r"
ELSE SWAP ( a r# )
." IN r" 1 U.R ." , "
THEN 1 U.R ;
And decoding instructions with 9 in the MSN is perhaps the
single most complex part of the code, as that range includes
such diverse ones as: COM Rd; SBIC A, b; BSET s; and RET.
It /seems/ that I've nailed much of it down, but I'm frankly
way less sure of that code right now, so not going to include
it here.
Now we can tie the above into the "top-level" disassembly words.
: DISA-UNK ( pc op - )
NIP DISA_UNK ;
CREATE DISATAB
( 0 ) ' DISA-0 ,
( 1 .. 3 ) ' DISA-RR DUP , , ' DISA-UNK DUP ,
( 4 .. 7 ) DUP , DUP , DUP , DUP ,
( 8 .. B ) DUP , DUP , DUP , ' DISA-IO ,
( C .. F ) DUP , DUP , DUP , ,
: DISA-W ( pc op - )
( Disassemble a single 16-bit "word" instruction )
OVER 7 U.R SPACE
DUP 7 U.R SPACE
DUP $ C RSHIFT $ F AND CELLS DISATAB +
( pc op addr ) @ EXECUTE ;
: DISA@ ( a - op )
( Fetch an instruction at a Forth address )
2C@ $ 8 LSHIFT OR ;
VARIABLE DISAORG 0 DISAORG !
: DISA- ( a - a' )
( Disassemble an instruction at a Forth address )
DUP DISA@ ( a op )
( DUP DISA/WI ) 2 ( a op oct )
ROT ( op oct a )
SWAP 2DUP + ( op a oct a' )
>R -ROT ( oct op a | a' )
DISAORG @ - ROT ( op pc oct | a' )
2 = IF SWAP DISA-W
ELSE ( FIXME: implement 4-octet instructions ) THEN
R> CR ;
: DISA ( from to - )
( Disassemble a memory region )
BEGIN ( to a ) 2DUP > WHILE DISA- REPEAT
2DROP ;
( Test it )
HEX
CR EXAMPLE-END EXAMPLE DUP DISAORG ! DISA
Expected output, or at least the interesting parts of it
(much of the EXAMPLE uses instructions not decoded by this
abridged version of the code):
0 C005 ?C005
2 0 NOP
4 0 NOP
6 0 NOP
8 0 NOP
A C02E ?C02E
C 2700 EOR r10, r10
E BB08 OUT 18, r10
...
68 B61F IN r1, 3F
6A 9493 ?9493
6C BE1F OUT 3F, r1
6E 9518 ?9518
Note that the "AVR Instruction Set Manual", be it [1] or [2],
provides a terse "CLR Rd" alias for "EOR Rd, Rd". Yet again,
for the sake of simplicity, I don't plan implementing shortcuts
like that in this disassembler.
(The initial six words are effectively interrupt vectors:
C005 is RJMP +5, or 000C, for "power up or reset"; and
C02E is RJMP +2E, or 0068, for a timer/counter overflow.
3F is the status, or flag, register, AKA "SREG" - it gets
saved in r1, otherwise reserved in this code, for the
duration of the ISR. 9493 is INC r9, and 9518 is RETI.)
That would be all for now. Thoughts?
[1] http://web.archive.org/web/20160411235033
/http://www.atmel.com/images/Atmel-0856
-AVR-Instruction-Set-Manual.pdf
[2] http://ww1.microchip.com/downloads/en/DeviceDoc
/AVR-InstructionSet-Manual-DS40002198.pdf
Re: writing an AVR disassembler
Author: dxf
Date: Mon, 16 Nov 2026 00:57
Date: Mon, 16 Nov 2026 00:57
26 lines
1727 bytes
1727 bytes
On 16/11/2026 9:55 am, Ivan Shmakov wrote: > ... > I /think/ that UNNIBBLE in particular can be made simpler, > but it's not obvious to me how. (Or do I just use some > built-in word here instead?) My 'built-in' (kernel) word is >DIGIT and was designed to be 'general purpose'. It's coded in assembler but here's a high-level version. It's case-sensitive to handle situations where lowercase would amount to an error. It returns 'char' in the event of error should one want to print it. : END postpone EXIT postpone THEN ; immediate \ >DIGIT >CHAR \ Cvt numeric char to digit u according to base. Case sensitive. \ Defined in kernel : >DIGIT ( char base -- u true | char false ) over [char] 0 - dup #10 #17 within 0= if dup #16 u> if 7 - then dup rot u< if nip true end dup then 2drop false ; \ Convert numeric digit u to ASCII character char \ Defined in kernel : >CHAR ( u -- char ) dup 9 > if 7 + then [char] 0 + ;
Re: writing an AVR disassembler
Author: albert
Date: Mon, 16 Nov 2026 13:49
Date: Mon, 16 Nov 2026 13:49
34 lines
2305 bytes
2305 bytes
In article <UOSz15Vn9V7SG_X-@violet.siamics.net>,
Ivan Shmakov <ivan@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)
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
The 8080 definition is a mere 30 lines.
A simplification can do only assembling, and result in a
100 line Pentium assembler with floating point instruction but
exclusive all extensions.
See
https://github.com/albertvanderhorst/ciforth
This assembler is in the file blocks.frt (a regular source file),
ignore everything else.
Try to understand line 2960 and up. This should be perfectly
fit for a minimal implementation of an assembler.
[I never thought of an excerpt of ciasdis to do a disassembler. ]
Groetjes Albert
--
The Chinese government is satisfied with its military superiority over USA.
The next 5 year plan has as primary goal to advance life expectancy
over 80 years, like Western Europe.
Re: writing an AVR disassembler
Author: dxf
Date: Tue, 17 Nov 2026 14:51
Date: Tue, 17 Nov 2026 14:51
22 lines
1675 bytes
1675 bytes
On 16/11/2026 11:57 am, dxf wrote:
> On 16/11/2026 9:55 am, Ivan Shmakov wrote:
>> ...
>> I /think/ that UNNIBBLE in particular can be made simpler,
>> but it's not obvious to me how. (Or do I just use some
>> built-in word here instead?)
>
> My 'built-in' (kernel) word is >DIGIT and was designed to be 'general
> purpose'. It's coded in assembler but here's a high-level version.
> It's case-sensitive to handle situations where lowercase would amount
> to an error. It returns 'char' in the event of error should one want
> to print it.
> ...
According to pForth (github) it has DIGIT with the same stack effect as
mine. It differs in that it is case-insensitive i.e. 'a' is treated as 'A'.
Based on that, then this should work:
: UNNIBBLE ( c - n )
( Convert a hex digit character into a number )
16 DIGIT 0= ABORT" character not in the 0-9a-fA-F range" ;
Re: writing an AVR disassembler
Author: Ivan Shmakov
Date: Tue, 17 Nov 2026 19:35
Date: Tue, 17 Nov 2026 19:35
50 lines
3425 bytes
3425 bytes
>>>>> On 2026-11-17, dxf wrote: >>>>> On 16/11/2026 11:57 am, dxf wrote: >>>>> On 16/11/2026 9:55 am, Ivan Shmakov wrote: >>> I /think/ that UNNIBBLE in particular can be made simpler, but it's not >>> obvious to me how. (Or do I just use some built-in word here instead?) >> My 'built-in' (kernel) word is >DIGIT and was designed to be 'general >> purpose'. It's coded in assembler but here's a high-level version. >> It's case-sensitive to handle situations where lowercase would amount >> to an error. It returns 'char' in the event of error should one want >> to print it. >> : >DIGIT ( char base -- u true | char false ) >> over [char] 0 - dup #10 #17 within 0= if dup #16 u> >> if 7 - then dup rot u< if nip true end dup then >> 2drop false ; > According to pForth (github) it has DIGIT with the same stack effect > as mine. It differs in that it is case-insensitive i.e. 'a' is treated > as 'A'. Based on that, then this should work: > : UNNIBBLE ( c - n ) > ( Convert a hex digit character into a number ) > 16 DIGIT 0= ABORT" character not in the 0-9a-fA-F range" ; ACK, thanks, it does. I'd say it ends up being more complex this way: DIGIT is implemented in pForth itself (see fth/numberio.fth), and is fairly involved at that, but my code is a prototype anyway, so it makes every sense to use what suitable built-in words that might be available. Like I've said, I intend for the code to eventually run on the MCU itself, and I expect an AVR Forth to be closer to, say, Pygmy Forth, than pForth. About the only reason I'm not writing this prototype in Pygmy is that I haven't figured out a good way to send bits of code from Vi(m) to that particular implementation (even with "CTTY AUX" and ">DOS". "INCLUDE CON", perhaps?) Regardless, Pygmy has -DIGIT , which is similar, but with it using BASE , it's far less suitable: p> : -DIGIT ( c - n) $30 - DUP 9 > IF 7 - DUP $A < OR THEN p> DUP BASE @ U< NOT ABORT" ?" ; Another thing to note is that "7" both here and in >DIGIT above is ASCII-specific. I have no plans for supporting, say, EBCDIC, but something like an explicit [ CHAR A CHAR 9 - 1- ] might be helpful should anyone desire to port the code to such an environment.
Re: writing an AVR disassembler
Author: dxf
Date: Wed, 18 Nov 2026 11:56
Date: Wed, 18 Nov 2026 11:56
12 lines
1409 bytes
1409 bytes
On 18/11/2026 6:35 am, Ivan Shmakov wrote: > ... > Another thing to note is that "7" both here and in >DIGIT above is > ASCII-specific. I have no plans for supporting, say, EBCDIC, but > something like an explicit [ CHAR A CHAR 9 - 1- ] might be helpful > should anyone desire to port the code to such an environment. While it avoids the 'magic number' there's no guarantee it won't need changing. EBCDIC was apparently too archaic even for ANS-Forth. They didn't want to know about it, saying such machines typically supported ASCII anyway.
Re: writing an AVR disassembler
Author: Ivan Shmakov
Date: Wed, 18 Nov 2026 17:10
Date: Wed, 18 Nov 2026 17:10
131 lines
6708 bytes
6708 bytes
>>>>> On 2026-11-16, albert@spenarnc.xs4all.nl wrote:
>>>>> Ivan Shmakov <ivan@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.
Re: writing an AVR disassembler
Author: albert
Date: Wed, 18 Nov 2026 19:52
Date: Wed, 18 Nov 2026 19:52
41 lines
2601 bytes
2601 bytes
In article <_9Chl2n-LnizheJe@violet.siamics.net>, Ivan Shmakov <ivan@siamics.netREMOVE.invalid> wrote: <SNIP> > 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? No, that doesn't work. The post-it fixup principle demands i.a. that the mnemonic significant bits are known by a mask that identifies them. 0 0 0 T! 08 07 8 1FAMILY, RLC RRC RAL RAR DAA CMA STC CMC defines 07 0F 17 2F .. 4F. During disassembly you match as a whole byte, because of the 0 passed to T! More illustrative is the following. 00 00 38 T! 08 00 8 xFAMILY| B'| C'| D'| E'| H'| L'| M'| A'| ( dst) 01 04 2 1FAMILY, INR DCR ( B'|) For disassembling you erase the bits from 38 in the instruction you want to disassemble. ( That are the bits filled in by B'| C'| etc.) What is left, you match with the 04 05 of INR DCR. If there is a bit outside of the 38 e.g. 14 then INR or DCR is not a disassembly and you go on. Otherwise you continue with matching the "fixups" B'| etc. You cannot make this to work with an arbitrary prescribed assembler syntax. The advantage is that the assembler disassembler works for all processors. What remains is filling in tables. It is comparatively easy to add a table for more instructions like an SSE set. However filling in the tables has the difficulty that the instruction must be hit in the right order, and before the fixup. Groetjes Albert -- The Chinese government is satisfied with its military superiority over USA. The next 5 year plan has as primary goal to advance life expectancy over 80 years, like Western Europe.
Re: writing an AVR disassembler
Author: dxf
Date: Thu, 19 Nov 2026 04:33
Date: Thu, 19 Nov 2026 04:33
17 lines
1583 bytes
1583 bytes
On 19/11/2026 4:10 am, Ivan Shmakov wrote: > ... > 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? Back in the days of CP/M there were source translators to convert between Intel and Zilog mnemonics should one need it.
Re: writing an AVR disassembler
Author: mikael
Date: Thu, 19 Nov 2026 09:46
Date: Thu, 19 Nov 2026 09:46
10 lines
1510 bytes
1510 bytes
On Sun, 15 Nov 2026 22:55:25 +0000, Ivan Shmakov wrote: > I've decided to write an AVR disassembler in Forth, and while > That would be all for now. Thoughts? 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 -- Mikael
Re: writing an AVR disassembler
Author: Ivan Shmakov
Date: Mon, 23 Nov 2026 15:35
Date: Mon, 23 Nov 2026 15:35
193 lines
8806 bytes
8806 bytes
>>>>> 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?
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads