HCW - 3.06 - coding - opcodes
In the world of information technology in general, and particularly online, you will possibly see the saying that you should not worry about some issue or other: it's all just ones and zeros. This is, of course, a truism that comes from the absolute fact that, inside a computer, it is, indeed, all just ones and zeros.
As I hope I have, by now, proved, everything that goes on inside a computer simply has to do with whether a given circuit is on or off. And other circuits pick up the result of this circuit being on or off, and, themselves, turn on or off in order to make additional changes in other circuits, and so on, and so forth. But all of those circuits are either on, or off, and we interpret that as being true, or false, and or as one or zero. It's all just ones and zeros.
And the pattern of ones and zeros can store text for us, or add up numbers, or display a visual copy of George Seurat's Sunday afternoon on the island of la grande jatte.
But it's all just ones and zeros. And the fact that, with the ones and zeros, we can create a pattern of activity to do an enormous variety of things with a single piece of hardware.
This is known as programming, or coding.
If you have researched it at all, you will have come up with an enormous variety of names of programming languages. There are all kinds of computer programming languages, created to assist people who want to get computers to do something new, to create new functions in that basic computer hardware. At its core, at its most basic, all of this boils down to machine language, which is, indeed, all just ones and zeros. Most of these ones and zeros, in terms of programming in actual machine language are collected into groups of eight or sixteen, with additional groups of eight or sixteen that may provide data, usually memory addresses, where further data can be found, or further programming can be found, in order to allow us to proceed with the function that we want to create. So, at its heart, programming is, as well, all just ones and zeros.
But, looking at tables of ones and zeros can get rather mind-numbing, very quickly, and so, for the discussion of how to create programs, I'm going to start at one step up, with something called assembly language. Assembly language, or assembler, simply takes some abbreviations for these groups of ones and zeros, in order to remind you of what they do. Generally speaking, when you look at the actual patterns of ones and zeros, the mnemonics that start out the same will also have very similar patterns of ones and zeros. So using examples from assembler is not really cheating: it's just making it easier for you.
This a chart of the assembly language commands for one particular microprocessor. Each different model of microprocessor will have a different set of commands. Families of microprocessors that are related to each other will have very similar patterns of commands. Some microprocessors for specialized purposes will have somewhat different lists of commands than this one, but most will be similar.
This isn't the full list, because I do not intend to teach you how to create your own programs with this. For one thing, most of you will never want to actually do this. If you do want to do this, once again, I am giving you enough information for you to go and research on the Internet and find lots of tutorials, and images, and even videos, that will teach you how to do it. There are even programs that you can buy and download for your phone or computer that will simulate for you an older, and simpler, microprocessor, so that you can, in fact, do your own programming and create your own assembler programs for yourself, if you want to do so. I am just going to mention a few of the assembly language commands, and tell you a little bit about what they do. Hopefully this will be enough to prove to you what it is that computers actually do.
You will notice that there is quite a column of commands that all start out with ADD. These, of course, have to do with addition. The reason that there are so many of them is that there are many ways to make a computer add to numbers at this most basic level. For example, we can ask the microprocessor, if it has multiple registers, to add the contents of one register, to another register. Then there are even variations about what we want them to do with the answer. Do we want the value in the first register to be increased by the value of the second register? Do we want the second register to be increased by the value of the first register? Do we want the values of both registers to be added together, and then stored in some particular place in memory? All of these possibilities are actually different versions of addition, and so each of them, and a variety more, as you can see from the list, are separate opcodes: the operating codes. The actual machine language.
Another one that I want you to notice in this list is one that is spelled DEC, which stands for decrement. That may not sound like something that's particularly important. Just simply subtracting one from a particular value. However, in terms of programming a computer, very often we will want to repeat the same action a number of times. For example, as I mentioned, sometimes in microprocessor will only have an addition circuit built into it. If you want to do multiplication, you have to write a program that will do repeated addition with the addition circuit. When we want to repeat an action a number of times, the number of times that we want it to happen can be stored in a special register. Then, every time we repeat the function that we want to happen, we decrement that register. And we stop it when we get to zero. (How we stop it we will look at after another few commands.)
You also see, in this list, a command called HLT. This stands for halt. It's actually a fairly important command. Having a command that says stop running, or stop processing commands at this point, is fairly important. (There is also a joke that, built into pretty much every computer, there is an also a command that says Halt and Catch Fire.)
In this table, I want you to note a command labeled JMP. This is the command jump. The jump command will be followed by a value, that will be the address in memory where the computer is to jump to, and then start following the operating commands and codes at that point. This is a very important command, which we can use in order to have a variety of different functions built into our computer program, and allow us to jump to those pieces of programming, and perform those functions. Generally speaking, those functions, or sections of programming, will end with a return command (RET), which will jump us back to the command immediately following the jump command function that we encountered in the first place.
Then there are two labeled JNZ, or JZ. I mentioned, earlier, the decrement command, and noted that when the register we were using for the decrement command reached zero, we wanted to stop what we have been repeatedly doing, and do something else. That is what the JZ command will do. Jump when this register reaches zero. While the decrement register is still not zero we will JNZ or jump non-zero. Generally speaking that will be going back to the beginning of the function that we want repeatedly done. So, at this point, you should be starting to see how we get computers to decide which of the functions built into the program we should be operating.
There's another set of commands that start out MOV. These stand for move. Like the ADD, the move commands have a lot of variations. We are using them to move data: move data from one register to another, move data from a register to the main memory of the computer, move memory from one place in memory to a different place in memory. I told you that computer programs always and only do two things: they either do calculations, like addition, or they manage data. The move commands is an example of managing data.
The move commands are actually slightly misnamed. What they really are, are copy commands. The data is not moved in that it doesn't disappear from the original location when it is copied into another location. If we move data from a register to main memory, the data in that register is still there. So, we always have to be very careful, when doing programming, to fully understand what it is that the operating code has done. If we then want to use the register where the data that we moved to a memory location originally resided, then we have to decide, do we want to zero out that register, now that the data has been moved to main memory? Do we want to have that data still there, and continue to do some processing on it? Do we want to store a completely new data value in that register? We have to always keep in mind that there is no DWIM command in any microprocessor. (That stands for Do What I Mean, and, I submit, we will never have true artificial intelligence unless somebody implements that particular opcode.)
And, I have to mention timing, again. You might think that the timing cycles in the computer (which we have mentioned before) mean that one opcode gets processed every one clock tick. Unfortunately, no. some of the circuits are quit convoluted, and take more time to reach the end of the circuit. And some circuits are complicated, and call *other* circuits. (And sometimes those other circuits call other circuits ...) So sometimes (actually, most of the time) a single opcode takes multiple clock cycles to complete. Most of the time the processor itself takes care of the fact that nothing else takes place before the opcode cycle completes. But sometimes (remember those multi-core CPUs?) you have to think about it yourself ...
This is why people think programming, or coding, is complicated. It really isn't. It is really amazingly simple. But you have to force your mind into considering all of these tiny, simple, logical steps that go into building the functions, and the process, that you want the computer to perform for you. And you have to make sure you get them all, and not miss any.
Introduction and ToC: https://fibrecookery.blogspot.com/2025/12/how-computers-work-from-ground-up.html


No comments:
Post a Comment