Compiled by 03IT44 - 1 -

Microprocessor Basics

The majority of people think that computers are some kind of complicated device that is

impossible to learn and infinitely intelligent, able to think better than a person. The truth

is much less glamorous.

A computer can only do what the programmer has told it to do, in the form of a

program. A program is just a sequence of very simple commands that lead the computer

to solve some problem. Once the program is written and debugged (you hardly ever get it

right the first time), the computer can execute the instructions very fast, and always do it

the same, every time, without a mistake.

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.

The microprocessor itself is usually a single integrated circuit (IC). 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....right?

A typical micro has three basic parts inside. They are the Program Counter (PC),

Memory, and Input / Output (I/O). 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 or

IC. There are many other actual parts inside our micro however, we will learn about each

and every single one, one step at a time.

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 traffic lights and stop.

Step 2: Look at the traffic light.

Step 3: Is your light green?

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 tap.

Step 1: Turn on the water.

Step 2: Put the glass under the tap.

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 tap

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 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.

The most difficult problem to be solved in programming a micro is to define the problem

you are trying to solve. Sounds silly but I assure you, it's not. This is the Logical Thought

Process I mentioned earlier. It is having a good understanding of the problem you're

trying to solve. You must understand the information I'm presenting in order pass the

course. Trying to remember everything does not work at university.

Decimal, Binary & Hex

Most people have learned to use the Decimal numbering system for counting and

calculations. But micros use a different system. It's called Binary and that’s why our

computers are called binary computers! All numbering systems follow the same rules.

Decimal is Base 10, Binary is Base 2, and Hex(adecimal) is Base 16. The base of a

system refers to how many possible numbers can be in each digit position. In decimal, a

single digit number is 0 through 9. In binary a single digit number is 0 or 1. In hex a

single digit number is 0 through 9, A,B,C,D,E, and F.

In decimal, as you count up from 0, when you reach 9, you add 1 more, then you have to

add another digit position to the left and carry a 1 into it to get 10 (ten). Ten is a two digit

decimal number.

In binary, as you count up from 0, when you reach 1, you add 1 more, then you have to

add another digit position to the left and carry a 1 into it to get 10B (two decimal). While

this is exactly what you do in decimal, the result looks like ten so we usually write the

letter ‘B’ after to distinguish it from ten decimal. [ We should really denote ten decimal

as 10D but we usually leave off the ‘D’]. So while decimal 10 (ten) looks like binary 10

(two decimal) they represent different decimal values. It is still useful to think in decimal,

since that's what we're used to, but we have to get used to seeing numbers represented in

binary.

In hexadecimal (hex), as you count up from 0, when you reach 9, you then go to A ten

decimal), then B (eleven decimal), etc., until you reach F (15 decimal). When you reach

F, you then add 1 more, then you have to add another digit position to the left and carry a

1 into it to get 10H (sixteen decimal). While this is exactly what you do in decimal, the

result looks like ten or 10B, so we usually write the letter ‘H’ after to distinguish it from

ten decimal and binary 10B. So while decimal 10 (ten) and binary 10B looks like hex

10H, they represent different values. It is still useful to think in decimal, since that's what

we're used to, but we have to get used to seeing numbers represented in binary and hex.

Another small difference between decimal terminology and binary is that in binary a digit

is called a bit. It gets even more confusing by the fact that 4 bits make a nibble. Two

nibbles make a byte. Two bytes make a word. Most numbers used in a micro don't go

beyond this, although there are others. Using what I've just said, if two nibbles make a

byte, you could also say that a byte is eight bits.

To represent a binary number larger than 4 bits, or a nibble, a different numbering system

is normally used. It is called hexadecimal, or Base 16. A shorter name for hexadecimal is

simply hex, and that's what we'll use here after. In this system there are 16 possible

numbers for each digit. For the first 10, 0 through 9, it looks like decimal. Unlike

decimal, when you add 1 more to 9, you get A. I know that having a letter to represent a

number is really confusing, but they had to call it something, and A is what they chose.

So a hex A is a decimal 10 (ten). The numbers count up from A through F. Just to clarify

this here is the sequence of counting in hex from 0 to where you have to add another digit

position to the left... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (no G). This represents a

decimal count of 0 through 15. At a count of F (15 decimal), if you add 1 more you get 10 (oh no! .. not another 10 that means something else!!). Sad but true.

Let's regroup here. A binary 10 (one zero) is decimal 2, a decimal 10 is ten, and a hex 10

is decimal 16. If you can get this concept, you will have conquered the most difficult part

of learning micros.

I need to get past one more obstacle, the idea of significance. In a decimal number like

123, 3 is the least significant digit position (the right most digit) and 1 is the most

significant digit position (the left most digit). Significance means the relative value of one

digit to the next. In the number 123 (one hundred twenty three) , each number in the right

hand most digit position (3) is worth 1. The value of each number in the next most

significant digit position (2) is worth ten and in the most significant digit position (1)

each is worth a hundred. I'm not trying to insult your intelligence here, but rather to point

out the rule behind this. The rule is that no matter what base you're working in, as you

start adding digits to the left, each one is worth the base times (multiplied) the digit to the

right. In the decimal number 123 (base 10), the 2 digit position is worth 10 times the 3

digit position and the 1 digit position is worth 10 times the 2 digit position. Hence the

familiar units, tens, hundreds, and so on. For some reason, for most people, this makes

sense for decimal (base 10) but not for any other base numbering system.

The very same is true for binary. The only difference is the base. Binary is base 2. So in

binary the least significant bit (remember bits?) is worth 1 ( this happens to be the same

for all bases). The next most significant bit is worth 2, the next worth 4, the next worth 8,

and so on. Each is 2 times (base 2) the previous one. So in an 8 bit binary number (or

byte, remember bytes?), starting from the right and moving left, the values for each of the

8 bit positions are 1, 2, 4, 8, 16, 32, 64 , and 128. If you've got this, you have passed a

major milestone, you should go celebrate your passage. If you haven't, I would re-read

the above until you do, and then go celebrate!! ( By the way, if you didn't get this the first

time through, join the crowd. I didn't either!!)

In hex (base 16) the same rule applies. In a 4 digit hex number, starting at the right and

working left, the first digit is worth 1 ( hey that's just like decimal and binary!!), the next

is worth 16 (base times the previous digit), the next is worth 256 (16 X 16), and the most

significant is worth 4096 (256 X 16). One last note, hex is just binary described another

way. A hex digit is a binary nibble ( remember nibbles?). Both are 4 bit binary values.

Trying to wrap all this confusion up in review, 4 bits is a nibble or a hex digit. Two hex

digits is a byte or 8 bit binary. A 4 digit hex number is a word, or 16 bit binary or 4

nibbles, or 2 bytes. You may have to review the previous paragraphs a few times (I did!!)

to get all the relationships down pat, but it is crucial that you are comfortable with all I've

said, so that what follows will make more sense.

Let's take a few example numbers and find the decimal equivalent of each. Let's start

with the binary number 1011, a nibble. Starting at the right and moving left, the first digit

is worth one, because there is a 1 there. The next is worth two, because there is a 1 in it.

The next would be worth 4, but since there is a 0, it's worth zero. The last or most

significant is worth eight, since there is a 1 in it. Add all these up and you get eleven. So

a binary 1011 is decimal 11. Also, since this could be a hex digit, the hex value would be

B.

Let's take a longer binary number 10100101. Starting at the right moving left, the first is

worth 1, the next is worth 0, the next is worth 4, the next is 0, the next is 0, the next is

worth 32, the next is 0, and the last is 128. Adding all these up you get decimal 165.

Dividing this number up into two hex digits, you get A5. So binary 10100101, decimal

165, and hex A5 are all the same value. Using hex, the rightmost digit is worth 5, and the

most significant digit is worth 160 (10 X 16), resulting in decimal 165. If you understand

this, you’re ready to move on, leaving the different systems behind. If you don't, keep

reviewing and studying until you do. If you don't understand and still continue, you will

be confused by what follows. I promise that once you get this, the rest is easier.

This ends the second lesson. I hope this wasn't too daunting or difficult, but there was a lot to get through. The rest of the course should be a little easier. Learning a new numbering system is like learning a new language. It's a little cumbersome at

first, but it gets easier.

Microprocessor Instructions

As mentioned earlier, we refer to a binary number like 1111 as 1111b, a decimal number

like 123 as 123, and a hex number like A5 as A5h. So don't be confused by the letters

following numbers, we use both caps and lowercase to denote binary, decimal, or

hexadecimal so there is no doubt what base a multidigit, or multibit, number is in.

Also there is another kind of memory, called flags. Flags are single bit numbers used to

indicate different conditions. They are called flags because they flag the program of

events or conditions. If a flag is raised, or has a 1 in it, it is said to be SET. If it is a 0, it is

said to be CLEARED or RESET.

One other thing, in an 8-bit byte, the 8 bits are referred to as bits 0 through 7, with bit 0

being the right most, or least significant (lsb), and bit 7 as the left most or most

significant (msb).

Lastly, there are various Registers inside a microprocessor or Central Processing Unit

(CPU). These vary from CPU to CPU, but all contain a register called the Accumulator. It

is also referred to in some as the A register. We will be using the accumulator in the

following discussion. It is a type of memory for storing temporary results and is 8 bits

wide, or a byte, as are most places that data can be put inside the CPU.

In the CPU we will be using, there are 5 different types of instructions and several

variations of each, resulting in over 100 different instructions. These 4 types are

ARITHMETIC, LOGICAL, BRANCHING, and DATA TRANSFER.

ARITHMETIC

The arithmetic instructions usually include addition, subtraction, division, multiplication,

incrementing, and decrementing although division & multiplication were not available in

most early CPU’s. There are two flags used with arithmetic that tell the program what

was the outcome of an instruction. One is the Carry (C) flag. The other is the Zero (Z)

flag. The C flag will be explained in the following example of addition. The Z flag, if set,

says that the result of the instruction left a value of 0 in the accumulator. We will see the

Z flag used in a later lesson.

Addition

This is straightforward and is simply to add two numbers together and get the result.

However there is one more thing. If, in the addition, the result was too big to fit into the