Difference between revisions of "CSC103: DT's Notes 1"
Line 951: | Line 951: | ||
|- | |- | ||
| | | | ||
− | === | + | ===Time to Play A Game=== |
|} | |} | ||
Let's assume that we want to play a simple game of ''coding''. The game is simple: we want two people to have a conversation where each word or sentence that they can say is limited to a small set of preselected sentence, and each one is associated with a number. When the two people talk to each other, they must pick the number corresponding to the sentence, question, or answer they want to say. | Let's assume that we want to play a simple game of ''coding''. The game is simple: we want two people to have a conversation where each word or sentence that they can say is limited to a small set of preselected sentence, and each one is associated with a number. When the two people talk to each other, they must pick the number corresponding to the sentence, question, or answer they want to say. | ||
+ | |||
+ | [[Image:CSC103 Conversation 2.jpg|200px|right]] | ||
Here's a list of numbers and their associated sentences: | Here's a list of numbers and their associated sentences: | ||
Line 1,037: | Line 1,039: | ||
04: 20 00 ''(Go back to Line 00)'' | 04: 20 00 ''(Go back to Line 00)'' | ||
− | So now, even though this simple game is far from sophisticated or even fully functional, it should illustrate the principle that are at play when a processor executes a program that resides in memory. We explore this in the next section. By the way, if while reading the last ''algorithm'' shown above you thought to yourself "Hmmm... something strange is going to happen when we reach the last student in the class...", then you have a very logical mind, and Yes, indeed, you are right, our algorithm is not quite correct. But it's | + | So now, even though this simple game is far from sophisticated or even fully functional, it should illustrate the principle that are at play when a processor executes a program that resides in memory. We explore this in the next section. By the way, if while reading the last ''algorithm'' shown above you thought to yourself "Hmmm... something strange is going to happen when we reach the last student in the class...", then you have a very logical mind, and Yes, indeed, you are right, our algorithm is not quite correct. But it has allowed us to get the idea of what's ahead of us. |
+ | |||
− | |||
{| style="width:100%; background:#FFD373" | {| style="width:100%; background:#FFD373" | ||
|- | |- | ||
| | | | ||
− | === | + | ===Computer Memory=== |
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
We are very close to understanding how the processor runs programs. We have seen that computers work with electricity, and that information is coded as 0s and 1s. We refer to an information cell that contains a 0 or a 1 as a ''bit''. The memory is a collection of boxes that contain bits. We refer to these boxes as ''words'', or ''Memory words''. | We are very close to understanding how the processor runs programs. We have seen that computers work with electricity, and that information is coded as 0s and 1s. We refer to an information cell that contains a 0 or a 1 as a ''bit''. The memory is a collection of boxes that contain bits. We refer to these boxes as ''words'', or ''Memory words''. | ||
− | + | Now is a good time for a bit of magic! The magic requires building a simple circuit with AND and NOT gates, as shown below: | |
<br /> | <br /> | ||
Line 1,184: | Line 1,112: | ||
It's a long structure made up of words. Words are numbered, from 0, to a large number, which is actually equal to the size of the memory, in increments of 1. For example, when you read the sticker for a computer on sale at the local store, it may say that the computer sports 4 Gigabytes of RAM. What this means is that the memory, the ''Random Access Memory'' (RAM), is comprised of words containing numbers, the first one associated with a label of 0, the last one with a label (almost) equal to 4,000,000,000. By the way, ''Giga'' means billion, ''Mega'', million, and ''Kilo'', a thousand. | It's a long structure made up of words. Words are numbered, from 0, to a large number, which is actually equal to the size of the memory, in increments of 1. For example, when you read the sticker for a computer on sale at the local store, it may say that the computer sports 4 Gigabytes of RAM. What this means is that the memory, the ''Random Access Memory'' (RAM), is comprised of words containing numbers, the first one associated with a label of 0, the last one with a label (almost) equal to 4,000,000,000. By the way, ''Giga'' means billion, ''Mega'', million, and ''Kilo'', a thousand. | ||
+ | |||
+ | {| style="width:100%; background:#FFD373" | ||
+ | |- | ||
+ | | | ||
+ | ===The Processor=== | ||
+ | |} | ||
Before we figure out what kind of number ''code'' the processor can understand, let's talk for an instant about the role of the processor relative to the memory. The processor is a machine that constantly reads numbers from memory. It normally starts with the word stored in the cell with label 0 (we'll say the ''memory cell at Address 0''), reads its contents, then moves on to the next word at ''Address 1'', then the next one at ''Address 2'', and so on. In order to keep track of where to go next, it keeps the address of the cell it is going to access in a special word it keeps internally called '''Program Counter''', or '''PC''' for short. PC is a special memory word that is '''inside''' the processor. It doesn't have an address. We call such memory words when they are inside the processor '''registers'''. | Before we figure out what kind of number ''code'' the processor can understand, let's talk for an instant about the role of the processor relative to the memory. The processor is a machine that constantly reads numbers from memory. It normally starts with the word stored in the cell with label 0 (we'll say the ''memory cell at Address 0''), reads its contents, then moves on to the next word at ''Address 1'', then the next one at ''Address 2'', and so on. In order to keep track of where to go next, it keeps the address of the cell it is going to access in a special word it keeps internally called '''Program Counter''', or '''PC''' for short. PC is a special memory word that is '''inside''' the processor. It doesn't have an address. We call such memory words when they are inside the processor '''registers'''. | ||
− | The processor has three important registers that allow it to work in this machine like fashion: the '''PC''', the '''Accumulator''' (shortened to '''AC'''), and the ''' | + | The processor has three important registers that allow it to work in this machine like fashion: the '''PC''', the '''Accumulator''' (shortened to '''AC'''), and the '''Instruction Register''' ('''IR''' for short). The PC is used to "point" to the address in memory of the next word to bring in. When this number enters the processor, it must be stored somewhere so that the processor can figure out what kind of action to take. This holding place is the '''IR''' register. The way the '''AC''' register works is best illustrated by the way we use a regular calculator. Whenever you enter a number into a calculator, it appears in the display of the calculator, indicating that the calculator actually holds this value somewhere internally. When you type a new number that you want to add to the first one, the first number disappears from the display, but you know it is kept inside because as soon as you press the = key the sum of the first and of the second number appears in the display. It means that while the calculator was displaying the second number you had typed, it still had the first number stored somewhere internally. For the processor there is a similar register used to keep intermediate results. That's the '''AC''' register. |
<br /> | <br /> | ||
[[Image:CSC103MotherBoard.jpg|250px | right]] | [[Image:CSC103MotherBoard.jpg|250px | right]] | ||
Line 1,198: | Line 1,132: | ||
But there is another subtlety here. Not all numbers are instructions. Just as in our games some numbers corresponded to sentences and others words that needed to be added at end of sentences ("did you like", "homework" for example), some numbers represent actions, while others are just regular numbers. When the processor starts absorbing the contents of memory cells, it assumes that the first number it's going to get is an instruction. When it analyzes this number internally | But there is another subtlety here. Not all numbers are instructions. Just as in our games some numbers corresponded to sentences and others words that needed to be added at end of sentences ("did you like", "homework" for example), some numbers represent actions, while others are just regular numbers. When the processor starts absorbing the contents of memory cells, it assumes that the first number it's going to get is an instruction. When it analyzes this number internally | ||
+ | |||
+ | {| style="width:100%; background:#FFD373" | ||
+ | |- | ||
+ | | | ||
+ | ===The Cookie Monster=== | ||
+ | |} | ||
+ | [[File:CookieMonsterPacMan.png|right|150px]] | ||
+ | The processor is just like the cookie monster. But a cookie monster acting like Pac-Man, a Pac-Man that follows | ||
+ | a straight path made of big slabs of cement, where there's a cookie on each slab. Our Cookie-Monster-Pac-Man | ||
+ | always starts on the first slab, which is labeled 0, grabs the cookie, eats it, then moves on to Slab 1, | ||
+ | grabs the cookie there, eats it, and keeps on going! | ||
+ | |||
+ | The processor, you will have guessed, uses the memory words as the slabs. It always starts by | ||
+ | going to the memory word at Address 0. It does this because the Program Counter register (PC) is | ||
+ | always going to be reset to 0 before the processor starts executing a program. | ||
+ | The processor sends the address 0 to memory, grabs the number that it finds at that location, | ||
+ | puts it in the IR register | ||
+ | for safe keeping, eats it to figure out what | ||
+ | that number means, what action needs to be performed, and then performs that action. | ||
+ | |||
+ | That's it! That's the only thing a processor ever does! | ||
+ | |||
+ | Well, almost the only thing. While it is performing the action, it also increments the PC register by 1, | ||
+ | so that now the PC register contains 1, and the processor will repeat the same series of steps, but this | ||
+ | time will go fetch the contents of Memory Location 1. | ||
+ | |||
+ | This means that the numbers that are stored in memory will represent some action to be performed by the | ||
+ | processor. They represent a ''code''. That code defines a language. We call it ''Assembly Language''. | ||
+ | Instead of trying to remember the number associated with an action, we use a short word, a ''mnemonic'' | ||
+ | that is much easier to remember than the number. Writing a series of mnemonics to instruct the processor | ||
+ | to execute a series of action is the process of writing an ''assembly-language program''. | ||
+ | |||
+ | The first mnemonic we will look at is LOD-C ''n''. It means LOAD a Constant into the AC register. LOD-C | ||
+ | is always followed by a number. | ||
+ | |||
+ | LOD-C 3 | ||
+ | |||
+ | The instruction above, for example, means load the number 3 into the AC register. | ||
+ | |||
+ | LOD-C 11 | ||
+ | |||
+ | The instruction above would mean load the number 11 into AC. | ||
+ | |||
+ | Before we write a small program we need to learn a couple more instructions. | ||
+ | |||
+ | STO 10 | ||
+ | |||
+ | The STO ''n'' mnemonic, or instruction, is always followed by a number representing an address in memory. It means | ||
+ | STOre the contents of the AC register in memory at the location specified by the number. So STO 10 | ||
+ | tells the processor "Store AC in memory at Address 10", or, "Store AC at Address 10" for short. | ||
Revision as of 10:37, 22 September 2013
--© D. Thiebaut 08:10, 30 January 2012 (EST)