2801
Basic Addressing Modes for All
Register Sets for 1802 and 9900 | TOC |
(Continuing the dry technical stuff, but it's still important. Don't go away.)
Mr. Mori continued his explanation, not yet putting anything on the whiteboard.
"Every CPU needs to address its registers, so register addressing should be considered something every CPU has."
I volunteered, "Is the 1802 X register how it addresses its index register?"
"That's a good way of looking at it." He frowned approvingly and nodded before continuing.
"Now, the register is often implicit in the instruction. For instance, the 6800 has an instruction to set the carry flag and another to clear it. The carry flag is explicit and inherent to the instruction, but the condition code register is implicit."
Chuck asked, "Is the stack register implicit in call instructions, then?"
Mr. raised both eyebrows approvingly. "You could say that. Yes, you could definitely say that."
He paused and thought. "But maybe not exactly for the 1802."
He tilted his head in thought. "Maybe."
"Anyway, another mode pretty much every CPU has is called the immediate mode in the documentation for all five of the processors we are looking at. It can also be called literal mode, among other things.
"A constant value in the instruction stream is used as the source of a load or move instruction. In some cases the constant might be in the instruction itself, in others, it will come immediately," he paused and began diagramming on the whiteboard, "after the instruction op-code, like this:"
He paused when he finished the diagram.
"You know, for the instruction stream, it's natural to draw memory putting the lowest byte at top, but that will feel upside down for data."
Barry asked, "So which way is it?"
"Whichever way makes most sense."
"Oh. And how do we know which we makes sense?"
Mr. Mori didn't elucidate. Instead, he continued, "Where that immediate constant is used and what is done with it is determined by the instruction."
Hec volunteered, "Is the PC implicit in immediate mode instructions?"
"Yeah." Mr. Mori shook his head in amusement. "I don't think I pay you guys enough to do my job for me."
He grinned as we all laughed.
"Note that immediate constants can also be two bytes in some cases."
He drew this to the side:
Bob asked, "Is byte 1 the low byte or the high byte?""Yes." Mr. Mori waited.
But we didn't laugh.
He shrugged in mock disappointment, but continued. "It depends on whether the processor is less significant byte first, or little-endian, or more significant byte first, big-endian. In the 8080 and 6502, byte 1 will be the low byte, but in the 6800, byte 1 will be the high byte."
Scott asked, "Isn't least significant first the canonical form?"
"I have read partially valid arguments for making either byte order canonical, but the best argument is just that everybody should do the same thing -- and that is usually actually a good argument to not do the same thing."
Scott scratched his head.
Mr. Mori thought a moment, then added, "For what it's worth, IBM usually designs their processors most significant first. I suppose we should discuss the advantages and disadvantages and historical reasons at some point, along with other philosophical questions like Barry's. Can you wait for that?"
(Irony in history, but IBM did not design the CPU for the 5150, did they?)
"Yeah, I guess."
"Thanks." Mr. Mori turned back to the board and erased and rewrote parts of the second diagram, then added to it:
"Absolute mode is where the address of the operand in memory, not the operand itself, is part of the instruction stream. The CPU loads the address out of the instruction stream into a temporary register, then loads the operand from that address. In this case, I'm showing the data operand as a 2-byte operand, but the data could be one byte or three, or any length, really, depending on the instruction, and on the instructions that follow.
"This mode is also called extended mode, or external address mode, among other things."
Barry asked, "So it's kind of like immediate mode, but the immediate value is the address where the actual operand is?"
"That's one way of looking at it."
"How does the CPU know the difference?"
"Basically, by the instruction op-code. For example, if it's an ADD instruction, there will be different op-codes for ADD immediate mode and ADD absolute mode. The immediate mode op-code grabs the number in the instruction stream as the operand. The absolute mode op-code first grabs the address from the instruction stream, then grabs the actual operand from memory at the address it just grabbed. Either way, once it has the operand, it can ADD it."
He let us digest this, then he added, "There are some CPUs that do things differently. For instance, in some, the operand itself says it's an address. But I don't want to talk about them, at least, not now. Likely to just confuse everyone. I don't know of any microprocessors that do that, anyway."
(No microprocessors that did that yet, anyway.)
Cyndy asked, "So little-endian processors will have the low byte first for addresses, too, not just data?"
"Yep."
She asked another question, "Is there a single-byte form of absolute address?"
Mr. Mori grinned. "Thank you for bringing that up. The 6502's zero page mode and the 6800's direct page mode are just that, short absolute addresses in the first 256 bytes of memory." He wrinkled his forehead. "I don't need to draw that one for you all, do I? It's just a single byte following the op-code, and you the address unit concatenates zeros on top of that?"
Nobody asked him to.
But Cyndy had one more question -- "Can you put addresses in registers?"
Mr. Mori mugged astonishment. "Cyndy, I'm beginning to feel threatened! That was just where I wanted to go next." Then he grinned. "Guys, is it time for talking about addresses in registers?"
Cyndy looked pleased with herself, and no one said no.
"You can, indeed, put addresses in registers. In fact, when any of these CPUs gets an absolute address and uses it, it stores it temporarily in a register or buffer that programmers can't access directly and puts it on the address bus from there. But we can also keep addresses in registers that we do have access to.
"Using a register like this is a mode that is often called register indirect mode. The instruction specifies a register, and the register contains the address. It feels a little superfluous to draw this, but we'll want to refer to the concepts in a moment:"
Mr. Mori paused again. "Oh. This time it's data I'm diagramming with the high addresses on top." He picked up the eraser, hesitated, then put it back down. "Nah, Well go with this. There's a reason. Bear with me."
Bill chuckled. "I don't think any of us really knows enough to complain."
Mr. Mori grinned again and continued.
"The 8080 has limitations, as I've mentioned, about which register pairs can be used with which instructions. But the 1802 and 9900 allow any register to be used to point to memory. The 6800 and 6502 are a bit special, but, again, as I've mentioned, they use their index registers to do this.
"But before I talk about register indirection on the 6800 and the 6502, I want to talk some about indexed mode addressing. I'm going to erase and rewrite parts of this diagram. You probably want to draw a second diagram instead in your notes."
Cyndy had already started making her second diagram as Mr. Mori altered the one on the board.
"We start with a register pointing to something in memory, in this case we'll make it a name with a length byte. I'm showing the ASCII character codes in decimal here.
"The base register points to the length of the name. But say we want to take a look at each letter in the name. We can add one to the pointer and look at the 'M', then add one again and look at the 'o', then add one again and look at the 'r'." He indicated each location in memory as he spoke. "But if we now want to look at the length again, we have to back up." He pointed back. "We have to subtract three, if we remember that's how far we've bumped."
"Or we can keep the original, copy it to another register, and bump that register up and down to look at the letters one at a time. That way we don't forget where the name is. And that's the two registers I'm showing in this diagram.
"Adding and subtracting one are so common that, in most CPUs, they have their own op-codes, mnemonically named INC and DEC or something similar, for INCrement and DECrement."
"Now, what if we want to jump directly to the score that comes after the name? Does everyone see how to do that?"
I waited for somebody else to say it, but no one did, so I said it. "Make another copy of the base address, load the length in from memory where it's pointing, and add the length to the copy?"
Mr. Mori nodded. "That's one way. Good catch about getting the length from the string itself. A common beginner's mistake would be to just load it as the immediate value 4. That would work with my name, but not yours.
"By the way, this technique of adding an offset to a base address in registers requires multiple instructions on all of our microprocessors."
(This would change, of course, with the 68000 and 6809, but that's down the road a ways.)
"This practice of pointing at parts of objects in memory at offsets from the base of the object is called indexing, essentially from the concept of indexing into an array or table. Or from matrix algebra.
"While we can do it in steps like we've been talking about, the 9900 provides a short-cut indexed addressing mode for arrays when their location is a constant known beforehand, where the instruction stream provides a constant absolute base address and a register provides an offset. This mode doesn't help if you have both the base and offset in registers."
He took some time to draw the following:
"Base address literal in the instruction stream points to the base of the name, and the register gives the offset to the letter."
Karl complained, "I'm not seeing why that has any advantage over just putting the address in the register and adding."
Mr. Mori pursed his lips and nodded before replying. "You do have a point. But you can do this addressing in a single instruction. And sometimes, you're short of registers. And sometimes, you're more interested in keeping the offset as a variable than in having the addresses themselves.
"Now, in fact, this only works when the base address is known in advance, so you do have a point or two."
"But I don't know what point I have," Karl laughed, and most of us laughed, too.
"More things we need to talk about later," Mr. Mori answered.
"One useful case we can talk about might be when making an identical copy of something. The 9900 also has auto-increment and auto-decrement modes which can be used, but maybe you can see that the same offset in the register can be used with with the base offset of each array. I will need to show you some concrete copying examples, later."
"But for now," he erased and rewrote parts the diagram, "I want to point out that the 9900 indexed mode is just adding numbers, so we can also do indexing like this:"
"Using the indexed mode this way, the register points to the object in memory, and the instruction stream contains a constant offset. We can see that this can be useful if we know in advance that a constant offset directly accesses the test score for records of a certain type -- which would not really be the case above. But diagramming a case that would apply would require more explanation than I want to dig into now."Can I skip that for the moment?"
Silence.
"Nobody says no, so we'll come back to it. As I say, this mode can help make the 9900 a bit faster than it otherwise would be, because what would take several instructions on, say, the 8080 or the 1802, can be done in one on the 9900.
"Now the 6800 has a similar mode using the X index register, but the constant in the instruction stream is limited to a single byte. This works well in the mode I just showed you, where X points to the object and the instruction specifies a small constant offset.
"But if you want the constant part in the instruction stream to address the base of an array, and the X register to contain the offset, on the 6800 the array has to start in the direct page."
I raised my hand. "Hang on. So the constant offset is a byte. I think I can see that. But you can use that byte as a short address in the first 256 bytes of memory, and then use the X register to hold the offset."
"Correct."
(Again, if constant base + variable index mode is interesting, but the 6809 and 68000 are overkill, check out the addressing modes of the 6805, which, as I say, would not be introduced for another couple of years.)
"Okay, I think I'm following this."
Cyndy looked at me. "I'm glad you are."
Chuckling again.
"We'll go over this more carefully later. Does anyone else want to stop to think out loud or ask questions?"
We didn't.
"By the way, for the 6800, register indirect is provided by using a zero offset in the indexed mode. This feels wasteful, but it allows the 6800 to provide indexed mode addressing in addition to register indirect for all general operators, which the 8080 and 1802 do not. There are lots of trade-offs in CPU design.
"And again providing this mode actually does help make up for the dearth of registers."
He paused, took a breath, and looked around at us.
"Uhm, the 6502 does things sort of similar to the 6800, but different, and it gets really confusing. I'm afraid I'm losing too many of you, so let's take a breather."
We all leaned back and some of us stretched.
I raised my hand. "Rick and I are going to write and ask for documentation, like you suggested, and, I think, about whether they can make prototyping kits available to students."
Rick interjected, "I'm actually more interested in that computer kit from Southwest Technical."
"You've got that kind of money?" Hec asked.
"I was about to say," began Mr. Mori.
Rick responded, "Maybe, from the part-time job I'm working. Or maybe I can talk Joe into pooling money with me. Anyway, I want to ask about it."
(I know. I'm stretching belief here. But it's necessary for the story.)
"Okay," Mr. Mori turned the projector back on and got out some blank transparencies. "Let's talk a little bit about how to word technical inquiries, and what information to include. That's actually an important technical skill."
Mr. Mori prompted us through the basics of writing letters of inquiry about engineering documentation and equipment, letting us do as much of the writing as we would. As a class we came up with lots of good ideas.
In the process, several of us put together draft inquiries.
Mr. Mori also gave us his contact information at the school to include in the letters.
Cyndy took lots of useful notes about inquiry letters, and Mr. Mori came over to check them.
"Can I commandeer these notes and type them up for the ditto machine for the whole class?"
"I want to use my notes for typing practice," she replied.
"But can I get a photocopy?"
Cyndy twirled a curl, and said, "Sure. I'll bring them by after school."
And the bell rang.
"By the way, since you're students, hand-written letters of inquiry for documentation will be okay, if your writing is legible. We'll pick up the addressing overview again tomorrow. I think you guys need some time to let what we've covered sit and simmer a bit, anyway."
(Did I mention that, if Mr. Mori had both the knowledge and wisdom he is demonstrating here, he would have been head-hunted away from the high school into engineering management by any one of a dozen companies at this time?)
Cyndy gathered her books, glanced quickly at Rick and me, and left.
Hec looked at Rick and me, too. "Uhm, I've got to go, guys, okay if we check notes later?"
Rick and I nodded, and Hec left, after which we finished touching up our notes.
"Looks like your letter is pretty complete," Rick commented as we left.
"I might be able to use it for typing practice today, if you don't mind waiting."
"I can do that. If I don't see you after the bell, I'll come to the typing lab."
"Sounds good."
He turned in to his class and I headed to the typing lab.
TOC | Next |
No comments:
Post a Comment