What is the significance of $ # and % in 6502?
Asked Answered
E

2

6

Platform

  • 6502 emulator
  • DASM assembler
  • Windows 10

I have numbers beginning from

#2

$2F

%0000111

I don't understand why # $ % are used in assembly code of 6502?

And sometimes ldx #$FF

Load the x register with #$FF

Why two symbols are used here?

Emcee answered 22/6, 2021 at 5:0 Comment(0)
J
12

DASM allows numbers to be expressed in binary, octal, decimal, and hexadecimal.

  • Binary numbers use the % prefix (e.g. %1101).
  • Octal numbers use the 0 prefix (e.g. 015).
  • Decimal numbers use no prefix (e.g. 13).
  • Hexadecimal numbers use the $ prefix (e.g. $0D).

The # symbol is used to specify immediate addressing:

LDA 0   ; Load the byte from address 0 in memory into register A
LDA #0  ; Load the value 0 into register A

One can of course combine immediate addressing with a different numeric base, e.g.:

LDA #$FF ; Load the value $FF into register A
Julianjuliana answered 22/6, 2021 at 7:42 Comment(0)
H
5

These symbols are common syntactic sugar used by lots of assemblers on lots of platforms, and are intended to make it easier for humans to provide numeric values to the assembler in bases 2, 10 and 16 (binary, decimal and hexadecimal):

%00001100 means 12 in binary
12 means 12 in decimal
$0C means 12 in hexadecimal

The # symbol has further significance as indicator of addressing in numerous assembly syntaxes including DASM:

LDA #%00001100 loads 12 into the Accumulator
LDA #12 loads 12 into the Accumulator
LDA #$0C loads 12 into the Accumulator
LDA $0C loads the contents of memory location 12 into the Accumulator
Hidrosis answered 22/6, 2021 at 5:42 Comment(7)
"The # and $ symbols have further significance as indicators of absolute and indirect addressing" Did you mean immediate? Indirect would be something like ($0C, X).Julianjuliana
@Julianjuliana Doh! Yes, I did. Corrected.Hidrosis
# doesn't indicate decimal, # indicates immediate (instead of memory address). e.g. lda 12 would load from memory, at address 12, wouldn't it? So it might be better to explain it as decimal being the default for numeric literals, with $ overriding to hex (just like 0x in C) and % overriding to base 2 (like 0b in C). Oh, I see Michael already posted an answer explaining it that way.Dixil
@PeterCordes I have edited the answer regarding decimal vs immediate, is this better?Bosnia
@youri: That fixes the first part. The answer unfortunately still says # and $ ... indicators of addressing. $ has nothing to do with addressing in DASM, it's equivalent to C's 0x. Some other assemblers do use $ differently, for example AT&T syntax for x86 uses $ to indicate immediate instead of memory (exactly like DASM uses #). But that's an unhelpful way to explain how DASM works. The example is good, but the text says $ indicates addressing.Dixil
@PeterCordes I ventured a second correction. Beyond that I let the OP bring further correction if required.Bosnia
@youri: Yeah, this is fine. This isn't the highest-voted or accepted answer, so it's less important to make it as good. And the only thing left that sounds a little odd to me is the suggestion that various assemblers use # for addressing. ARM syntax uses # on immediates, but bare numbers don't have a different meaning without it. Some others use it as the comment character. Maybe there are other 8-bit micros that use it similarly to 6502 syntax? I'm not familiar with their syntax, but based on the user-name, Eight-Bit Guru might be thinking of those, not modern ISAs.Dixil

© 2022 - 2024 — McMap. All rights reserved.