What does lda #>oam do?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
What does lda #>oam do?
by on (#225121)
So I was looking through some code that was posted on this forum. I can't remember who's code it was (sorry about that), but there was list accumulator load:

Code:
lda #>oam


What does the #> mean. I've been googling around for it, but I just can't seem to find it anywhere.

Thanks for your help,
Rick
Re: What does lda #>oam do?
by on (#225124)
It depends on the assembler, so you should always refer to your assembler documentation.

>thing usually means "get the high byte of thing". Likewise, <thing means "get the low byte of thing". The # obviously means immediate addressing, thus the accumulator will end up holding the high (or low) byte of wherever thing is located. The high and low bytes are calculated at assemble-time, not run-time. Better demonstrated with pseudocode, I suppose:

Assume oam is a variable that resides somewhere -- say in RAM, at address $0520. Thus:

Code:
  lda #<oam   ; equivalent to lda #$20
  lda #>oam   ; equivalent to lda #$05

Likewise, if you were to not use immediate addressing, and had this instead...
Code:
  lda <oam    ; equivalent to lda $20
  lda >oam    ; equivalent to lda $05

Make sense?
Re: What does lda #>oam do?
by on (#225126)
Ok, that makes sense. I've been looking through the documentation online, and just haven't been able to find it listed. I'm using ca65 btw.

Thanks for your help!
Re: What does lda #>oam do?
by on (#225128)
The documentation for this feature in ca65 is here, in two places (sort of):

Expressions: https://www.cc65.org/doc/ca65-5.html#ss5.1
Pseudo functions: https://www.cc65.org/doc/ca65-10.html

With regards to the latter, in ca65 you can alternately say this, which may make more sense to you:

Code:
  lda #.hibyte(oam)   ; same as lda #>oam
  lda #.lobyte(oam)   ; same as lda #<oam

Note that it's lobyte, not lowbyte (i.e. no "w" in "low").

The "shorthand" syntax is what a lot of assemblers have used for the past ~40 years.
Re: What does lda #>oam do?
by on (#225129)
If the next line is sta #4014 (or sta OAMDMA) you might also need to know that oam has to be aligned to a single page (i.e. <oam =$00) to work properly with the DMA hardware accessed by writing the high byte of its address to $4014.
Re: What does lda #>oam do?
by on (#225147)
Most people use $0200-$02FF as their OAM buffer, but you can use any page of RAM (or even ROM!) for that purpose. Naming that array, as you would any other variable, allows you to access the OAM buffer by name, instead of by address, and lda #>oam will get the page number for the buffer, which can be used to start an OAM DMA.

The advantage of doing it that way is that if you ever decide to move the OAM buffer to another page, you just need to change the variable declaration, because the sprite routines and the DMA code are not using hardcoded addresses.