6502 Assembler in 96 lines of Forth (July 1980)
6502 Assembler in 96 lines of Forth (July 1980)
by Bill Ragsdale, in Forth Dimensions Vol 3 Number 5 (pdf) (or this archive version) (or this wikified version)
"Some will be fascinated by a label-less, macro-assembler whose source code is only 96 lines long! Others will be repelled by reverse Polish syntax and the absence of labels."
"The author immodestly claims that this is the best FORTH assembler ever distributed. ... It is released to the public domain as a defense mechanism."
Features:
- User macros, definable at any time
- Literal values expressed in any base
- Expressions using any resident computation capability
- Nested control structures without labels, with error control
- Assembler source itself in a portable high level language
"Some will be fascinated by a label-less, macro-assembler whose source code is only 96 lines long! Others will be repelled by reverse Polish syntax and the absence of labels."
"The author immodestly claims that this is the best FORTH assembler ever distributed. ... It is released to the public domain as a defense mechanism."
Features:
- User macros, definable at any time
- Literal values expressed in any base
- Expressions using any resident computation capability
- Nested control structures without labels, with error control
- Assembler source itself in a portable high level language
Last edited by BigEd on Sun Mar 27, 2016 8:50 am, edited 2 times in total.
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Machine-readable source code for this remarkable assembler is found here. For a description of the assembler refer to the Forth Dimensions article mentioned in the previous post (thanks, Ed!).
-- Jeff
-- Jeff
Last edited by Dr Jefyll on Sun Mar 27, 2016 1:31 pm, edited 1 time in total.
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
- GARTHWILSON
- Forum Moderator
- Posts: 8774
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: 6502 Assembler in 96 lines of Forth (July 1980)
It's nice that writing an assembler in Forth is trivial enough that one can do it to their own taste in a couple of hours. I did, because I wanted a more-standard mnemonic-operand order for assembly code. It doesn't require any parsing or even separate assembler vocabulary, because AND_ABS for example lays down the op code for the instruction with the right addressing mode, then you comma-in the operand. Because of the obvious addressing mode included in the instruction name, it does not conflict with Forth's AND. Since it's Forth, macro ability is effortlessly innate, and I didn't even realize it until later.
I think Samuel Falvo posted an assembler that was well under one screen (16x64) long. I'll come back and add the link if I find it.
I think Samuel Falvo posted an assembler that was well under one screen (16x64) long. I'll come back and add the link if I find it.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Quote:
It's nice that writing an assembler in Forth is trivial enough that one can do it to their own taste in a couple of hours.
And tastes do vary, of course. I'm not trying to suggest that Ragsdale's fully-RPN approach is superior to the more-standard mnemonic-operand order you mentioned, or that other choices he made are necessarily the best. But I used to use the Ragsdale assembler quite extensively. Now, years later, I'm fooling around with FIG Forth again, so it's only natural to pick up where I left off. The other day I got FIG Forth running in the Kowalski simulator, and tonight I started the simulation and then dropped in Ragsdale's high-level source code -- which compiled instantly without a glitch
FWIW I am a fan of the structured conditionals that Ragsdale included -- such as IF ELSE THEN and BEGIN UNTIL, for example, as opposed to bare assembly-language instructions ( BNE BCC etc.). Ragsdale even included error-checking (similar to that used in the high-level FIG compiler) which ensures that your conditionals are properly paired. But, frankly, for assembly-level coding I found that the error-checking can get to be a nuisance... although it's not terribly difficult to circumvent -- you can break the rules if you need to.
Back in the day I extended the Ragsdale assembler to include Rockwell 65C02 instructions. If I can find that code I'll post it.
-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Dr Jefyll wrote:
Quote:
It's nice that writing an assembler in Forth is trivial enough that one can do it to their own taste in a couple of hours.
[..]
FWIW I am a fan of the structured conditionals that Ragsdale included -- such as IF ELSE THEN and BEGIN UNTIL, for example, as opposed to bare assembly-language instructions ( BNE BCC etc.). Ragsdale even included error-checking (similar to that used in the high-level FIG compiler) which ensures that your conditionals are properly paired. But, frankly, for assembly-level coding I found that the error-checking can get to be a nuisance... although it's not terribly difficult to circumvent -- you can break the rules if you need to.
Back in the day I extended the Ragsdale assembler to include Rockwell 65C02 instructions. If I can find that code I'll post it.
-- Jeff
(sorry for the strange text format which comes from the the editor on my "development system", a Forth on a C64
BTW, it's influenced by several assemblers, but the roots in Ragsdale's version can't be denied. It has two (CPU) modes to adapt on how the code for structural elements is generated. WDC specific instructions are included too (just for completeness).
Johann
(yes, I know, call me a grave digger
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Welcome, Johann -- nice to have another Forth enthusiast in our midst! 
Can you explain how you evaluate the result of your test suites? When executed, T1C02 evidently assembles some code, as does T2C02. Then what?
Oops, I have yet to follow through on the search for that code. Unfortunately, it's probably on a non-DOS 5-inch floppy.
LOL! Back then that medium seemed perfectly wonderful -- I was delighted just to avoid using audio cassette! We're talking mid 1980s, btw, so I guess you & I were hacking Ragsdale at about the same time.
Cheers,
Jeff
Can you explain how you evaluate the result of your test suites? When executed, T1C02 evidently assembles some code, as does T2C02. Then what?
Quote:
At the time Jeff's post appeared I decided to offer my 65C02 assembler (written longer ago), if Jeff doesn't find his.
Cheers,
Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
https://laughtonelectronics.com/Arcana/ ... mmary.html
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Dr Jefyll wrote:
Welcome, Johann -- nice to have another Forth enthusiast in our midst!
BTW, I was already in touch with Garth Wilson (for his '816 Forth which I studied with great appreciation).
Dr Jefyll wrote:
Can you explain how you evaluate the result of your test suites? When executed, T1C02 evidently assembles some code, as does T2C02. Then what?
I added the disassembler and decompiler on my home page (including some short instructive hints, hope so).
Dr Jefyll wrote:
Quote:
At the time Jeff's post appeared I decided to offer my 65C02 assembler (written longer ago), if Jeff doesn't find his.
Re: 6502 Assembler in 96 lines of Forth (July 1980)
This seems like a good thread for the 1991 text document B.Y.O.ASSEMBLER -or- Build Your Own (Cross-) Assembler....in Forth by Brad Rodriguez - there's a lot more by him nearby including the Moving Forth series.
(I was actually hunting for what might be a mythical (8080) "assembler in 54 lines" mentioned in this history of Forth in Russia by Sergey Baranov, and eventually found a three-screen (8080) assembler by John Cassady (also here) which is probably the one mentioned. But it's in Forth Dimensions, not in Dr Dobb's as promised:
The author wrote a book on Forth which seems to have sold over 50 thousand copies - that's some enormous pent-up demand.
)
(I was actually hunting for what might be a mythical (8080) "assembler in 54 lines" mentioned in this history of Forth in Russia by Sergey Baranov, and eventually found a three-screen (8080) assembler by John Cassady (also here) which is probably the one mentioned. But it's in Forth Dimensions, not in Dr Dobb's as promised:
Quote:
Just at that time the team came across a copy of an article in the Dr. Dobb’s Journal with a
listing of an Intel 8080 assembler in Forth which took only 54 lines of text, one third of which
being a table with recognizable mnemonics of Intel 8080 assembler instructions. Especially
striking was the authors’ claim that this was a complete assembler encompassing all modes and
features of the Intel 8080 instruction set! The team spent considerable effort to clarify and
understand how the assembler was done (with the CREATE-DOES> constructs); however, as
soon as we got it, the power and beauty of this approach was greatly appreciated. As there were
no other texts on Forth available at that time, the challenge was in grasping how this suite of Forth
words worked as expected from just this listing.
listing of an Intel 8080 assembler in Forth which took only 54 lines of text, one third of which
being a table with recognizable mnemonics of Intel 8080 assembler instructions. Especially
striking was the authors’ claim that this was a complete assembler encompassing all modes and
features of the Intel 8080 instruction set! The team spent considerable effort to clarify and
understand how the assembler was done (with the CREATE-DOES> constructs); however, as
soon as we got it, the power and beauty of this approach was greatly appreciated. As there were
no other texts on Forth available at that time, the challenge was in grasping how this suite of Forth
words worked as expected from just this listing.
)
Re: 6502 Assembler in 96 lines of Forth (July 1980)
In line assembly is always cool to that but I find that most
of my assembler needs are targeted to some other machine with
no mass storage.
I always add such things as file buffering for output.
I fell in love with John Cassady's control structures and have been
using the concept ever since.
Having no requirement for a fields or returns makes it so that one
can write more coherent code as well. It begins to be readable
an much easier to add meaningful comments to.
I still write most as single pass and add the ability to have forward
labels, that must be declared.
Since I build the object code in a buffer that is flushed to a disk file,
the dictionary space can be used for linked list for forward references
to be patched.
I also have a word called SEGMENT.
It is used like:
$200 SEGMENT MyCode
$FFFE SEGMENT InterruptVector
MyCode
LABEL init-sequence
... \ some code here
LABEL IRQ
InterruptVector
IRQ A!
MyCode
... The interrupt code
It can have other uses since it doesn't have to be defined
until needed and also easily modified to span keep out areas
that the code needs to strattle. One doesn't have to think about
where some entry point might end up.
Dwight
of my assembler needs are targeted to some other machine with
no mass storage.
I always add such things as file buffering for output.
I fell in love with John Cassady's control structures and have been
using the concept ever since.
Having no requirement for a fields or returns makes it so that one
can write more coherent code as well. It begins to be readable
an much easier to add meaningful comments to.
I still write most as single pass and add the ability to have forward
labels, that must be declared.
Since I build the object code in a buffer that is flushed to a disk file,
the dictionary space can be used for linked list for forward references
to be patched.
I also have a word called SEGMENT.
It is used like:
$200 SEGMENT MyCode
$FFFE SEGMENT InterruptVector
MyCode
LABEL init-sequence
... \ some code here
LABEL IRQ
InterruptVector
IRQ A!
MyCode
... The interrupt code
It can have other uses since it doesn't have to be defined
until needed and also easily modified to span keep out areas
that the code needs to strattle. One doesn't have to think about
where some entry point might end up.
Dwight
Re: 6502 Assembler in 96 lines of Forth (July 1980)
I found the assembler I wrote in Forth enormously helpful learning the language - it really drove the point home that you don't program "in" Forth, but that you build on the core language to create your own specific language, and that you "change the problem" by finding a different syntax for the instructions. So if anybody is wondering if it is worth the effort, I would say yes, very much so.
Re: 6502 Assembler in 96 lines of Forth (July 1980)
When I first started with computers, I saw things like BASIC as being
way beyond what I'd ever do. I don't mean writing code in basic, I mean
writing a BASIC interpreter or compiler.
It was all just magic.
Once I got my hands of Forth, I realized how simple it really was. It
is really about organization. I suspect I could write one in most any language
after using Forth for some time.
I've written a number of specialize interpreters/compilers over the years.
Last night, I wrote code to create a JEDEC file for a PAL. While I could
have let PALASM do it for me, there was some special connections for
the ZEROs that I wanted to avoid glitching.
PALASM doesn't know enough about my signals to do the optimal thing.
All the magic is within a persons grasp.
I know what you mean about expanding the language. I try to explain
it to people but they never get it. It is so uniquely Forth.
It is a shame that PCs are becoming less and less computers and more
and more just web surfing boxes.
Dwight
way beyond what I'd ever do. I don't mean writing code in basic, I mean
writing a BASIC interpreter or compiler.
It was all just magic.
Once I got my hands of Forth, I realized how simple it really was. It
is really about organization. I suspect I could write one in most any language
after using Forth for some time.
I've written a number of specialize interpreters/compilers over the years.
Last night, I wrote code to create a JEDEC file for a PAL. While I could
have let PALASM do it for me, there was some special connections for
the ZEROs that I wanted to avoid glitching.
PALASM doesn't know enough about my signals to do the optimal thing.
All the magic is within a persons grasp.
I know what you mean about expanding the language. I try to explain
it to people but they never get it. It is so uniquely Forth.
It is a shame that PCs are becoming less and less computers and more
and more just web surfing boxes.
Dwight
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Quote:
It is a shame that PCs are becoming less and less computers and more and more just web surfing boxes.
Re: 6502 Assembler in 96 lines of Forth (July 1980)
BigEd wrote:
This seems like a good thread for the 1991 text document B.Y.O.ASSEMBLER -or- Build Your Own (Cross-) Assembler....in Forth by Brad Rodriguez - there's a lot more by him nearby including the Moving Forth series.
BigEd wrote:
(I was actually hunting for what might be a mythical (8080) "assembler in 54 lines" mentioned in this history of Forth in Russia by Sergey Baranov, and eventually found a three-screen (8080) assembler by John Cassady (also here) which is probably the one mentioned.
In "Zech, "Die Programmiersprache FORTH", Appendix "Assembler 8085 6502" (german) " we have an 8085 assembler implementation, maybe influenced by Cassady's version, but that's not clear. It's similar from the syntax (except it uses the comma style mnemonics), but not that compact in structure, probably with focus to a better readability. However, both have at least the <BUILDS DOES> style in common.
Zech's assemblers are now referenced on my site mentioned above.
Johann
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Dr Jefyll wrote:
FWIW I am a fan of the structured conditionals that Ragsdale included -- such as IF ELSE THEN and BEGIN UNTIL, for example, as opposed to bare assembly-language instructions ( BNE BCC etc.). Ragsdale even included error-checking (similar to that used in the high-level FIG compiler) which ensures that your conditionals are properly paired. But, frankly, for assembly-level coding I found that the error-checking can get to be a nuisance... although it's not terribly difficult to circumvent -- you can break the rules if you need to.
-- Jeff
-- Jeff
Re: 6502 Assembler in 96 lines of Forth (July 1980)
Arlet wrote:
and pretend it's still 1980.
Cat; the other white meat.