## Is this all the Address Math?

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

Moderator: Moderators

gukingofheart
Posts: 44
Joined: Tue Dec 04, 2018 2:28 pm

### Is this all the Address Math?

Not sure what to call this, but learned something new today. That for certain codes/hex or whatever, you can write an address based on math.

Code: Select all

``````Zero-Page Indexed

This works just like absolute indexed, but the target address is limited to the first 0xFF bytes.

The target address will wrap around and will always be in the zero page. If the instruction is LDA \$C0,X, and X is \$60, then the target address will be \$20. \$C0+\$60 = \$120, but the carry is discarded in the calculation of the target address.

Indexed Indirect

This mode is only used with the X register. Consider a situation where the instruction is LDA (\$20,X), X contains \$04, and memory at \$24 contains 0024: 74 20, First, X is added to \$20 to get \$24. The target address will be fetched from \$24 resulting in a target address of \$2074. Register A will be loaded with the contents of memory at \$2074.

If X + the immediate byte will wrap around to a zero-page address. So you could code that like targetAddress = (X + opcode[1]) & 0xFF .

Indexed Indirect instructions are 2 bytes - the second byte is the zero-page address - \$20 in the example. Obviously the fetched address has to be stored in the zero page.

Indirect Indexed

This mode is only used with the Y register. It differs in the order that Y is applied to the indirectly fetched address. An example instruction that uses indirect index addressing is LDA (\$86),Y . To calculate the target address, the CPU will first fetch the address stored at zero page location \$86. That address will be added to register Y to get the final target address. For LDA (\$86),Y, if the address stored at \$86 is \$4028 (memory is 0086: 28 40, remember little endian) and register Y contains \$10, then the final target address would be \$4038. Register A will be loaded with the contents of memory at \$4038.

Indirect Indexed instructions are 2 bytes - the second byte is the zero-page address - \$20 in the example. (So the fetched address has to be stored in the zero page.)

While indexed indirect addressing will only generate a zero-page address, this mode's target address is not wrapped - it can be anywhere in the 16-bit address space.
``````
Besides Zero and Indirect, are there any others? Does Immediate or Absolute do this to at all?
This might explain why I couldn't solve the ExciteBike Turbo GG connection.. because I was trying to find "BC C0", but could of easily been another address that connects the two.

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

### Re: Is this all the Address Math?

The 6502's six indexed addressing modes are in fact one of its big practical advantages over the 8080.

Code: Select all

``````  lda aaaa,x  ; Absolute indexed with X
lda aaaa,y  ; Absolute indexed with Y
lda dd,x    ; Direct page* indexed with X
ldx dd,y    ; Direct page indexed with Y (LDX and STX only)
lda (dd,x)  ; Indexed indirect (rarely used outside audio drivers)
lda (dd),y  ; Indirect indexed (very common)
``````
The other modes (immediate, absolute, relative) do not involve addition.

* "Direct page" is the more general term. It is also called "zero page" on 6502 and 65C02 because it is always at page \$0000-\$00FF. On other 6502-like processors, such as 65816, HuC6280, and SPC700, it is elsewhere or relocatable.