Yet another 6502 assembler

Programming the 6502 microprocessor and its relatives in assembly and other languages.
erhanbaris
Posts: 3
Joined: 21 Aug 2024

Yet another 6502 assembler

Post by erhanbaris »

I was working on some other compiler and want to get a fresh breeth, so, I worked on that project.

It support basic syntax and some preprocessor directives. It can generate binary file (ROM) but not ELF or MBF format. You can use it for NES or Atari game development. I will be happy to get feedback and improve it make it usable by who interest on that.

https://github.com/erhanbaris/timu6502asm
Last edited by erhanbaris on Wed Aug 21, 2024 11:53 am, edited 1 time in total.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Yet another 6502 compiler

Post by BigEd »

Hello, and welcome! Well done, it's always good to see a software project which is useful to 6502 users.

I think perhaps it would be best to call this an assembler - even better if you could edit the subject of this thread. Otherwise we might have a pointless reply (or two) about the use of "compiler" versus "assembler" - which to me is obviously just a difference of dialect.
erhanbaris
Posts: 3
Joined: 21 Aug 2024

Re: Yet another 6502 compiler

Post by erhanbaris »

BigEd wrote:
Hello, and welcome! Well done, it's always good to see a software project which is useful to 6502 users.

I think perhaps it would be best to call this an assembler - even better if you could edit the subject of this thread. Otherwise we might have a pointless reply (or two) about the use of "compiler" versus "assembler" - which to me is obviously just a difference of dialect.
Done :)
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Yet another 6502 assembler

Post by BigEd »

Great!

What we should really be doing is discussing the facilities your assembler offers - of course, I haven't had a good look.

Also notable, on this forum you'll find a number of unique self-built 6502 computer designs, as well as interest in the historical offerings from Commodore, Apple, Acorn, and others. We're quite well distributed over the various possible interests - including but not limited to the consoles.
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Yet another 6502 assembler

Post by BigDumbDinosaur »

Welcome to 6502 land.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Yet another 6502 assembler

Post by BigEd »

Oh, one thing I notice in your README, Erhan, is that you do offer local labels - a nice feature - but you call them 'branches'. I think everyone would say that a label is the place you get to, whereas a branch is the means of getting there.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Yet another 6502 assembler

Post by GARTHWILSON »

I'll second that.  A label is a target.  A branch is an action, as with all the conditional branch instructions BNE, BEQ, BPL, etc. (and the BRA Branch Relative Always instruction).  In the interest of source-code density, hopefully labels don't require being on their own line, but can instead be followed by code on the same line.

I suggest also changing the terminology also on variables.  What you have here:

Code: Select all

  var1 = $10
  var2 = 22
  var3 = %11001100
suggests that variable 1 is stored at address $10 (which is in ZP RAM).  Otherwise, what you have there are constants, or equates, not variables.  Variables are scratchpad records the target computer can keep changing as needed.  Most assemblers will do something like (and I'll change your "var" to "con" for "contant"):

Code: Select all

CON1:  EQU  $10
CON2:  EQU  22
CON3:  EQU  %11001100


Where you have:
Quote:
.word

Write 1 or many word information into memory


    .byte $1122
    .byte $3344, $5566
I suspect you meant to put ".word" there (in blue), not ".byte."

This may need some clarification:
Quote:
.org

Change reference locations. It is not changing where the codes are stored, it is changing jump and branch references.
because in my experience with assemblers, .org very much does change where the code is laid down.

I have a list of recommendations for if someone writes an assembler, at http://wilsonminesco.com/AssyDefense/as ... uests.html, linked on the Program Tips page of the 6502 Primer—although I see now it could stand some improvement.

In any case, writing an assembler is always a good learning experience.
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?
erhanbaris
Posts: 3
Joined: 21 Aug 2024

Re: Yet another 6502 assembler

Post by erhanbaris »

I made a small mess in the README file. I'll make the necessary correction. Since English is not my first language, I occasionally make mistakes. I appreciate all of your feedback. I'll take care of those problems as soon as I can, and feel free to request any more features that you would really like to see or use. Since I wrote it in less than ten days, not many features are available at this time. If someone is interested in my project, I intend to dedicate more effort to improving it.
I will check all those links and try to change based on those informations.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Yet another 6502 assembler

Post by BigEd »

Very impressive! Not to worry too much about your English - easily corrected!

I see you have a few minimal test files - it might be good also to have an example file which shows every feature. In particular, all of your directives.

One directive which might be useful is an alignment directive - for example to place something at the next page boundary.
John West
Posts: 383
Joined: 03 Sep 2002

Re: Yet another 6502 assembler

Post by John West »

GARTHWILSON wrote:
in my experience with assemblers, .org very much does change where the code is laid down.
That's what every assembler I've used does, and it's almost always what you want.

But not always always. Occasionally there's a need for assembling code to one location with the intent that it be copied to another before it is run (such as CHRGET in Microsoft's BASIC). Do any assemblers provide a way to do that? It's not something that would be used often, but it would be a useful feature to have.
Dietrich
Posts: 45
Joined: 01 Jan 2003

Re: Yet another 6502 assembler

Post by Dietrich »

Most assemblers can do that. I most cases the .org statement defines the stert adress of the code, but not the location of the code in the source file. Thus, by careful .ORGing und referencing the code in question can be easily copied to its destined location in memory.

Dietrich
My system: Elektor Junior Computer, GitHub https://github.com/Dietrich-L
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Yet another 6502 assembler

Post by BigDumbDinosaur »

John West wrote:
GARTHWILSON wrote:
in my experience with assemblers, .org very much does change where the code is laid down.
That's what every assembler I've used does, and it's almost always what you want.

But not always always. Occasionally there's a need for assembling code to one location with the intent that it be copied to another before it is run (such as CHRGET in Microsoft's BASIC). Do any assemblers provide a way to do that? It's not something that would be used often, but it would be a useful feature to have.

A common trick seen with 6502-powered home computers from the late 1970s and early 1980s was loading a program into the stack, which would cause it to automatically run when loading was finished.  Obviously it wasn’t possible to assemble directly into the stack—the assembler would lose control of the machine, so the program was assembled for the stack address, but actually deposited elsewhere in RAM for saving to local mass storage.

The HCD-65 DEVPAK assembler for the Commodore 128 emitted an object file in MOS Technology portable format (similar to an S-record file), rather than an actual binary.  A loader included with the assembler was then used to place a binary image of the program anywhere desired in memory, regardless of assembly address.  That made it possible to assemble for the stack or zero page without actually overwriting either area.
x86?  We ain't got no x86.  We don't NEED no stinking x86!
vbc
Posts: 80
Joined: 23 Apr 2020

Re: Yet another 6502 assembler

Post by vbc »

John West wrote:
But not always always. Occasionally there's a need for assembling code to one location with the intent that it be copied to another before it is run (such as CHRGET in Microsoft's BASIC). Do any assemblers provide a way to do that? It's not something that would be used often, but it would be a useful feature to have.
vasm supports the rorg directive to do that:

Code: Select all

$ cat r.s
 org $00
lab1:
 jmp lab1

 org $20
lab2:
 jmp lab2

 rorg $8000
lab3:
 jmp lab3

 org $10
lab4:
 jmp lab4
$ vasm6502_oldstyle -quiet -Fbin r.s
$ hexdump -C a.out 
00000000  4c 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |L...............|
00000010  4c 10 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |L...............|
00000020  4c 20 00 4c 00 80                                 |L .L..|
User avatar
GARTHWILSON
Forum Moderator
Posts: 8773
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: Yet another 6502 assembler

Post by GARTHWILSON »

erhanbaris, your English is very good for someone whose first language is not English.
John West wrote:
GARTHWILSON wrote:
in my experience with assemblers, .org very much does change where the code is laid down.
That's what every assembler I've used does, and it's almost always what you want.

But not always always. Occasionally there's a need for assembling code to one location with the intent that it be copied to another before it is run (such as CHRGET in Microsoft's BASIC). Do any assemblers provide a way to do that? It's not something that would be used often, but it would be a useful feature to have.
...and then there are linkers, which I suppose are primarily for when multiple people are working on different parts of the same project.  The first commercial assembler I used, 2500AD, used a linker, and I never did figure it out, only use the command line that was recommended by someone else, modifying it for each project's file names.  I haven't used that assembler in over 30 years though.
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
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Yet another 6502 assembler

Post by BigEd »

I haven't studied the behaviour of org in this assembler. But one common little difficulty - not really a difficulty, but often seen as one - is that, for example, code which lives in a ROM which will ultimately be accessed at say $8000 will have addresses in the ROM which start at $0000. There's a difference between the offset in ROM, and the address the 6502 sees.

I think this difficulty is resolved by the way in which the assembler can produce a binary, or the way in which that binary is used to produce a ROM image.

(In the case of emulators, of course the ROM image will be a binary file loaded by a program. In the case of physical systems, the ROM image will be fed to a programmer, or built into a bitstream, or loaded by a monitor.)
Post Reply