What is the practical use of indexed indirect addressing?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 569
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: What is the practical use of indexed indirect addressing

Post by Jarhmander »

In fact, JMP (indirect) already existed in the stock 6502, what changed in latter models is the fact that the fetch of the indirect address is not buggy and doesn't wrap in a page...
((λ (x) (x x)) (λ (x) (x x)))
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: What is the practical use of indexed indirect addressing

Post by psycopathicteen »

I'm guessing it's because the inventors of the 6502 didn't predict what opcodes were going to be used the most often. They couldn't have predicted their chips being used in one of the most popular video game systems in history.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What is the practical use of indexed indirect addressing

Post by tepples »

psycopathicteen wrote:I'm guessing it's because the inventors of the 6502 didn't predict what opcodes were going to be used the most often. They couldn't have predicted their chips being used in one of the most popular video game systems in history.
Would that be the Atari 2600 or the Commodore 64? (Kapp'n)
User avatar
Gilbert
Posts: 564
Joined: Sun Dec 12, 2010 10:27 pm
Location: Hong Kong
Contact:

Re: What is the practical use of indexed indirect addressing

Post by Gilbert »

Apple ][, more likely.
Karateka on NES/Famicom sucksksksk, so Apple ][ >>> Famicom >>>> NES. :roll:
User avatar
slembcke
Posts: 172
Joined: Fri Nov 24, 2017 2:40 pm
Location: Minnesota

Re: What is the practical use of indexed indirect addressing

Post by slembcke »

WHAT?! Have you forgotten the Atari Lynx and Gamate?!

In all seriousness though, this thread is sort of fascinating. The obvious use is for something like an array of pointers, but some of the suggestions here are pretty brilliant. (ex: A table of streams since you can cheaply increment their pointers, or lining up the indexes with other things you want to access)
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: What is the practical use of indexed indirect addressing

Post by koitsu »

FrankenGraphics wrote:Please do! This stuff is interesting, especially as my book is quite sparse on practical examples.
I will try to type these up this weekend. They're long -- usually 1-3 pages. They're commented, but if I was to provide "just a snippet" it wouldn't provide enough useful educational context.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: What is the practical use of indexed indirect addressing

Post by Oziphantom »

psycopathicteen wrote:I'm guessing it's because the inventors of the 6502 didn't predict what opcodes were going to be used the most often. They couldn't have predicted their chips being used in one of the most popular video game systems in history.
Chuck wanted it to be $5. So making it more expensive and balanced, well you end up with the 68K ;) So it wasn't meant to be "a really nice thing to program for", it was meant to be "$5". But he did design it to be used in EVERYTHING. Cash registers, pinball machines, medical equipment etc But it was price that mattered not ease of use.
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: What is the practical use of indexed indirect addressing

Post by FrankenGraphics »

I find it kind of amusing that cash registers, stage light controllers etc to this day often follows obvious programmer logic, as opposed to user logic.

On a cash register, to enter a specific mode of operations, you enter a one-digit variable, then hit "key mode".
A user would in a high degree of probability intuitively hit key mode, then enter the single digit, if not knowing this from the manual or from someone instructung them first day at work, because that follows a linguistic intuition: "set key mode to #".

Want to remote-set the light intensity manually of, say PARcan #53 over the stage, using a digital controller? That's typically
054 CALL
0-255 SET

Whereas on many digital musical synthesizers from the 80s i've used, if you want to carry out a certain action or jump to a specific page of settings, you'd
CALL nnn
or
PAGE nnn

not always though.
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: What is the practical use of indexed indirect addressing

Post by gauauu »

FrankenGraphics wrote: 054 CALL
0-255 SET
The advantage of that is that it easily distinguishes between commands with different digit lengths without requiring another keystroke.

SET-25 ... The system doesn't know if you are done typing the number, or whether you meant 255. So it would require a final "enter" keypress. Putting the "SET" command at the end saves that keystroke.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What is the practical use of indexed indirect addressing

Post by tepples »

FrankenGraphics wrote:I find it kind of amusing that cash registers, stage light controllers etc to this day often follows obvious programmer logic, as opposed to user logic.

On a cash register, to enter a specific mode of operations, you enter a one-digit variable, then hit "key mode".
Reminds me of the "monitor" (debugger) of the Apple II ROM. (Who remembers 3D0G)?
FrankenGraphics wrote:A user would in a high degree of probability intuitively hit key mode, then enter the single digit, if not knowing this from the manual or from someone instructung them first day at work, because that follows a linguistic intuition: "set key mode to #".
That'd depend on whether your native language puts the verb at the end of the sentence. In languages that do, "three switch_mode" sounds more "natural" than "switch_mode three". Consider the programming language var'aq, which uses reverse Polish notation (RPN) syntax in order to match the Klingon language's object-verb order. It'd be even more complicated to provide a word order switch in order for speakers of different languages to use a device.

Aside: In his dissertation Representation, information theory and basic word order (PDF, 200+ pages), Luke Maurits presents experimental evidence that the basic word order matching the order of recall of aspects of an event is subject-object-verb, as in Japanese and Hindi. We see shift over time to other word orders in other languages because verb before object is better at spreading information throughout a sentence.
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: What is the practical use of indexed indirect addressing

Post by FrankenGraphics »

good points, both. That's true. And when you're supposed to work with an industrial or commercial console, you're likely to work with it or something similar again over long periods, so training new staff in subject-verb operations is a negligible effort in the grand scheme of things.

The trouble with the cash register case specifically is that there's nothing to differentiate monetary operations from administrative ones. Let's say the clerk was planning on doing a non-fiscal checkup (typically under keymode 2) or program a new commodity group (let's say that's under keymode 4), then got interrupted by some task - for example a customer comes along. that 2 or 4 remains in the soft accumulator and unless the clerk makes a habit of pressing Cl before starting to register goods, it can get hairy. This is only valid for manual price input though. A scanner device will always overwrite the accumulator.
If keymodes were operated so that you press the mode key first, then a single digit, it would be watertight. I don't think i've ever seen a cash register with more modes than can fit a single numerical digit. It is also a better analogy to cash registers with actual turnkeys for operating modes - you grip the key, then turn it to the desired digit/mode. edit: actually the best analogy would be to hold the keymode button and hit a single digit.

In parable to the apple 2 thing; zenith computers used halt operations and break out into a monitor/BIOS if you pressed ctrl+alt+ins.
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: What is the practical use of indexed indirect addressing

Post by koitsu »

koitsu wrote:I will try to type these up this weekend. They're long -- usually 1-3 pages. They're commented, but if I was to provide "just a snippet" it wouldn't provide enough useful educational context.
As promised, I took a look at these in detail this weekend. The book ("Programming the 6502" by Rodnay Zaks, 1983, 4th edition, ISBN 0-89588-135-7) has an entire chapter on data structures. This includes linked lists, binary trees, sorting algorithms, overall data structure creation/organisation, etc.. They're long and include descriptions as well as flow chart diagrams to help with comprehension. The code is commented, though sparsely (normal for the time, given screen widths).

The code is written for ASM 65 from Rockwell, which was a 6502 assembler written in BASIC for HP 2000F computers. Younger folks unfamiliar with some of the more "arcane" assembler syntaxes of the 80s might not comprehend the syntactical sugar. For example: * refers to the current PC at assemble-time, so something like * = $0 sets the PC to $0000, hence any variables/labels you define there get assembled starting with a base address of $0000 (i.e. zero page) (and, of course, it's still your own responsibility to populate zero page with actual data); .WORD declares an explicit 16-bit value (e.g. .dw $1234 or .db $34, $12 in more common-day assemblers). Furthermore, the code is not intended for the NES, but should work given that RAM in the NES resides from $0000 to $07FF.

I would prefer to scan these pages and put them up somewhere, especially since flow chart diagrams are heavily used, except as is common with most reference books, the content is copyrighted ((C) 1983 SYBEX Inc. 2344 Sixth Street, Berkeley, CA 94710; founded by Dr. Zaks); the code would not OCR well given the font, size, and weight. So what I've typed up below is indeed a copyright violation; I'm hoping SYBEX will allow me a bit of leeway.

Here are the general examples in book, all from Chapter 9, which use indexed indirect ((foo,x)) addressing:

1. A hashing algorithm (8 pages; 2 pages of code; long)
2. Bubble sort (5 pages; 1 page of code; short)
3. A merge algorithm (4 pages; 1 page of code; semi-long)

The short of it: in all 3 cases, indexed indirect addressing is used to reference a 16-bit pointer (or series of pointers) which are used to point to one or more data structures.

I'll include the code from #2 above, since it's the shortest:

Fig. 9-48: Bubble-Sort: Memory Map

Code: Select all

     +-------------+
0000 |             | -----+
     |- TABLE PTR -|      |
0001 |             |      |
     +-------------+      |
     |             |      |
     |             |      |
     |             |      |
     +-------------+      |
0200 |             |      |
     |   PROGRAM   |      |
     |             |      |
     +-------------+      |
     |             |      |
     |             |      |
     |             |      |
     +-------------+      |
     |  NUMBER n   | <----+
     +-------------+
     |  ELEMENT 1  |
     +-------------+
     |  ELEMENT 2  |
     +-------------+
     |             |             Y                       X
     +-------------+      +-------------+         +-------------+
     |             | <----|     PTR     |         | EXCHANGED?  |
     +-------------+      +-------------+         +-------------+
     |             |      CURRENT ELEMENT
     +-------------+
     |             |
     +-------------+
     |  ELEMENT n  |
     +-------------+
What's omitted in the description (which I've also omitted, because it's an entire page) is what the value of $0600 is that TAB contains. This is the actual address in RAM that contains the data structure described (i.e. the actual data to sort). In other words NUMBER n is at $0600, ELEMENT 1 is at $0601, etc.. This data must reside in RAM because the routine actually modifies the RAM area it's reading from. I feel all that's pretty important to know. :-)

Fig. 9-49: Bubble-Sort Program

Code: Select all

* = $0

TAB    .WORD $600

* = $200

SORT   LDX #0           ;SET EXCHANGED TO 0
       LDA (TAB,X)
       TAY              ;NUMBER OF ELEMENTS IS IN Y
LOOP   LDA (TAB),Y      ;READ ELEMENT E(I)
       DEY              ;DECREMENT NUMBER OF ELEMENTS TO READ.
       BEQ FINISH       ;END IF NO MORE ELEMENTS
       CMP (TAB),Y      ;COMPARE TO E'(I)
       BCS LOOP         ;GET NEXT ELEMENT IF E(I)>E'(I)
EXCH   TAX              ;EXCHANGE ELEMENTS
       LDA (TAB),Y
       INY
       STA (TAB),Y
       TXA
       DEY
       STA (TAB),Y
       LDX #1           ;SET EXCHANGED TO 1
       BNE LOOP         ;GET NEXT ELEMENT
FINISH TXA              ;SHIFT 'EXCHANGED' TO A REG. FOR COMPARE...
       BNE SORT         ;IF SOME EXCHANGES MADE, DO ANOTHER PASS.
       RTS
P.S. -- There's actually a checksum routine in this book that looks neat/useful too, but it doesn't use the discussed addressing mode, so it's off-topic.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What is the practical use of indexed indirect addressing

Post by tepples »

koitsu wrote:something like * = $0 sets the PC to $0000, hence any variables/labels you define there get assembled starting with a base address of $0000
For comparison: ca65 has a .feature pc_assignment that makes * = behave like .org.
koitsu wrote:.WORD declares an explicit 16-bit value (e.g. .dw $1234 or .db $34, $12 in more common-day assemblers).
For comparison: .word in ca65. I guess the more things change, the more they don't.

This particular bubble sort routine could have been done just as easily with lda (tab),y, seeing as Y doesn't need to be saved (the next instruction overwrites it anyway), and X=$00 isn't used farther down.
User avatar
Banshaku
Posts: 2417
Joined: Tue Jun 24, 2008 8:38 pm
Location: Japan
Contact:

Re: What is the practical use of indexed indirect addressing

Post by Banshaku »

That's interesting. I received this specific book (confirmed with the ISBN) as a Christmas present when I started nes programming and I read the basics but stopped in the middle of it (well, I stopped everything actually, not just the book).

I guess I should take the time to read it since there is maybe a lot of interesting things that may come handy someday. I remember reading it in the Tokyo subway and it was, well, not very convenient since it is always overcrowded in the morning ^^;;

What are the highlight of the books, the part that should be read no matter what? I still think my knowledge of 6502 is quite low so I don't yet grasp what is a good approach or not (like the Tepples explained one). I guess that comes with experience, nothing you can do about it.
Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: What is the practical use of indexed indirect addressing

Post by Oziphantom »

koitsu wrote:I would prefer to scan these pages and put them up somewhere, especially since flow chart diagrams are heavily used, except as is common with most reference books, the content is copyrighted ((C) 1983 SYBEX Inc. 2344 Sixth Street, Berkeley, CA 94710; founded by Dr. Zaks); the code would not OCR well given the font, size, and weight. So what I've typed up below is indeed a copyright violation; I'm hoping SYBEX will allow me a bit of leeway.
http://www.bombjack.org/commodore/books-generic.htm a fair way down the page.

Although generally Machine Language on the Commodore 64, 128 and other Commodore Computers Revised and Expanded Edition by Jim Butterfield is seen as "the" 6502 book to learn from ;) found here http://www.bombjack.org/commodore/books.htm
Post Reply