Writing an assembler

Programming the 6502 microprocessor and its relatives in assembly and other languages.
sje
Posts: 9
Joined: 01 Jul 2007

Writing an assembler

Post by sje »

I spent some time recently looking at freeware 6502 assemblers on the net. There are plenty of them, but none that work the way I'd like to see (e.g., portable source, correct operation, high capacity, ease of use, macros, and scoping). So I'm writing my own. Hopefully I'll have it done before my MicroKIM shows up on my doorstep.

I've had the program produce a 6502 opcode decoding list as a test. The easily program readable list is available at:

http://homepage.mac.com/chessnotation/6502decode

The assembler will include a disassembly service, and I'm thinking of using it on the PROM contents from a few 6502 based dedicated chess machines. I guess that's okay as I don't recall signing a "no reverse engineering" End User License Agreement when I purchased any Fidelity, Novag, or Saitek machine.

Here's a snippet from my program's disassember output from scanning part of the KIM-1's ROMs:

Code: Select all

1800 a9ad   lda #$ad
1802 8eec17 stx $17ec
1805 203219 jsr $1932
1808 a927   lda #$27
180a 8d4217 sta $1742
180d a9bf   lda #$bf
180f 8d4317 sta $1743
1812 a264   ldx #$64
1814 a916   lda #$16
1816 207a19 jsr $197a
1819 ca     dex
181a d0f8   bne $1814
181c a92a   lda #$2a
181e 207a19 jsr $197a
1821 adf917 lda $17f9
1824 206119 jsr $1961
1827 adf517 lda $17f5
182a 205e19 jsr $195e
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Post by fachat »

Quote:
I spent some time recently looking at freeware 6502 assemblers on the net. There are plenty of them, but none that work the way I'd like to see (e.g., portable source, correct operation, high capacity, ease of use, macros, and scoping). So I'm writing my own.
Just wondering - do you mean 6502 native assembler? or cross- assember?

If the latter, what were your gaps with the ones you tried (ok, specifically with xa, because I did that one.... :-)

And what keeps you from modifying one of the open source variants?

Thanks
André
sje
Posts: 9
Joined: 01 Jul 2007

Post by sje »

I need a cross assembler. I also need an integrated disassembler and hooks for an emulator. I'm rather particular about syntax and pseudo op format issues, so the assembler should have flexibility here, or, even better, be able to modify the source file using one syntax style into my style.

Other than the occasional assembly language foray, I only use C++ and Lisp. I haven't done a major C only project in well over a decade.
VBR
Posts: 25
Joined: 29 May 2007

Post by VBR »

Just what the world needs, another 6502 assembler. :-)

I'm not trying to spoil your fun, but there are so many already, it's hard to believe that none are suitable. Did you try ca65, for example?
sje
Posts: 9
Joined: 01 Jul 2007

Post by sje »

I looked at a whole bunch of them, and at this point I can't recall the specifics of (almost) any of them. Trust me, I would not write an assembler if I had located an already existing one that was suitable for my purposes.

My assembler is coming along nicely and should be ready after a couple more coding sessions. I've got it producing KIM-1 paper tape format object output as an option. (Complete with the six end of record NULs and the trailing X-Off byte; it all checks out.)

Oh, and the first assembler I wrote was for the IBM/370. Back in 1976.
VBR
Posts: 25
Joined: 29 May 2007

Post by VBR »

Are you writing a linker also or just going straight to executable?
sje
Posts: 9
Joined: 01 Jul 2007

Post by sje »

The object output format will be selectable and will include relocatable output suitable for linking. I'll do a linker as well.
kc5tja
Posts: 1706
Joined: 04 Jan 2003

Post by kc5tja »

All this sure seems like reinventing the wheel. cc65 and the remainder of the toolchain (which includes ca65) is an outstanding product that is capable of producing a raw binary image as well as relocatable output modules. As I've not seen a specific list of grievances against this (or any other) assembler, I can only assume that the grievances are purely syntactical in nature.
sje
Posts: 9
Joined: 01 Jul 2007

Post by sje »

Your assumption is erroneous.

See my earlier post. I don't spend time on hacking old style C sources. I need named scopes, extensive programmable pseudo op capability, service routine hooks for a working emulator, and several other features. I'm not willing to compromise and neither am I willing to wait for someone else to supply the features I need. And quite honestly, I don't feel obligated to detail every insufficiency of every freeware assembler out there.

If you're happy with a particular product, well then, that's fine. Kindly extend the same right of evaluation to others.
kc5tja
Posts: 1706
Joined: 04 Jan 2003

Post by kc5tja »

Well, folks have asked you what the deficiencies you found were. You failed to answer in any significant detail. I made an assumption based on data which was not available.

No, you're not obligated to provide the information we asked for, out of courteous curiosity. But, by the same token, you ARE obligated to at least expect people to guess given insufficient data. And, you ARE also expected (but not obligated) to not give any lip about it if they do. A polite, emotionally uncharged correction would be nice, of course.
DonnaD
Posts: 33
Joined: 05 Jul 2007

Post by DonnaD »

I am looking for a freeware native 6502 assembler to integrate into my homebrew computer. Does anyone know of one? I've searched, but have come up empty handed. Even a ML monitor would suffice.
User avatar
8BIT
Posts: 1787
Joined: 30 Aug 2002
Location: Sacramento, CA
Contact:

Post by 8BIT »

My SBC-2 OS has a monitor based on the Apple 2e, including a mini-assembler (similar to the apple 2 mini-assembler). It supports 65c02 opcodes but does not use labels... you enter it one line at a time with hexidecimal values and addresses. branches are calculated for you, both forward and backward.

IO uses input and output character vectors, so you can taylor it to your IO devices.

Its not exactly what you want, but it is FREE.

EDIT: Additional comments:
If you are communicating through an RS-232 port, you can create files on your PC and do a text transfer and let the mini-assembler assemble your code on the fly. There are limitations and you'll have to work a little to get a finished program but the benefit is having your source in assembly stored on your PC. I can expand on this process if anyone is interested.
/EDIT


Daryl

http://sbc.rictor.org/software/sbcos.html
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Post by fachat »

DonnaD wrote:
I am looking for a freeware native 6502 assembler to integrate into my homebrew computer. Does anyone know of one? I've searched, but have come up empty handed. Even a ML monitor would suffice.
On http://www.6502.org/users/andre/misc/index.html you find my C64 @ASS and @MON native assembler and monitor. You will have to adopt to other systems, but that should not be a problem. I've ported it to the PET as well as to my selfbuilt GeckOS (multi-platform) operating system http://www.6502.org/users/andre/osa/index.html

I have to admit the code is not very beautiful due to less experience on my side when I wrote it, space constraints, and not-so-advanced tools then.

Hope this helps
André

EDIT: I only ported @MON to the PET and GeckOS. @ASS somehow relies on the Commodore BASIC file layout, as you type the source in the BASIC editor.
sje
Posts: 9
Joined: 01 Jul 2007

Some necessary features

Post by sje »

Work continues on my assembler project. As mentioned before, I need extensive pseudo op functionality, and this includes fairly general arithmetic expressions. These constant value expression may appear as the right hand side of a set or equ pseudo op, and also may appear as the displacement field when constructing an effective address. General expressions may also appear in other places.

The lexer, parser, and error manager has been done for the above. It uses C/C++ expression syntax and implements 23 different operators with 12 different precedence levels. Parentheses are used to override default precedence or for adding textual clarity. Also, tab and space characters can be used between tokens as desired although they are not needed.

Because parentheses are used for arithmetic operand grouping, square brackets are used to indicate indirection in address operands as needed.

A separate and simpler parser handles address operands; it calls the expression parser when required.

A constant expression value that changes from pass to pass is correctly flagged as a phase error.

--------

In general, the complexity of the user source program is limited only by the available virtual memory on the host. The symbol manager uses a list of scopes, and the symbols in each scope are organized using a self balancing (AVL) tree for speed.

--------

As with C/C++, user identifiers are unlimited in length and are case sensitive. Names for CPU opcodes, pseudo ops, and the three registers are not case sensitive on input. Collisions between user and reserved symbols are correctly detected.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Post by GARTHWILSON »

Is part of the idea to make a better free assembler available? I have not tried any of the free ones as my company bought me Universal Cross-Assembers' C32 15 years ago; but although that one is not free, it was only $200 and was good for scores of processors, not just the 6502, and gives you the tools to adapt it for new processors that may not have been designed yet when the assembler was sold. IOW, it should basically be the last assembler you'd ever need. I don't think I hear you telling of anything it can't do; but there is one thing I would like in a cross-assembler that I have not seen so far. Although they allow assembler variables, they do not give the tools to effectively make and use assembler variable arrays. What I would like the array for is to make stacks, so that with macros, I can make and nest program structures in assembly, and have more-structured assembly-language code without all the labels.
Post Reply