Assembler with structured conditionals
Re: Assembler with structured conditionals
I wonder if there is a lack of interest in Forth assemblers with structured conditionals or if Ragsdale's is the only one with which most Forth programmers are familiar? Here is the source for mine.
Any comments, questions, suggestions?
[Edit: It's released under the LGPL version 2 or later. Do I need to include a copy of the license?]
[Edit: Included license]
[Edit: It's released under the LGPL version 2 or later. Do I need to include a copy of the license?]
[Edit: Included license]
Last edited by JimBoyd on Tue Dec 04, 2018 9:19 pm, edited 2 times in total.
Re: Assembler with structured conditionals
The problem with Forth assemblers isn't necessarily the syntax (as off-putting as it may be to some), but the fact that it's in Forth in the first place.
If it were just a stand alone program that takes a text file and spits out an assembled binary, then it's simply a different syntax rather than an entirely different tool chain.
and, yes, you should include a copy of the license in the ZIP file.
If it were just a stand alone program that takes a text file and spits out an assembled binary, then it's simply a different syntax rather than an entirely different tool chain.
and, yes, you should include a copy of the license in the ZIP file.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Assembler with structured conditionals
The first 6502 Forth I worked with (around 1990 or '91) was with a metacompiler at work, which had the postfix assembly language and structured conditionals. I absolutely hated the postfix assembly, which is why, when I wrote my own Forth assembler, I made the mnemonic come first like in normal assemblers and then you comma-in the operand, like AND_ABS foobar , . I really like using the program flow control macros in a normal assembler though, and although I did not become very fond of what came with the metacompiler in that regard (because of the first problem), Forth does afford a natural macro capability, so I might implement the macros in Forth if I ever write anything in assembly long enough in the Forth environment for that to matter. Most of these things are pretty short though, just primitives, runtimes, subroutines, and whatever ISRs are not Forth. They're definitely not whole applications. I have a longer example in a non-Forth environment near the end of this page, and a couple above it that might be interesting.
One big advantage is that Forth names can include almost any characters. It's just a tad frustrating that normal assemblers see certain characters and think you're trying to do an operation between the spaces, for example R/W_SPI (since SPI sends and receives a byte at the same time). Another advantage in Forth is that you can do complex operations between assembly-language instructions to prepare operands, data, conditional assembly, etc., much more than you can with a normal assembler.
Quote:
The problem with Forth assemblers isn't necessarily the syntax (as off-putting as it may be to some), but the fact that it's in Forth in the first place.
One big advantage is that Forth names can include almost any characters. It's just a tad frustrating that normal assemblers see certain characters and think you're trying to do an operation between the spaces, for example R/W_SPI (since SPI sends and receives a byte at the same time). Another advantage in Forth is that you can do complex operations between assembly-language instructions to prepare operands, data, conditional assembly, etc., much more than you can with a normal assembler.
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: Assembler with structured conditionals
GARTHWILSON wrote:
... I might implement the macros in Forth if I ever write anything in assembly long enough in the Forth environment for that to matter.
Forth assemblers (and when I say "Forth assemblers", I'm talking the typical Forth Assembler most of us are familiar with rather than some edge case) are for writing words for a Forth environment, rather than large assembly language programs. So, they're not, typically, designed for some of the problems that regular assemblers are designed for.
Notably, Forth assemblers aren't set up to manage or work with libraries and object files. They don't need a linker. They also don't offer things like cross references, reference tables (labels and their assignments), etc.
Early Forth assemblers were also, naturally, limited by the BLOCK structure of Forth source code, which is why much of it is written with several instructions on a single line vs one instruction per line in traditional assemblers.
Forth assemblers are single pass assemblers as well.
Basically consider someone writing Prince of Persia using a Forth assembler.
There's nothing to stop someone from writing a full boat, stand alone, full featured assembler using the RPN syntax. But, in the end I don't think it would save much in terms of the assembler code, or how wide of an appeal it would have in the end.
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Assembler with structured conditionals
The term "Forth assembler" conjures up in my mind a very small assembler that's just there to support Forth in the way we both stated above.
It could be done, but it's just a bit cumbersome and I'm sure the need is rare. In my assembler I do have a set of about two dozen constants that return addresses in the Forth system, including things like NEXTadr, PUSH, CPUSH, and N.
I use text source files, but I do like the ability to put multiple assembly-language instructions on a line, grouping them appropriately, and reducing the number of lines. I suppose a normal assembler could be written to do this, but I have not seen one. It should be pretty simple.
Heh heh, not a chance—unless the idea is that it's mostly Forth, with the performance-critical parts being done in these short assembly sections the typical Forth assembler is intended for.
Quote:
They also don't offer things like cross references, reference tables (labels and their assignments), etc..
It could be done, but it's just a bit cumbersome and I'm sure the need is rare. In my assembler I do have a set of about two dozen constants that return addresses in the Forth system, including things like NEXTadr, PUSH, CPUSH, and N.
Quote:
Early Forth assemblers were also, naturally, limited by the BLOCK structure of Forth source code, which is why much of it is written with several instructions on a single line vs one instruction per line in traditional assemblers.
I use text source files, but I do like the ability to put multiple assembly-language instructions on a line, grouping them appropriately, and reducing the number of lines. I suppose a normal assembler could be written to do this, but I have not seen one. It should be pretty simple.
Quote:
Basically consider someone writing Prince of Persia using a Forth assembler.
Heh heh, not a chance—unless the idea is that it's mostly Forth, with the performance-critical parts being done in these short assembly sections the typical Forth assembler is intended for.
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: Assembler with structured conditionals
JimBoyd wrote:
I wonder if there is a lack of interest in Forth assemblers with structured conditionals or if Ragsdale's is the only one with which most Forth programmers are familiar?
There has, however, been a fair amount of discussion here on this forum. It's not all tidily arranged in one thread, unfortunately -- that can't 100% be expected, given the way discussions evolve!
In addition to this thread, here are two others with pertinent remarks:
- This and nearby posts in the Petil thread pertain to CASE statements.
Self Modifying Code (perhaps too broad a title, as you didn't mention the intended context of structured conditionals)
-- 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: Assembler with structured conditionals
Well, there's this example:
http://www.forth.org/fig-forth/fig-forth_68000.pdf
This is an implementation of Fig-Forth written in an RPN assembler (itself written in Forth). Though if you look at it, you'll see it's more traditional assembler than the typical utility in a Forth system to build assembler based words.
AT A GLANCE, this listing is almost unreadable, to me. By that I mean I would have to give it some thorough study to get past the syntax that they are using. But if you notice, this assembler does NOT offer structured conditionals or loops (this is readily apparent in the FIND word, where you see FIND1, FIND2, etc. labels.
I only mention this as an example of the momentum that typical assembler syntax has, being pretty common across CPUs and instructions sets.
RPN as an expression representation has utility to folks, particularly in calculators as it just works better on going calculations. But having as assembler of this scope relying on RPN, is really designed as a benefit to the computer, not the developer. I don't think I would necessarily enjoy working with the assembler syntax presented here.
But, then, we can get used to anything.
http://www.forth.org/fig-forth/fig-forth_68000.pdf
This is an implementation of Fig-Forth written in an RPN assembler (itself written in Forth). Though if you look at it, you'll see it's more traditional assembler than the typical utility in a Forth system to build assembler based words.
AT A GLANCE, this listing is almost unreadable, to me. By that I mean I would have to give it some thorough study to get past the syntax that they are using. But if you notice, this assembler does NOT offer structured conditionals or loops (this is readily apparent in the FIND word, where you see FIND1, FIND2, etc. labels.
I only mention this as an example of the momentum that typical assembler syntax has, being pretty common across CPUs and instructions sets.
RPN as an expression representation has utility to folks, particularly in calculators as it just works better on going calculations. But having as assembler of this scope relying on RPN, is really designed as a benefit to the computer, not the developer. I don't think I would necessarily enjoy working with the assembler syntax presented here.
But, then, we can get used to anything.
Re: Assembler with structured conditionals
GARTHWILSON wrote:
The first 6502 Forth I worked with (around 1990 or '91) was with a metacompiler at work, which had the postfix assembly language and structured conditionals. I absolutely hated the postfix assembly
Re: Assembler with structured conditionals
whartung wrote:
The problem with Forth assemblers isn't necessarily the syntax (as off-putting as it may be to some), but the fact that it's in Forth in the first place.
If it were just a stand alone program that takes a text file and spits out an assembled binary, then it's simply a different syntax rather than an entirely different tool chain.
and, yes, you should include a copy of the license in the ZIP file.
If it were just a stand alone program that takes a text file and spits out an assembled binary, then it's simply a different syntax rather than an entirely different tool chain.
and, yes, you should include a copy of the license in the ZIP file.
Code: Select all
HEX
C000 BOTTOM!
SUB FLASHER
0 # LDA, TAX, TAY,
BEGIN,
D020 STA, CLC, 1 # ADC,
CS-DUP 0= UNTIL,
INY,
CS-DUP 0= UNTIL,
INX,
0= UNTIL,
RTS,
END-CODE
This short piece was saved with the metacompiler word TSAVE and loads at address $C000 or decimal 49152 ( an address familiar to Commodore 64 users who also programmed their machines ). The screen shot shows the short BASIC program that uses this routine. The screen shot was taken during the test. Yes it completed successfully. This has given me food for though about the metacompiler. Currently, the source is compiled with MLOAD or MTHRU but, writing MINCLUDE, a metacompiler version of INCLUDE will be easy to write but, there may be a better way. I'll have to go through the metacompiler code and clean it up and maybe rethink some of the metacompiler design.
Cheers,
Jim
P.S. BOTTOM! could be renamed ORIGIN.
Re: Assembler with structured conditionals
Dr Jefyll wrote:
In addition to this thread, here are two others with pertinent remarks:
- This and nearby posts in the Petil thread pertain to CASE statements.
Self Modifying Code (perhaps too broad a title, as you didn't mention the intended context of structured conditionals)
Quote:
BTW, I'll quickly inject a comment of my own. Ragsdale's assembler (and yours, I think) check to see that conditionals are properly paired, which means (for example) that BEGIN leaves both an address and an error-check code on stack and UNTIL removes these two items. Usually that's fine, but in certain situations the error-check code becomes a nuisance, leading to extra SWAPs ROTs etc. I daresay there's no perfect solution, but perhaps it'd be best if the error-check codes (but not the addresses) got pushed & popped from their own separate stack. (This is a variation of an idea Garth suggested elsewhere.)
-- Jeff
-- Jeff
- GARTHWILSON
- Forum Moderator
- Posts: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Assembler with structured conditionals
JimBoyd wrote:
GARTHWILSON wrote:
The first 6502 Forth I worked with (around 1990 or '91) was with a metacompiler at work, which had the postfix assembly language and structured conditionals. I absolutely hated the postfix assembly
Part of the problem with postfix assembly is not the postfix itself, but the difficulty in applying vertical alignment for visual factoring, since the addressing mode is separate, and operands are of varying lengths (regardless of what system is being used). Sometimes operands must be calculated from a longish line of source code; so vertical alignment of instruction mnemonics would require putting them waaaaay off to the right; and then if you want to indent a portion for a structure, well, forget it. Now there's no room for comments either.
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: Assembler with structured conditionals
GARTHWILSON wrote:
Part of the problem with postfix assembly is not the postfix itself, but the difficulty in applying vertical alignment for visual factoring, since the addressing mode is separate, and operands are of varying lengths (regardless of what system is being used). Sometimes operands must be calculated from a longish line of source code; so vertical alignment of instruction mnemonics would require putting them waaaaay off to the right; and then if you want to indent a portion for a structure, well, forget it. Now there's no room for comments either.
Code: Select all
HEX
// CLOSE ALL OPEN FILES
CODE ACLOSE ( -- )
XSAVE STX,
BEGIN,
98 LDY,
0= NOT WHILE,
258 ,Y LDA, // LAST OPENED
FFC3 JSR, // CLOSE
REPEAT,
XSAVE LDX,
NEXT JMP, END-CODE
Re: Assembler with structured conditionals
Dr Jefyll wrote:
[...] Self Modifying Code (perhaps too broad a title
JimBoyd wrote:
Sorry about that, I kinda got sidetracked on that one. The disscussion digressed to a discussion on fixing that ugly hand calculated offset in my example SMC.
Dr Jefyll wrote:
[...] I daresay there's no perfect solution, but perhaps it'd be best if the error-check codes (but not the addresses) got pushed & popped from their own separate stack.
JimBoyd wrote:
That's why I've got CS-DUP CS-DROP CS-SWAP and CS-ROT which also work with high level Forth conditionals. I just treat the address and security number as a single unit.
Unfortunately, after refreshing myself on the subject (it's been years since I involved myself with this) I have no dramatic improvement to offer. In my notes I did find the following...
Code: Select all
: IF[WITHIN], ( like IF, but used within an assembler structured conditional )
>R >R [COMPILE] IF,
R> R> ; IMMEDIATEcheers,
Jeff
ps: returning to the subject of clean and tidy coding, don't you think you should used a named constant in cases like this?
Code: Select all
98 LDY,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: 8773
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Assembler with structured conditionals
If I implement the structured conditionals, I might do something like:
(My Forth assembler requires comma'ing in the operands.) One thing I would definitely do is put the test condition on the line before the WHILE, so the first character of WHILE, can be vertically aligned with that of BEGIN, and REPEAT,. Same thing with an IF, for example. It's just (very strong) personal preference. That's a nice thing about Forth though, and maybe one reason that managers sometimes aren't too fond of Forth. It allows us free thinkers to do things our own way, independently of how others do it. It's also one reason Forth will never die. Since we can get under the hood and modify or extend even the very compiler and assembler, it won't affect us much if commercial support were ever to disappear.
Edit: In my assembler macros for the normal (non-Forth) assembler, I put the condition with the IF, WHILE, etc., like IF_EQ, WHILE_NEG, etc..
Code: Select all
HEX
CODE ACLOSE ( -- ) \ Close all open files.
STX_ZP XSAVE C,
BEGIN,
LDY_ZP 98 C, \ (but give a name, as Jeff suggests)
0<>
WHILE,
LDA_ABS 258 C, \ LAST OPENED
JSR FFC3 , \ CLOSE
REPEAT,
LDX_ZP XSAVE C,
JMP NEXT ,(My Forth assembler requires comma'ing in the operands.) One thing I would definitely do is put the test condition on the line before the WHILE, so the first character of WHILE, can be vertically aligned with that of BEGIN, and REPEAT,. Same thing with an IF, for example. It's just (very strong) personal preference. That's a nice thing about Forth though, and maybe one reason that managers sometimes aren't too fond of Forth. It allows us free thinkers to do things our own way, independently of how others do it. It's also one reason Forth will never die. Since we can get under the hood and modify or extend even the very compiler and assembler, it won't affect us much if commercial support were ever to disappear.
Edit: In my assembler macros for the normal (non-Forth) assembler, I put the condition with the IF, WHILE, etc., like IF_EQ, WHILE_NEG, etc..
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: Assembler with structured conditionals
whartung wrote:
and, yes, you should include a copy of the license in the ZIP file.