Ca65 syntax guide for 6502?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
nesrocks
Posts: 468
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Ca65 syntax guide for 6502?

Post by nesrocks » Tue Sep 01, 2020 7:38 am

I have always been somewhat lost as to the syntax used by ca65. I find the documentation hard to follow. For example:

"4.5 Number format
For literal values, the assembler accepts the widely used number formats: A preceding '$' or a trailing 'h' denotes a hex value, a preceding '%' denotes a binary value, and a bare number is interpreted as a decimal. There are currently no octal values and no floats."

From my tests "lda 1" is the same as "lda $1" and not "lda #$1". It loads the value in zeropage byte 1, not the decimal value 1, as the documentation has me believe.

Obviously, after more testing I figured that a preceeding # will tell it to load the literal value instead of an address. But that was a bit confusing as it isn't mentioned in that section of the docs.

Is there a more practical guide for ca65 syntax?
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!

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

Re: Ca65 syntax guide for 6502?

Post by tokumaru » Tue Sep 01, 2020 8:13 am

nesrocks wrote:
Tue Sep 01, 2020 7:38 am
Obviously, after more testing I figured that a preceeding # will tell it to load the literal value instead of an address. But that was a bit confusing as it isn't mentioned in that section of the docs.
The use of "#" to represent immediate values is a general 6502 convention, not an aspect of ca65 specifically, so that's why it's not mentioned in the docs.

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

Re: Ca65 syntax guide for 6502?

Post by turboxray » Tue Sep 01, 2020 8:14 am

Literal values does not mean immediate values. It's telling you the format of using direct numbers vs something like labels - not the addressing mode.

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

Re: Ca65 syntax guide for 6502?

Post by Oziphantom » Tue Sep 01, 2020 8:16 am

that is not CA65 that is 6502

LDA Value
LDA #Value

the first means value is an address and the second means it is a immediate number

same with
ADC #5 ; # is immediate address mode
ADC 5 ; ZP address mode
ADC 500 ; Abs address mode

see any list of 6502 opcode for the list, although the best set is the MOS 6502 datasheet are reproduced in the various Commodore Programmer Reference Guides. PET/Vic-20/C64/+4/C128


what CA65 is telling you is

5 = $5 = 5h = %1001
160 = $a0 = a0h = %10100000

so you can enter the numbers however you want

lda %1001 ; load for memory address 5
sta 2004h ; store to address 8196
etc

User avatar
Controllerhead
Posts: 148
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Ca65 syntax guide for 6502?

Post by Controllerhead » Tue Sep 01, 2020 8:59 am

nesrocks wrote:
Tue Sep 01, 2020 7:38 am
Is there a more practical guide for ca65 syntax?
I use this reference page a ton:
https://www.masswerk.at/6502/6502_instruction_set.html

It has all the addressing modes / cycle counts / flag updates / byte values for each instruction.

There are plenty of gotcha's with the 6502, for instance, STA / X / Y / TXS will not set the N and Z flags. BIT has no immediate mode. You can use ASL / LSR / ROL / ROR bit shifts directly on memory. Plenty more!
Image

User avatar
nesrocks
Posts: 468
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: Ca65 syntax guide for 6502?

Post by nesrocks » Tue Sep 01, 2020 9:01 am

Thanks guys. I have just found this page on the wiki. https://wiki.nesdev.com/w/index.php/Programming_guide
It seems to have basic info I should read.

I'm writing a quick asm guide (for myself) for the concepts, practices and truths that I haven't fully embraced yet as an exercise. It seems to help a lot.

edit: dougeff has also written about this https://nesdoug.com/2016/03/10/26-asm-basics/
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!

Post Reply