2801
6502 Indexed Addressing,
Including Memory Indirect
| ALUs, Register Indirect Review, Role Playing the 6800 |
TOC |
"Okay, Harvey, you said the 6502 has two index registers, but they are only eight bits wide, right?"
"That's what my notes say."
"And, Pete, you mentioned a 16-bit index base constant part for the instruction."
"I thought it might be a possibility."
"What benefit would it have?"
"When you know an array's address, it could start anyplace in memory, as opposed to the 6800's 8-bit constant part. But the index register would still limit the array's width."
Mr. Mori put a transparency on the projector.
"Something like this?"
Mr. Mori stopped and looked at the diagram. "Oh, wait. The 6502 assembler syntax uses parentheses to mean indirection, and this is going to be confusing. Let me fix this."
He erased the punctuation in question:
Pete said, "So I guessed right?"
"I guess you did. What else is different about it, different from the 6800 and 9900?"
"Little endian," Harvey said.
"Yep. The 6502 puts its low byte first, like the 8080 and the 1802."
Bill asked, "Are X and Y interchangeable?"
"Sort of. There are some differences in which instructions and addressing mode can be used with which. We'll talk about it in a minute or so, but Y indexing is not supported for some single-operand instructions. File that fact away in your brain somewhere, and we'll come back to it."
Ted raised his hand. "Single-operand instructions means read-modify-write?"
"You're trying to confuse everyone."
"Sorry."
Mr. Mori grinned. "Just kidding. You're right and we'll talk about that later, too. Probably not today." He paused, then asked, "Can anyone think of a way of getting around the width limitation?"
I suggested, "Use the middle of the array for the constant base address?"
Mr. Mori nodded. "Can you tell us how to do that?"
"Uhn --"
While I was thinking, Frank raised his hand.
"Frank?"
"If the array is larger than 256 bytes but smaller than 513, provide one routine to point into the low half, and provide another to point into the high half. Then you can figure out somehow which half you're needing to point to, to choose which routine to use."
"Very good, and we'll talk about how to actually do that later, as well, because knowing how can be useful. Not today, I think. And there's another way that involves modifying the constant base in the actual op-code at run-time, but I do not want to teach you that."
"Why not?" Karl asked.
Cyndy said, "You might accidentally modify the wrong part of the instruction?"
"That's part of it," Mr. Mori agreed. "What else?"
Frank said, "You might modify the constant part of the instruction once, and something might interrupt the code, modify it again, and then somehow fail to restore it before the original code finishes accessing the array, which would really confuse the code."
"Excellent. And the guys who designed the 6502 knew that could happen, so they gave us another way that doesn't require separate routines for each 256 bytes of the array, but I want to talk about that after I talk about something else."
He picked up another transparency. "I need to erase the parentheses on this one, too." He did so and replaced the one on the projector. "Just to be complete, what does our 16-bit constant part and 8-bit variable part mean for arrays when you don't know where an array begins until the program runs?"
"If the base address is in the index register and the 16-bit part in the instruction is the offset, that's pretty much useless," Karl complained."Why so?"
"How often are you going to have an offset larger than 256 when the array starts in page zero?"
"Good point. The architects of the 6502 knew this as well, probably from programming the 6800, and they provided a short indexed mode." Mr. Mori picked up another transparency, fixed it, and swapped it in.
"So you can put the base address in the index register and only use one byte for the offset. They were saving space in the op-code table, so the Y index register can't do this except for two instructions, load X and store X. And I'm not sure whether the guys who designed the 6502 did this for some reason or it just worked out that way, but this address mode wraps around in page zero, so you'll need to remember that, too."
I asked, "So the entire array has to fit in page zero?"
"If it doesn't, you'll have to use the 16-bit offset form for the parts that go beyond, which can get really awkward, or just use the 16-bit offset form for the whole array."
Bill said, "I think I can see that."
"Of course, we can also do 8-bit constant base with 8-bit variable index with this addressing mode, as we have discussed before. Gotta fix this one, too." He did so, and swapped another transparency in.
Karl said, "And this still limits the useful size of arrays."
(Surprisingly, there are a lot of useful applications for microcontrollers that don't actually need any more than 256 bytes for any particular array, or even for all arrays. In fact, one of Motorola's corporate customers pointed this out to Motorola sales engineers, probably about the time I and my real-life classmates were beginning the Industrial Electronics class in the real world. And Motorola would respond by designing the 6805, which would be appropriate to describe as a half of a 6801 -- almost literally. Or maybe half a 68HC11. Only the accumulator A, only an 8-bit X. Some bit handling instructions not in the 6800 or 6801. But the 6805 and its descendants would become one of Motorola's biggest cash cows in your reality. Some analysts would estimate that descendants of the 6805 outnumbered all other microprocessors and microcontrollers delivered in the early 1990s, before ARM ..., but I'm getting way ahead of the story again.)
"If we had some way to work on all 16 bits of an address in memory and then use the result as an address, it would help, right?"
Karl grunted agreement. "I don't see any way to handle generalized arrays with just what you've described, except by what you said, multiple access functions or run-time self-modifying code. Is there a 16-bit index register hidden away somewhere?"
"That brings us to two more addressing modes in the 6502 which are called memory indirect addressing modes."
Bill said, "Indirecting from memory would be like indirecting through the 9900's working registers?"
"Sort-of kind-of pretty much like, the way I see it."
Harvey added, "So X and Y are only eight bit, but the 6502 can use 16-bit addresses directly out of memory?"
"Directly indirectly." Mr. Mori grinned at his own joke. None of us students really got why it was amusing, just yet.
"Let's look at one of these addressing modes." Mr Mori swapped transparencies. "The first one is a kind of extension of the short indexed mode, but what you are indexing in the direct page is an array of addresses that the 6502 can load into its address buffer. You might use it something like this:"
"Note that we are now scanning the list of addresses, not the string, with the index register. Maybe I should have shown the address buffer in this, but that might not be exactly accurate. Hmm. Let's give it a shot."
Karl stood up and said, "So there is a 16-bit address register hidden in there!"
Mr. Mori reworked part of the diagram while we watched. "Yeah, sort-of. Whether it's an actual 16-bit register or not, I don't know. But it's as if there is one there."
Karl sat down as Frank said, "So you with this we can look at the length byte, but how would you scan the string?"
I suggested, "You could have a 16-bit variable that's an array of one, that you can copy the address to, and do address math on that like we did on the 6800, and then use this indirect mode on the array of one?"
"Might work, but what happens to X while you're doing that?" Mr. Mori asked.
Ted said, "I think it gets walked on."
I asked, "Can you use this mode with Y so you can keep X pointing to the array?"
Mr. Mori said, "You guys are thinking. Great. Y does have a little bit different use. Let's look at it." He swapped transparencies.
"Oh, let me put that address buffer in here."
He started reworking one part, then stopped again for a moment.
"Except there must be two virtual 16-bit buffers, according to the timing charts. Or they figured out a way to reuse it pretty efficiently. Hmm."
He reworked another part, as well.
"I'm not sure, as you may have heard me just telling myself, whether the 6502 adds Y to the same address buffer it used to bring the address out of P, or whether there are two separate buffers. No guarantees on the actual accuracy of this diagram. Either way, hopefully, you can see something of how it works in this diagram."I said, "So P in this is like Rick's and my X16 variable --"
Rick said, "That was your X16 variable, but, I think you're right. It looks like we can do math on that in memory, and then it can be used directly as a base address with Y as the index. Which overcomes the width and location problems like in the 6800, but without having to load and store the 16-bit X."
Bill said, "So, we could load an address from a table of string addresses indirecting from X, then move that address to P and use P to scan the string."
Harvey said, "I think you'd use the X indirection to do something like checking the length of these strings, and if the length is in the right range, use regular indexed instructions to load the pointer to P, where you could use the Y indirection to scan the strings."
Mr. Mori said, "Harvey, do you want to try writing some code to do that?"
"Uh, not really."
Mr. Mori picked up the marker from the whiteboard tray. "How about if Bill helps?"
Bill and Harvey looked at each other doubtfully, but Bill stood, and then Harvey stood, and they went to the whiteboard. Harvey picked up the marker.
Harvey said, "Do we put the address of the array of string pointers in X?"
Bill said, "Let's use the address for the constant part, like the example shows."
Chuck asked, "Could it go either way?"
Mr. Mori said, "Let's wait to think about that."
Harvey wrote
LDX #4 ; 3rd string is offset 4
Bill said, "We need to get the length to look at it."
Harvey hesitated, and then wrote
LDA (N,X) ; N array is in page 0, get length of string
"Then what?" Bill asked.
Hec said, "We don't really know how this part works."
Mr. Mori said, "Just write something."
Harvey wrote
(something)
Everyone laughed, but Mr. Mori said, "Good."
Bill said, "Assuming we can proceed, we need to load the address into P.
Harvey said, "And we do that byte-by-byte." He wrote,
LDA N,X ; low byte of address
STA P ; P is in page 0
LDA N+1,X ; high byte of address
STA P+1
Karl said, "Is that legal? I can see P plus one, but N+1 --" and he trailed off.
Chuck said, "Bumping the constant for the base by one probably has the same effect as bumping the variable X by one, doesn't it?"
Mr. Mori said, "It does."
"Okay," Bill said. "We need the offset into the string."
"Put it in Y?" Harvey suggested, then followed his own suggestion.
LDY #3 ; 3rd char after length
Bill said, "I guess we're ready to do whatever it was we were going to do."
Harvey said, "That was op B, wasn't it?" and wrote,
opb (P),Y
"Did I get the parenthesis right, Mr. Mori?"
"That's the way I understand the syntax to work. Parentheses indicate indirection." Mr. Mori replied.
Mark said, "So the 6502 is carrying the concept of page zero as a bank of pseudo-registers a bit farther than the 6800."
"Thank you for pointing that out." replied Mr. Mori.
I said, "But we still aren't really scanning any strings. We need some way to repeat things."
"True. Anyway, once you understand how these two modes work here, I think you'll be able to think of more ways to use them."
Karl said, "I don't really see how modifying P is significantly different from modifying the address field of an instruction. If you modify P and then something interrupts and modifies it again before you can use it, you're still going to be using a wrong base address."
Mr Mori said, "Good point. One difference is, as Cyndy pointed out, that you are less likely to overwrite actual instruction object code with P. Another is that you can collect P with other variables that need to be swapped out and restored when an interrupt happens. It makes it easier to manage what needs to be managed."
Karl set his mouth in thought.
Mr. Mori let us think some more before continuing. "Now there's one more mode, and I want to mention it. It's a mode only for jumps, that they call absolute indirect in the docs. An absolute jump moves program flow to the 16-bit address that the instruction specifies. An absolute indirect jump moves program flow to the 16-bit address that is stored at the address specified in the instruction."
Barry said, "So, the absolute jump is like load PC immediate, and the absolute indirect jump is like load PC absolute?"
Scott and Bob groaned, but Mr. Mori said, "That's another way of looking at it, but I'm afraid some of our students will be confused.
Mark stood up and went to the whiteboard.
Mr. Mori said, "Looks great. There is a bug in this instruction, however. When the instruction specifies an address with a low byte of 255, it grabs the low byte of the new PC from the proper place, but then wraps the address around to get the high byte instead of incrementing."
"So we should never give it an address that ends in hexadecimal $FF, then," Mark said. He wrote in a note next to address 2101, "Don't use JMP (0XXFFH). No 255 in low byte.". And he put the marker back and sat down.
"Correct."
Mr. Mori shut the overhead projector off, leaving it in place to cool.
"We've been burning the brain oil, so let's shift gears and look at your letters of inquiry." He moved an opaque projector in beside the transparency overhead projector.
"About your letters of inquiry, Ms. Wilson informs me that she will not allow Cyndy and Joe to type anybody's letter of inquiry for them in the school's typing lab. I must say that she has convinced me of the folly of the plan."
There were a few scattered groans and complaints, along with some "Of course." and such.
"On the other hand, she did have an another plan, and I think it's a better plan. She thinks she might be willing to hold the computer lab open after school for you guys to go over in small groups, and she can coach you and then let you type them up yourselves. But she needs to check with administration first."
Responses shifted a bit more to the positive.
"I think most of you realize that typing is a useful skill when you're working with computers. So if she gets approval, this could be a good opportunity. But you must all understand that, if you do this, you must treat the typewriter lab equipment with respect, no less respect than our electronics lab."
Mark said, "No playing football with the typewriters, Karl."
There were scattered chuckles.
"Hey!" Karl objected. "I know how to throw a typewriter respectfully."
More chuckles.
Mr. Mori gave us an indulgent frown. "Getting yourselves kicked out of school would leave us short of students again. Don't do that."
There was scattered laughter.
Rick and Hec shook their heads.
Cyndy's eyes grew wide with concern.
I gave it all an eyeroll.
"Guys," Cyndy exclaimed, "Think of what Ms. Williams would say if she heard this. She'd probably change her mind."
The jesting suddenly stopped.
Karl coughed. "Sorry, Cyn. Just kidding."
"'Cyndy', to you."
Karl ducked his head in apology.
"The problem with jokes is when they suddenly quit being jokes," Mr. Mori's voice was sober. "There are parts on those electric typewriters that I'm sure some of you will be tempted to get your fingers into or treat like balls. I don't want to have to be this way, but I will have to have promises from everyone who goes to behave yourselves. I may even need a permission slip from your parents."
Mark said, "I guess that was partly my fault, too, leading off like that. Sorry, Karl. Sorry, guys."
There were general murmurs of agreement, and Mr. Mori let them die down.
"So, Joe," he shifted gears. "Do you have your letter with you?" he asked as he turned the opaque projector on.
Cyndy gave me a querying look, and responded before I could. "I have mine typed up." She slipped it out of her notebook and handed it to Mr. Mori while I got my copy from the typing class out.
He looked puzzled, and accepted Cyndy's, but turned to me. I handed him the letter I had typed at school.
Rick glanced over at me. "You kept a copy, then. Smart move."
Mr. Mori looked them both over, then said, "It looks like you both did a bit more editing before typing these up yesterday."
"Ms. Wilson made some suggestions," Cyndy explained before I could.
"Nice of her."
Cyndy was still querying me with her eyes.
"We've been asked to be careful about showing students' actual addresses and phone numbers in class, do you mind if I mark out your address and phone number and show this to the class?"
"No problem, either way," I replied. "But, for the record, that's not what I actually sent."
"Sent?" Mr. Mori gave me a quick look.
"I guess I got impatient last night."
He blinked at me. "You have a typewriter at home?"
"Dad does, but I sent it handwritten. I was careful to make it legible."
Mr. Mori looked non-plussed. "Administration wants copies of every letter sent. I don't guess you made a copy of it?"
"Scratch copy in pencil, with mark-up," I said as I turned to dig into my notebook for the scratchwork, but Cyndy was holding a piece of paper up for me.
It was my turn to to give her a querying look.
"You might want this," she said, quietly, with a knowing smile.
I blinked and accepted it.
It was my own writing, the letter I thought I had sent the night before.
"What? How on earth?"
Cyndy smirked and kept her voice low enough only Hec and Rick and I could hear. "She said you didn't notice."
"She --" I kept my voice low as well.
Mr. Mori watched our exchange with interest.
"Writes fast. And accurate."
I gave an embarrassed laugh and covered my eyes with the letter for a moment before handing it over to Mr. Mori without comment.
"Secrets?" Mr. Mori asked. He looked at Hec, who shrugged.
"Well, good for you, you did make a copy. Does not look like pencil, though. And I don't see any mark-up."
I coughed. "I'm going to have to blame this one on my study partner."
"Good for you, Cyndy, although you might have gone so far as to encourage him to be more patient."
"Hec and I studied at my house last night."
"Rick?"
"Different study partner," Rick said.
Mr. Mori gave the four of us a look and said, "Curiouser and curiouser."
None of us volunteered more information.
He chuckled. "Well, let's take a look. I'll go ahead and mark out your address and phone number."
He compared mine with Cyndy's and said, "I think I'll start with Cyndy's after all."
He marked her address and phone number out and put her letter on the platen and adjusted the projector. "Okay, she uses standard greeting --"
"What happens," asked Harvey, "if they aren't sirs?"
"'Dear Sirs' is standard greeting. They won't be upset. Odd-ball greetings will not impress anyone."
"Oh."
"She identifies herself and our school, and our class. She gives my name as the teacher and the school address and contact information we showed you yesterday.
"Then she explains her interest in the 8080, that we will be studying it in class. It's a little bland, but that's actually good.
"And she asks for information on how to obtain documentation for both the microprocessor and its support parts. And she closes.
"Clean grammar, no attempts to dress things up. No particular reason to refuse the request. She will likely get some standard advertising material, a list of parts that she can request information about, and a standard order form for the datasheets."
He looked around the class. "Any questions?"
Nobody thought of anything immediately.
"Okay." He took Cyndy's out and gave it back to her, and replaced it with my typewritten inquiry, after marking out my address and phone number.
"We see Joe has done essentially the same here. You'd think they typed these up together yesterday in typing class." He chuckled to himself. "But you can see some differences in expression. Which do you guys think is more likely to get a good response?"
We spent a minute or two critiquing Cyndy's and my different ways of wording things.
"Okay." He gave me back my typewritten letter, then marked my phone number and address off the handwritten letter and put it on the platen. "In this version, Joe is explicit about his interest in the 6800. He isn't just interested in it for class, he thinks there are advantages. He even goes so far as to name all the microprocessors we are studying.
"I'm not sure that's a good idea. I'm not sure it's a bad idea. It is something of a sales pitch approach, however, and we need to be aware that it may be received as insincere. It may also be received as ambitious."
"The inquiry itself, he's becoming more ambitious. In addition to information on the CPU and related parts, he's asking for information about development tools.
"Now, whoever handles this at Motorola might not think about a student wanting to know about development tools. The development tools aren't cheap, and they may wonder whether Joe understands that. I do think they are more likely to reply with some information about development tools, since he asks.
"But he does sound like a kid who's asking bigger questions than he realizes. Sorry, Joe."
I nodded.
He continued, "On the one hand, I don't recommend Joe's approach in general. Smaller companies without secretary staff, an engineer may get the letter and tend to set the inquiry aside to respond to when he has more time to think about it. On the other, I'm not sure it would hurt with Motorola, they are a fairly large company, and it is more likely the letter will be handled entirely by a secretary. I guess we'll see."
He took my other letter off the platen and gave it back to me. "Harvey, are you willing to let us see yours?"
"Uh, sure." He already had his out, and handed it to Mr. Mori. "No problem if you mark out the address and phone number."
Mr. Mori scanned it. "Okay, here we see some differences." He marked out the personal information and put Harvey's letter on the platen. "Harvey is sending his to Apple, which is a company selling computers, not a company selling microprocessors. Well, selling the mainboard. So what he is asking for is comprehensive information on what the mainboard includes, and what options Apple sells for it at that apocalyptic price, which, low as it is for a computer that is almost complete, is enough to buy a pretty decent used car."
He looked up. "The letter is okay, but you guys should know. The apocalyptic price does not include the TV used as an output device. It also does not include the keyboard, power supply, or a cassette interface. So there is some assembly required. And Apple is selling them so fast they really haven't had time to print up a lot of the documentation materials that they might be willing to send to students.
"I don't want to discourage you, Harvey, but you might do better just sending an inquiry to MOS Technology about their KIM-1. It doesn't output to the TV, but it's much cheaper, and you can ask about the 6502 and peripheral parts at the same time. You might want to reference Joe's letter." He handed Harvey back his letter.
"Do you mind, Joe?" Harvey asked.
"No problem." I passed my second letter over to him, and he started copying from it.
Rick said, "Harvey, can I check your letter for ideas? I'm going to be sending mine to Southwest Technical Products, to ask them about their 6800-based computers."
"Sure." Harvey passed his letter to Rick.
Mr. Mori asked, "Anybody get a letter to RCA or TI?"
Hec said, "I've got one to send to RCA, but it basically looks like Cyndy's."
I said, "You might want to take a look at the COSMAC manual over at OC before you send it."
Hec looked thoughtful. "Maybe so."
Mr. Mori looked at Ted, but Ted shrugged and didn't say anything.
"Well, we're close to the bell. I'll let you guys clean up your notes or work on letters of inquiry."
I went to the front of the room and looked through more magazines while Rick worked on his letter. I found an ad for the MEK6800d1 and read it before taking it back to show Rick.
Hec read a little over Rick's shoulder and then went back to the magazine stack and thumbed through more magazines, finding an article on the COSMAC ELF, which he brought back to the table. Cyndy read over Hec's shoulder. When Harvey brought my letter back and Rick gave him his back, he saw what Hec and Rick were looking at, and went up and thumbed though more magazines, finding an ad for the KIM-1.
We passed the magazines around, and Barry dug up an ad for Intel's SDK-80 in another magazine. No one was able to find ads for any similar devices for the 9900.
While we were digging around in magazines, Mr. Mori was talking with Chuck.
After class, Mr. Mori snagged Ted to talk. Ted seemed hesitant.
Cyndy and Hec left together, deep in discussion. Rick and I headed out, and met Sapphire outside Rick's next class, and Sapphire and I walked to the typing lab together.
"Mom suggests that we should study where others are around."
"Oh?"
"My leaving early for your seminary seemed to worry her."
"We'll have to see what she feels comfortable with, I guess."
"But there are things I need to ask you that I won't feel comfortable about if there are other people around."
"I'm sure we can figure something out."
We stayed outside the classroom, holding hands and not saying much until Cyndy approached, and we went inside.
| TOC | Next | |
| Where it starts |
Copyright 2026 Joel Matthew Rees
[Edit record here: https://joelrees-novels.blogspot.com/2026/08/mark00-3809-2801-6502-indexed-addressing-including-indirect.html]