And herein lies the power of a computer. Even though the program consists of very simple instructions, the overall result can be very impressive, due mostly to the speed at which the computer can process the instructions. Even though each step in the program is very simple, the sequence of instructions, executing at millions of steps per second, can appear to be very complicated, when taken as a whole. The trick is not to think of it as a whole, but as a series of very simple steps, or commands.
Most microprocessors, or very small computers, (here after referred to simply as micro's) have much the same commands or instructions that they can perform. They vary mostly in the names used to describe each command. In a typical micro, there are commands to move data around, do simple math (add, subtract, multiply, and divide), bring data into the micro from the outside world, and send data out of the micro to the outside world. Sounds too simple....it is.
A typical micro has three basic parts inside. They are the Program Counter, Memory, and Input / Output. The Program Counter keeps track of which command is to be executed. The Memory contains the commands to be executed. The Input / Output handles the transfer of data to and from the outside world (outside the micro's physical package). The micro we'll be using is housed inside a 40 pin package, or chip. There are many other actual parts inside our micro, but too much, too soon, would be too confusing right now. We will, however, learn about each and every single one.
A Simple Program
As stated before, a program is a sequence or series of very simple commands or instructions. A real world example program might be the problem of crossing a busy street.
Step 1: Walk up to the corner of the street and stop.
Step 2: Look at the traffic light.
Step 3: Is the light green for your direction of travel?
Step 4: If the light is red, goto step 2. (otherwise continue to step 5)
Step 5: Look to the left.
Step 6: Are there cars still passing by?
Step 7: If yes, goto step 5. (otherwise continue to step 8).
Step 8: Look to the right.
Step 9: Are there cars still passing by? (there shouldn't be any by now, but, you never know!)
Step 10: If yes, goto step 8. (otherwise continue to step 11)
Step 11: Proceed across the street, carefully!!
Now this may seem childish at first glance, but this is exactly what you do every time you cross a busy street, that has a traffic light (at least, I hope you do). This is also exactly how you would tell a micro to cross the street, if one could. This is what I mean by a sequence or series of very simple steps. Taken as a whole, the steps lead you across a busy intersection, which, if a computer did it, would seem very intelligent. It is intelligence, people are intelligent. A programmer that programmed these steps into a micro, would impart that intelligence to the micro.
The micro would not, however, in this case, know what to do when it got to the other side, since we didn't tell it. A person, on the other hand, could decide what to do next, at a moments notice, without any apparent programming. In the case of a person, though, there has been some programming, it's called past experiences.
Another program might be to fill a glass with water from a faucet.
Step 1: Turn on the water.
Step 2: Put the glass under the faucet.
Step 3: Look at the glass.
Step 4: Is it full?
Step 5: If no, goto step 3.(otherwise, continue to step 6)
Step 6: Remove the glass from under the faucet.
Step 7: Turn off the water.
This is a simpler program, with fewer steps, but it solves a problem, to fill a glass with water. In a micro, the problems are different (they could be the same if you've made some kind of servant robot), but the logical steps to solve the problem are similar, that is, a series of very simple steps, leading to the solution of a larger problem.
Also notice that since the steps are numbered, 1 through 7, that is the order in which they're executed. The Program Counter, in this case, is you, reading each line, starting with 1 and ending with 7, doing what each one says. In a micro, the Program Counter automatically advances to the next step, after doing what the current step says, unless a branch, or jump, is encountered. A branch is an instruction that directs the Program Counter to go to a specific step, other than the next in the sequence. The branch in this example is step 5. Not only is this a branch, but it is a conditional branch. In other words, based on whether the glass is full or not, the branch is taken, or not. A micro has both branch and conditional branch instructions. Without this ability to reuse instructions, in a sort of looping action, a solution would take many more steps, if it would be possible at all.
The point of this lesson is to show how a simple set of instructions can solve a bigger problem. Taken as a whole, the solution could appear to be more complicated than any of the separate steps it took to solve it. Well that wasn't so tough, was it?
The most difficult problem to be solved in programming a micro is to define the problem you are trying to solve. Sounds like some kind of play on words, but I assure you, it's not. This is the Logical Thought Process I mentioned earlier. The mark of a good programmer is one who has this ability. It is, in my humble opinion, a developed skill, coupled with some talent, and a good understanding of the problem you're trying to solve. In most cases, God has endowed you with the talent and the ability to reason, all you need do is develop the skill of Problem Solving.
My home page is http://www.hkrmicrop.com/personal/index.html .
On to lesson 2.