This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

A question about "#>" and "#<" in 6502

A question about "#>" and "#<" in 6502
by on (#201248)
On the APU basics page of the NESDev wiki, there is some code that "plays a 400 Hz square wave (50% duty) at maximum volume: "

jsr init_apu

lda #<279
sta $4002

lda #>279
sta $4003

lda #%10111111
sta $4000

I know that #% is how you get the literal value for a binary number, and #$ is how you do that for hexadecimal numbers, but I have seen decimal literals represented as e.g. "lda #255". So, my question is, what are the "#>" and "#<" doing here? What are these operators?

Maybe it is an assembler-specific thing, so I am using ca65 if that matters.
Re: A question about "#>" and "#<" in 6502
by on (#201254)

> and < are special unary operators that take some 8-bit subset of their argument

In this example, #<279 is equivalent to #(279 & 255), and is equal to #$17

(This behavior is assembler specific, and most-but-not-all 6502 assemblers use this syntax)
Re: A question about "#>" and "#<" in 6502
by on (#201255)
# modifies an instruction to take a literal value rather than fetch a value from an address. The characters that follow it don't change this.

% means the number that follows is binary. It doesn't matter if it's supposed to be a literal value or an address, that's determined by the # not the %.

$ is the same just for hexadecimal.

< means to use only the low byte (bits 0-7) of the number, it does not matter if that number was prefixed with % or #. (It is the same as number & 255.)

> means to use only the high byte (bits 8-15) of the number. (It is the same as number >> 8.)

<$1234 = $34
>$1234 = $12
symbol = $1234
<symbol = $34
>symbol = $12

# modifies the instruction.
%,$ modifies how a number is written.
<,> selects the low or high byte of a 16-bit value.
These three things are all independent. You could use all three of them on the same operand, if you wanted. # only works with instructions (and not in a table of bytes, for example).

This is the ca65 meaning, at least. In NESASM < and > only work as instruction modifiers, which is maybe a little strange.
Re: A question about "#>" and "#<" in 6502
by on (#201257)
Be warned that "<" and ">" are not standard notation, and some assemblers may have completely different ways of specifying this. Of note, early versions of the CopyNES BIOS contained a rather severe bug (a few broken instructions in the "MicroBug" feature) as a result of trying to use this syntax in an assembler that didn't support it.
Re: A question about "#>" and "#<" in 6502
by on (#201261)
Thanks so much, everyone. It makes sense now.

Good point about the presence of the binary operator not necessarily indicating a literal value, rainwarrior. I haven't considered the possibility of using a binary number for an address, so that's food for thought. Clears things up!
Re: A question about "#>" and "#<" in 6502
by on (#201410)
Then of course you probably wouldn't normally type an address in binary or decimal. Binary is useful when you see the number as flags or something like that, and wants it to be clear in your source code what flags you set and clear. Hexadecimal is useful for addresses, but I guess many people use it for about everything. Decimal is only useful when you want a number to be more human readable, like the value in a loop counter.

Personally I mostly use hexadecimal for about everything, including loop counters, (although the $ is annoying to type on Swedish keyboards as it requires the AltGr key) except certain flags that I use binary for. I only use decimal for assembly time math like this:
  lda array+7        ;element 7 (which is the eighth element) in the array
  lda #NUMBER*2      ;double the number
  lda #NUMBER/2      ;half of the number
Re: A question about "#>" and "#<" in 6502
by on (#201411)
It's also convenient to type #0 to clear some register, especially if you have $ hidden behind AltGr
Re: A question about "#>" and "#<" in 6502
by on (#201414)
I only use binary (mostly for bit masks) and hex, never decimal. My main reason for avoiding decimal numbers is because they can easily be confused with hex numbers (even though I have them colored differently using syntax highlighting), and this has caused me some annoying bugs in the past. Better avoid this sort of confusion and always use the base that often makes more sense. Binary is easy to spot, so there's no problem in using that.

As for using binary for addresses, it may not make much sense for CPU addresses, but breaking up PPU addresses into bit fields is a very useful thing to do when manipulating the various tables that exist over there (pattern, name and attribute), so I sometimes do find myself loading partial PPU addresses in binary form.
Re: A question about "#>" and "#<" in 6502
by on (#201420)
It always surprises me how adamant some people are using hex for absolutely everything. Frankly, I get quite annoyed when reading code where hex has been used where decimal would have made much more sense (and vice versa). I'm talking about cases like adding 100 to a score counter, but using $64 in the code.

Personally, I try to use the base which makes the code easiest to understand. I can't say that I've run into decimal vs. hex bugs like tokumaru.
Re: A question about "#>" and "#<" in 6502
by on (#201426)
At first I also thought it was very annoying to use hexadecimal for everything when the assembler allowed other bases. But I was taught that it was a convention so I'd thought I better get used to it.
It's still annoying to type that stupid $ character (AltGr+4) all the time, but I've gotten quicker at it, and I think using $64 instead of 100 have given me a better feel for hexadecimal values (I look them up in the Windows calculator just in case though, haha). I usually keep comments that explains things in decimal if necessary.

Another reason I stick mostly to hexadecimal is simply because Notepad++ refuses to highlight certain combinations of characters. Especially the # plus a number (#$ or #% plus a number works for some reason).
Re: A question about "#>" and "#<" in 6502
by on (#201502)
You can file an issue on the bug tracker for that kind of thing. Scintilla is open source, and so is Notepad++.
Re: A question about "#>" and "#<" in 6502
by on (#201513)
Notepad++ highlights everything properly for me. I even use different colors for different bases.
Re: A question about "#>" and "#<" in 6502
by on (#201680)
How do you set up the numbers in your highlighting file Tokumaru? I put $ % #$ #% in "Prefix 2" and A B C D E F a b c d e f in "Extras 1", any other combination seems to break hexadecimal highlighting. All other number fields are empty.

And how do you make it do different bases with different highlighting? I guess you don't use the Number style for numbers?

Dwedit wrote:
You can file an issue on the bug tracker for that kind of thing. Scintilla is open source, and so is Notepad++.

I don't know what a bug tracker is though. I will have to do some research first.