About 6502 common syntax standards

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
User avatar
Banshaku
Posts: 2393
Joined: Tue Jun 24, 2008 8:38 pm
Location: Japan
Contact:

About 6502 common syntax standards

Post by Banshaku » Sun Aug 16, 2020 3:10 am

Here a question that may ends without clear answer but will ask it anyway :lol:

When I ported code from ca65 to vasm (vbcc assembler), a lot of the way you wrote code were not the same. For example, labels has to be on the first column (very common) but all other command line segment, include etc cannot. Same thing for command for macro definition.

This made me realize that the way I wrote my code was very ca65 specific and I should try to write it in a more general way to avoid such issues if the need arise to change/port code to another assembler (enum seems not common either).

My question is, what are other points that should be considered to write very common asm?

tepples
Posts: 22049
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: About 6502 common syntax standards

Post by tepples » Sun Aug 16, 2020 7:25 am

NESASM likewise requires labels in column 1 and everything else in a later column.

User avatar
Banshaku
Posts: 2393
Joined: Tue Jun 24, 2008 8:38 pm
Location: Japan
Contact:

Re: About 6502 common syntax standards

Post by Banshaku » Sun Aug 16, 2020 8:39 am

I see, so nesasm does that too. In the case of vasm, they call it "old syntax", which seem to imply that it's the older way of doing thing. Still, how common is ca65 syntax then?

My goal is to try to write code in a way that could reduce the pain when using it with a different assembler. If the restriction are not too strict then I wouldn't mind it but it's hard to find what are the rules to follow to do such thing.

For now I'm following to some degree what vasm does after porting code to it. I'm not sure what to do with enum though.

User avatar
tokumaru
Posts: 11858
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: About 6502 common syntax standards

Post by tokumaru » Sun Aug 16, 2020 9:25 am

I find it a bit lame when assemblers have strict rules about the absolute placement of things in a line of code. The relative positions should be enough for them to identify what's what.

Assembly code isn't very portable anyway unless you only use global labels and drastically cut down on the use of assembler directives. AFAIK, the only compatible way to declare variables and constants is to do it the old way and assign each value/address explicitly, which sucks because it's so hard to manage and so error-prone. Besides ORG, BYTE/DB and WORD/DW, you can't really count on assemblers having a consistent set of directives.

Oziphantom
Posts: 913
Joined: Tue Feb 07, 2017 2:03 am

Re: About 6502 common syntax standards

Post by Oziphantom » Sun Aug 16, 2020 10:23 am

the old parsers written on machines that have tiny amounts of RAM use the
directive opcode comment
format. Turbo Assembler, Merlin, PDS, HP64000 assembler et al will use this format as it make parsing the code which was hand written in asm much easier to write.
Most assemblers will at least give a warning if you put a label not on char 0, or in say WLA-DX cases give you somewhat fun results.

to be portable you basically
all labels 8 characters or less Typically all caps but you can probably get away with upper and lower case for the most part probably fair to go to 16 characters now.
labels with : on the end, some need it, others are happy to ignore it for "compatibility" reasons.
don't use local labels, or + - directives.
macros are well and truly in "nothing standard"

ca65 is somewhat unique because it lives with a C compiler.

tepples
Posts: 22049
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: About 6502 common syntax standards

Post by tepples » Sun Aug 16, 2020 11:12 am

Banshaku wrote:
Sun Aug 16, 2020 8:39 am
I see, so nesasm does that too. In the case of vasm, they call it "old syntax", which seem to imply that it's the older way of doing thing. Still, how common is ca65 syntax then?
There are three general behaviors, which I'll call "column 1 labels", "labels with colon", and "labels anywhere". A column 1 label is not followed by a colon. It must be first on the line, with no preceding whitespace. A label with colon may have whitespace before it but must be followed by a : character. An ASM6 label is any word preceding the instruction or macro name that isn't already an instruction, macro, or label, including any column 1 label or label with colon.

Some assemblers support a "cheap local label", valid only between two ordinary labels. This allows reusing a common label name, such as @loop, in different subroutines. They may also support a one-directional label, for which searching goes only up or down, intended for use with tight loops a dozen lines or fewer.
  • NESASM recognizes column 1 labels. A following : is ignored. Cheap local labels begin with .. Indirection uses []. (Source: NESASM3 usage.txt)
  • ASM6 recognizes ASM6 labels, which tend to cause missed errors when a source file misspells an instruction or macro name. It ignores leading whitespace. Cheap local labels begin with @. One-directional labels are + for down, - for up, or anything beginning with + or -. The manual doesn't mention whether indirection uses () or []. (Source: ASM6 README)
  • The assembler in GNU Binutils targets numerous instruction sets, mostly 32- and 64-bit. For most instruction sets, it recognizes labels with colon. One-directional labels are named with only digits 0 through 9, referred to with 1f for down or 3b for up. Labels not to be exported begin with .L. Cheap local labels are named with digits followed by $. (For HP PA-RISC, the GNU assembler instead recognizes column 1 labels, and labels not to be exported begin with L$.) (Source: Using as, section "Labels" and "Symbol Names")
  • ca65 (targeting 6502-like CPUs and anything else that has a macro pack defined) recognizes labels with colon. Using .feature labels_without_colons causes it to also recognize column 1 labels. Cheap local labels begin with the character set with .localchar, usually @. The only one-directional label is :, referred to with :+ for down, :- for up, :++ for down skipping one, etc. Indirection uses () for short or [] for 65816-only long, unless .feature bracket_as_indirect is on (which can't be used on 65816). (Source: ca65 Users Guide sections Standard labels and .FEATURE)
  • RGBASM (targeting the Game Boy CPU) recognizes column 1 labels. It allows (but ignores) one trailing colon for file-local labels, and it exports any label followed by two colons. Cheap local labels begin with .. Starting with version 0.4.0, RGBDS also recognizes cheap local labels preceded by whitespace thanks to lexer improvements by ISSOtm. Indirection uses (). (Source: man 5 rgbasm, section SYMBOLS) Prior to 0.4.0, I had used a dedent script to translate source files from labels with colon to column 1 labels before feeding them to RGBASM.
  • WLA DX (targeting several Z80-like and 6502-like CPUs) recognizes labels with colon. Cheap local labels start with @. Labels not to be exported from the section start with _. One-directional labels (style +-) are any number of + for down or any number of - for up. One-directional labels (style _) are __ for the label, _f for downward reference, and _b for upward reference. Both [] and () are recognized for indirection. One-directional labels appear not to need the colon. (Source: WLA DX manual)

zzo38
Posts: 1075
Joined: Mon Feb 07, 2011 12:46 pm

Re: About 6502 common syntax standards

Post by zzo38 » Sun Aug 16, 2020 3:19 pm

Also of note perhaps is that NESASM (and MagicKit) use < to denote zero page addressing.

Also, some assemblers (although not any for 6502 that I know of) support labels like 1H; then you can write 1B to reference the nearest 1H backward, or 1F to reference the nearest 1H forward. This is supported in at least MIXAL, MMIXAL, and Glasm. (Maybe it is worth implementing this in some assembler for 6502, too)
[url=gopher://zzo38computer.org/].[/url]

User avatar
Banshaku
Posts: 2393
Joined: Tue Jun 24, 2008 8:38 pm
Location: Japan
Contact:

Re: About 6502 common syntax standards

Post by Banshaku » Sun Aug 16, 2020 6:05 pm

A lot of interesting information here, I will keep note of it.

I started to change the way I write code a little bit like avoiding using "scope" or ".proc" (which is just a scope anyway), write only labels on column 1 and all else on another column. I still use cheap local labels to some degree.

Now I see that there is no "right" way to write things and will understand you cannot port "as-is" code but at the least is should be simpler if the need arise. Not sure if it's worth changing the local labels to "." since it seems more common but I wouldn't be surprised that it just make ca65 go strange with other command that start with "." so I will just give up on that.

The enum, I'm not sure what to think of it. A lot of asm don't use them so it seems specific to ca65 a few other ones (and other asm use it differently). Maybe just using symbols would be easier (this is how I converted enum in vasm).

turboxray
Posts: 104
Joined: Thu Oct 31, 2019 12:56 am

Re: About 6502 common syntax standards

Post by turboxray » Mon Aug 17, 2020 10:08 am

Technically, NESASM 3.24 has a few updates. It's really just updates for PCEAS, but NESASM gets built with the same core.

User avatar
za909
Posts: 211
Joined: Fri Jan 24, 2014 9:05 am
Location: Hungary

Re: About 6502 common syntax standards

Post by za909 » Mon Aug 17, 2020 2:18 pm

If I may, I want to use this opportunity to complain about PCEAS non-standard syntax. I was bothered so much by its use of brackets for [indirect] addressing, that I started working on a PCE-specific fork of ASM6 just so that I could stick to what I'm used to. I should get back to that at some point.

turboxray
Posts: 104
Joined: Thu Oct 31, 2019 12:56 am

Re: About 6502 common syntax standards

Post by turboxray » Mon Aug 17, 2020 4:29 pm

za909 wrote:
Mon Aug 17, 2020 2:18 pm
If I may, I want to use this opportunity to complain about PCEAS non-standard syntax. I was bothered so much by its use of brackets for [indirect] addressing, that I started working on a PCE-specific fork of ASM6 just so that I could stick to what I'm used to. I should get back to that at some point.
That's a bit extreme over some square brackets. Just modify the source of PCEAS and swap it ( [] for () and () for [] ). Add a switch for the swap. I would never code in ASM6 for PCE. PCEAS is so much more powerful than ASM6. Also, why are you trying to write PC-Engine code in ASM6 in the first place???

Pokun
Posts: 1511
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: About 6502 common syntax standards

Post by Pokun » Thu Aug 20, 2020 7:38 am

He is adding HuC6280 functionality to ASM6. I like ASM6 a lot and think it's a good idea to expand it for other architectures, although I don't have any problems with PCEAS. PCEAS/NESASM is indeed more powerful than ASM6 and with the modern updates it's much more useful and less annoying than it used to be (the forced 8 kB banks are gone among other things).

BTW, the "PCEAS.EXE" file can just be renamed to "NESASM.EXE" for it to run in NES mode.

tepples wrote:
Sun Aug 16, 2020 11:12 am
  • ASM6
    ......
    The manual doesn't mention whether indirection uses () or []. (Source: ASM6 README)
The readme for ASM6 is a bit minimal and seems to miss a few things. I'm pretty sure it only recognizes () for indirection.

Post Reply