From last time you'll remember that computer memory can hold different types of information, namely data and instructions. Both of these are just numbers that are given special meaning either by the programmer (data) or by the computer processor itself (instructions). Last time we talked about how normally the processor reads one instruction, performs the action associated with that instruction, then reads the next instruction, performs it, and on and on. The program counter of the processor holds a number which is the memory address of the next instruction it is going to load for the processor to perform.
Imagine as if all of the instructions of a computer program were in the computer memory all in a row, and the program counter points to the first one, then the next one, then the next. That's pretty much how it works, except that model doesn't allow for the repetition of any instructions (or in other words, the processor can never execute an instruction that has a lower address than its current program counter). It also would mean that you can never skip an instruction that maybe you don't want to do right now. The answer to both of these problems is collectively "jumping" and "branching." A jump is a processor instruction that UNconditionally changes the program counter (and thus the source of the next instruction) to a new value, instead of just the next one in the line. A branch is a processor instruction that conditionally changes the program counter (meaning it may or may not change the program counter, depending on whatever condition the programmer dictates).
We'll talk in a later post about what the difference is between conditional branching and unconditional jumping, but first let's go through an example of regular, unconditional jumping. Let's say your program counter (PC) is currently set to 16 (PC=16). This means that the next instruction that will be executed by the processor is located at memory address 16. The processor loads the instruction at memory address 16, and it finds:
jump 60
This means that executing this instruction directly changes the value of the program counter to PC=60. For the processor's next instruction it looks at memory address 60. Let's say at memory address 60 it finds:
jump 16
This instruction will directly change the program counter to PC=16, which you'll recognize is where it just was last time. This little example is very silly because all it does is jump between PC=16 and PC=60 over and over, like an infinite loop. You would never see this in a real program, but it gives you an idea of how unconditional jumping works, jumping both backwards and forwards. In this example if there hadn't been another jump instruction at PC=60, and instead there had been an add, or load, or store, then the program counter would have just behaved like normal after executing the instruction at PC=60, increasing to the next instruction in the list, and so on.
So in summary, jump instructions directly change the value of the processor's program counter (PC), controlling which instruction will get executed next by the processor. Next time we'll look at conditional jumping, or "branching," and where these conditions come from in the first place.
NOTE: when I say that memory location 16 contains the instruction "jump 60," I don't mean that the word "jump" is somehow written into that memory location. What really is going on is that there is a series of bits that would look pretty random to you or me at first glance, but that the processor interprets as "jump" and then the binary number 60. I'm going to talk about computer instructions in terms of English, not in terms of binary.
Showing posts with label computer memory. Show all posts
Showing posts with label computer memory. Show all posts
Monday, February 28, 2011
Sunday, February 27, 2011
Remember these things
We know computer memory is a big long list of bytes that can be read and written (loaded and stored), but now we need to talk about the types of information that is contained in the memory. There are two broad categories of information that the computer keeps track of, namely, instructions and data. Both of these are just numbers when you get right down to it, because that's all the memory can hold in it, so the difference between them lies in how the computer treats these different kinds of information.
Let's talk about data first. The word "data" is the plural form of the word "datum," which just means "piece of information." So "data" means "many pieces of information." All data in a computer is represented as binary numbers, but those binary numbers can stand for a variety of different things. It just depends on what meaning the computer's programmer decides to give those binary numbers (which the computer doesn't really care about, by the way). The programmer could consider the byte 0b0001 0001 to mean the decimal number 33 or the ASCII symbol '!' (ASCII is just a standard convention for treating single-byte numbers as text symbols). It is very common for things we want to represent in a computer to take up more than one byte. For example, numbers larger than 255 must be represented by more than one byte. Also, picture and movie files are larger than one byte. Large quantities of text can be represented by very long lists of single bytes that are interpreted as ASCII symbols (like the example above). In the end, it is the value of the byte(s) and the context which the programmer gives them that really determines what the data in a computer memory means.
The other kind of information that can be stored in computer memory is computer processor instructions. I know we haven't talked about how computer processors work at all yet, but I think this one detail about their operation cannot be avoided at this time. Processors work by reading a part of the computer memory, and then depending on what was contained in that part of the computer memory (or that "instruction"), the processor will perform an action. There is a counter in the processor called the "program counter" which keeps track of the place in memory that the processor will get its next instruction. After every instruction is processed the program counter gets increased (i.e., "counts up") to move on to the next instruction. After that instruction is processed, the program counter "counts up" again, moving on to the next instruction, and so on.
Just like how data is just a series of arbitrary bytes until the programmer steps in to give those bytes meaning, instructions are just series of arbitrary bytes until the processor's designer steps in to give those bytes meaning. There are many different types of instructions, including adding, loading, storing, jumping and branching. There are also many more, but these are the basic building blocks of computer science that we're focusing on for now. We've already talked in detail about adding, loading and storing.
Jumping and branching are special instructions that deal with changing the program counter. Normally the program counter just increases (counts up) to the next instruction after each previous instruction is completed. This doesn't allow for repeating any previous instructions (or looping, as it's called in programming). Jumping and branching allow for the program counter to be set to whatever value the programmer wants. This can include increasing the program counter, or decreasing it. Next time we'll talk more about jumping and branching and what it allows computers to do that would otherwise be impossible.
Note: Yes, I know that I was inconsistent about treating the word "data" as a plural word. This is pretty much universal in all technical and academic literature. Even though the word represents a plural concept, it is almost always treated grammatically as a singular. I will be following this convention from here on out.
Let's talk about data first. The word "data" is the plural form of the word "datum," which just means "piece of information." So "data" means "many pieces of information." All data in a computer is represented as binary numbers, but those binary numbers can stand for a variety of different things. It just depends on what meaning the computer's programmer decides to give those binary numbers (which the computer doesn't really care about, by the way). The programmer could consider the byte 0b0001 0001 to mean the decimal number 33 or the ASCII symbol '!' (ASCII is just a standard convention for treating single-byte numbers as text symbols). It is very common for things we want to represent in a computer to take up more than one byte. For example, numbers larger than 255 must be represented by more than one byte. Also, picture and movie files are larger than one byte. Large quantities of text can be represented by very long lists of single bytes that are interpreted as ASCII symbols (like the example above). In the end, it is the value of the byte(s) and the context which the programmer gives them that really determines what the data in a computer memory means.
The other kind of information that can be stored in computer memory is computer processor instructions. I know we haven't talked about how computer processors work at all yet, but I think this one detail about their operation cannot be avoided at this time. Processors work by reading a part of the computer memory, and then depending on what was contained in that part of the computer memory (or that "instruction"), the processor will perform an action. There is a counter in the processor called the "program counter" which keeps track of the place in memory that the processor will get its next instruction. After every instruction is processed the program counter gets increased (i.e., "counts up") to move on to the next instruction. After that instruction is processed, the program counter "counts up" again, moving on to the next instruction, and so on.
Just like how data is just a series of arbitrary bytes until the programmer steps in to give those bytes meaning, instructions are just series of arbitrary bytes until the processor's designer steps in to give those bytes meaning. There are many different types of instructions, including adding, loading, storing, jumping and branching. There are also many more, but these are the basic building blocks of computer science that we're focusing on for now. We've already talked in detail about adding, loading and storing.
Jumping and branching are special instructions that deal with changing the program counter. Normally the program counter just increases (counts up) to the next instruction after each previous instruction is completed. This doesn't allow for repeating any previous instructions (or looping, as it's called in programming). Jumping and branching allow for the program counter to be set to whatever value the programmer wants. This can include increasing the program counter, or decreasing it. Next time we'll talk more about jumping and branching and what it allows computers to do that would otherwise be impossible.
Note: Yes, I know that I was inconsistent about treating the word "data" as a plural word. This is pretty much universal in all technical and academic literature. Even though the word represents a plural concept, it is almost always treated grammatically as a singular. I will be following this convention from here on out.
Friday, February 25, 2011
Bathtubs are memorable
I'm going to get right to the point. All that nonsense about lined up bathtubs full of buckets? I made it up. I was trying to teach symbolically. Did it work?
Anyway, here's the breakdown. In that metaphor, the lined-up bathtubs collectively represent the memory system of a computer. The bathtubs represent individual bytes, and the buckets in the bathtubs represent individual bits. The numbers? Those were numbers. It would have taken too much imagination on my part to invent a replacement for numbers.
So am I trying to tell you that the memory system of a computer is a giant series of bytes full of bits? Pretty much. That's not how it's physically implemented in the computer (have you ever noticed that memory chips are square-ish, and not 3 meters long and impossibly thin?), but we're not quite ready to talk about the physical implementation of computers yet.
As for the loading and storing, I pretty much just explained that one directly also. There are fundamentally two things that a computer can do with its memory system. It can "load" values out of it, and you can "store" values into it. I know, it might seem like "load" should mean "put something into the memory," but remember that when a number is "loaded" out of the memory, it's "loaded" into somewhere else. Yeah, that doesn't really cut it for me either, but remember that the opposite of "load" is "store," and that sounds even more like you're storing something into the memory (and this time you really are).
All of the bytes in a memory system are numbered. There's an order, and all of the bytes in memory have an "address." The computer loads numbers out of a particular address, and stores numbers into other addresses. A computer might "store 128 into memory address 183,820,"or "load memory address 1,024 and put its contents into variable X." One of the basic things that makes this a reliable system to use is that the contents of the memory system cannot change unless the computer explicitly changes it. That allows us to confidently store numbers into the memory and get the same number back out when we load it later.
Just to recap, a computer's memory system consists of a very long string of bytes that hold their value between accesses. These bytes can be accessed using a specific numbered address for each byte. These accesses can be loads or stores. Loads read numbers out of the memory, and stores put numbers into the memory.
Next time we'll talk about what kinds of things are stored in memory, in preparation for explaining our last big ingredient to building a working computer: branching.
Tuesday, February 22, 2011
Thanks for the memories
I apologize for the all-too-long post last time. It probably should have been two posts. Today, however, I want to paint a mental picture.
Imagine you are in an empty void, floating high above what looks like a thin, straight line starting directly beneath you, but stretching on extremely far ahead of you. You are slowly floating down to where this line begins. As you get closer, you notice that the line is not as narrow as you first had supposed. You get closer still and notice that this entire line is comprised of a series of bathtubs. These bathtubs each have a sign next to them with a number. The one directly below you is marked "0." Next to it is bathtub "1." The furthest you can read with your naked eye seems to be bathtub "23," but you can tell that all of the bathtubs in the line have increasing numers the further away they get from you, so you assume the numbering system just continues on forever.
You finally touch down next to bathtub "0." Strangely enough, this bathtub is not full of water or even Jell-o, but rather it is full of buckets. 8 buckets right in a row. Of course you assume that the buckets are full of Jell-o, but this isn't the case either. The buckets have numbers in them, single binary digits, one digit per bucket. 8 digits per bathtub. Some buckets have 0b0, and some have 0b1.
These bathtubs do not have regular faucets to fill them with water. Instead, where the faucet should be, there are two buttons. One is marked "load," and the other, "store." You look around, and finding yourself alone decide that it wouldn't hurt anyone to try out these buttons. You reach out with your left hand and timidly press the load button of bathtub 0. Electricty runs through your left hand, up your arm, across your body and down to your right hand, where suddenly appears the binary number 0b1101 1000 floating above the palm of your right hand. Don't worry, it doesn't hurt. You notice that this is the exact same binary number that is in the bathtub. It's as if the load button copied the value of the bathtub into your hand.
You next try the store button. Again, electricity runs through your body, this time from right to left, but the value of bathtub 0 remains unchanged. That's odd. Next you decide to try an experiment where you load the value of one bathtub, and try to store it somewhere else. You press load again on bathtub 0, and again the value 0b1101 1000 appears in your right hand. You move to bathtub 1, which has the value 0b0000 0000, and press its store button. Suddenly bathtub 1's value changes to match the value in your hand, and the value that was in bathtub 1 is lost forever. After some more experimentation of loading values from various bathtubs and storing them into other bathtubs, you start to reach the limits of how much fun you can have with this.
It gets boring moving numbers around if that's all you can do. You start wishing you could do something else with these numbers. You wish you could at least add them together (see, I told you addition was going to be important), and do something interesting with the loaded numbers before you store them away again.
And then you wake up. Or something. I'm not very good at endings for dream sequences. Next time we'll talk about the interpretation of the dream, and what these bathtubs and buckets have to do with real computers.
Imagine you are in an empty void, floating high above what looks like a thin, straight line starting directly beneath you, but stretching on extremely far ahead of you. You are slowly floating down to where this line begins. As you get closer, you notice that the line is not as narrow as you first had supposed. You get closer still and notice that this entire line is comprised of a series of bathtubs. These bathtubs each have a sign next to them with a number. The one directly below you is marked "0." Next to it is bathtub "1." The furthest you can read with your naked eye seems to be bathtub "23," but you can tell that all of the bathtubs in the line have increasing numers the further away they get from you, so you assume the numbering system just continues on forever.
You finally touch down next to bathtub "0." Strangely enough, this bathtub is not full of water or even Jell-o, but rather it is full of buckets. 8 buckets right in a row. Of course you assume that the buckets are full of Jell-o, but this isn't the case either. The buckets have numbers in them, single binary digits, one digit per bucket. 8 digits per bathtub. Some buckets have 0b0, and some have 0b1.
These bathtubs do not have regular faucets to fill them with water. Instead, where the faucet should be, there are two buttons. One is marked "load," and the other, "store." You look around, and finding yourself alone decide that it wouldn't hurt anyone to try out these buttons. You reach out with your left hand and timidly press the load button of bathtub 0. Electricty runs through your left hand, up your arm, across your body and down to your right hand, where suddenly appears the binary number 0b1101 1000 floating above the palm of your right hand. Don't worry, it doesn't hurt. You notice that this is the exact same binary number that is in the bathtub. It's as if the load button copied the value of the bathtub into your hand.
You next try the store button. Again, electricity runs through your body, this time from right to left, but the value of bathtub 0 remains unchanged. That's odd. Next you decide to try an experiment where you load the value of one bathtub, and try to store it somewhere else. You press load again on bathtub 0, and again the value 0b1101 1000 appears in your right hand. You move to bathtub 1, which has the value 0b0000 0000, and press its store button. Suddenly bathtub 1's value changes to match the value in your hand, and the value that was in bathtub 1 is lost forever. After some more experimentation of loading values from various bathtubs and storing them into other bathtubs, you start to reach the limits of how much fun you can have with this.
It gets boring moving numbers around if that's all you can do. You start wishing you could do something else with these numbers. You wish you could at least add them together (see, I told you addition was going to be important), and do something interesting with the loaded numbers before you store them away again.
And then you wake up. Or something. I'm not very good at endings for dream sequences. Next time we'll talk about the interpretation of the dream, and what these bathtubs and buckets have to do with real computers.
Subscribe to:
Posts (Atom)