6502 Assembler in 96 lines of Forth (July 1980)

Topics relating to various Forth models on the 6502, 65816, and related microprocessors and microcontrollers.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

6502 Assembler in 96 lines of Forth (July 1980)

Post by BigEd »

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
Last edited by BigEd on Sun Mar 27, 2016 8:50 am, edited 2 times in total.
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by Dr Jefyll »

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
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
User avatar
GARTHWILSON
Forum Moderator
Posts: 8775
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by GARTHWILSON »

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.
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?
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by Dr Jefyll »

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.
Fer sure; assemblers are a great example of an application that Forth excels at.

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 :D

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
User avatar
JeeK
Posts: 18
Joined: 14 Aug 2012
Location: Austria - Vienna
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by JeeK »

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.
Fer sure; assemblers are a great example of an application that Forth excels at.
[..]
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
At the time Jeff's post appeared I decided to offer my 65C02 assembler (written longer ago), if Jeff doesn't find his. It took some time to get it on-line, after some "face-lifting", however, here it is: Forth 65C02 assembler
(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 :twisted: )
User avatar
Dr Jefyll
Posts: 3526
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by Dr Jefyll »

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?
Quote:
At the time Jeff's post appeared I decided to offer my 65C02 assembler (written longer ago), if Jeff doesn't find his.
Oops, I have yet to follow through on the search for that code. Unfortunately, it's probably on a non-DOS 5-inch floppy. :shock: 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
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
JeeK
Posts: 18
Joined: 14 Aug 2012
Location: Austria - Vienna
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by JeeK »

Dr Jefyll wrote:
Welcome, Johann -- nice to have another Forth enthusiast in our midst! :)
Thanks. I'm on this forum since 2012 (irregularly from time to time). The postings and the discussion level here is really "advanced", so it took some time to step in.
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?
This is not indented for some kind of automatic testing. I forgot to mention, that I cross-check the produced code just with a (my Forth) disassembler and it is necessary to tweak the test code to get it compiled (e.g. insert the keyword "FAR", to produce long branches based on JMP ...
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.
Oops, I have yet to follow through on the search for that code. Unfortunately, it's probably on a non-DOS 5-inch floppy. :shock: 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.
Oh my god, I had the floppy problem with my "OS-9 Level I" stuff (single density - FM formated, partially), not easily to read on PC hardware, but got it, but with a selected equipment (nothing special, anyway).
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by BigEd »

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:
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.
The author wrote a book on Forth which seems to have sold over 50 thousand copies - that's some enormous pent-up demand.
)
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by dwight »

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
scotws
Posts: 576
Joined: 07 Jan 2013
Location: Just outside Berlin, Germany
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by scotws »

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.
dwight
Posts: 213
Joined: 08 Jun 2004

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by dwight »

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
User avatar
Arlet
Posts: 2353
Joined: 16 Nov 2010
Location: Gouda, The Netherlands
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by Arlet »

Quote:
It is a shame that PCs are becoming less and less computers and more and more just web surfing boxes.
On the other hand, there are plenty of cheap and fun embedded processors and boards. Yesterday I ordered a $10 microcontroller kit, containing a $1.50 microcontroller and free USB programmer. It has 64kB of flash and 8kB of SRAM, timers, UART, I2C, SPI, RTC and ADC. Tools and demo programs are free. If you want, you can put Forth on it, and pretend it's still 1980.
User avatar
JeeK
Posts: 18
Joined: 14 Aug 2012
Location: Austria - Vienna
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by JeeK »

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.
B.Y.O.ASSEMBLER perfectly describes how this kind of assembler are designed and built. Really important referring to it, as I now added it to my site. Thanks for this pointer.
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.
Very interesting.
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
JimBoyd
Posts: 931
Joined: 05 May 2017

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by JimBoyd »

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
The assembler I wrote for my Forth for the Commodore 64 is also based on Ragsdale's assembler. I rewrote M/CPU and the index table is ten bytes smaller. I added range checking for the instructions that compile a branch. One change I'm thinking about is removing the comma's at the end of the opcode and control structure names. On the other hand, the code words for the system are written with the comma's in the names and I'd have to rewrite it.
User avatar
cbmeeks
Posts: 1254
Joined: 17 Aug 2005
Location: Soddy-Daisy, TN USA
Contact:

Re: 6502 Assembler in 96 lines of Forth (July 1980)

Post by cbmeeks »

Arlet wrote:
and pretend it's still 1980.
Which is, basically, every single day of my life. I live in the 80's and refuse to accept any other timeline.
Cat; the other white meat.
Post Reply