A Micro's Commands (or Instructions).

I would like to take a moment and address a few things that will make it easier for me to describe things to you later. In a micro 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, it is easier than having to say the whole words binary, decimal, or hexadecimal, but still indicate it. By doing this, 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.

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, and bit 7 as the left most or most significant. In micros, a 0 is just as real a number as 1 or 8, and it should not be thought of as "nothing".

Lastly, there are various Registers inside a micro. These vary from micro to micro, but all contain a register called the Accumulator. It is also referred to in some as the a register. I 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 micro.

In the micro we will be using, there are 5 different types of instructions and several variations of each, resulting in 110 different instructions. These 5 types are ARITHMETIC, LOGICAL, BOOLEAN, BRANCHING, and DATA TRANSFER.

ARITHMETIC

The arithmetic instructions include addition, subtraction, division, multiplication, incrementing, and decrementing. There are two flags used with arithmetic that tell the program what the outcome of the instruction was. 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 straight forward 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 accumulator, part of it might be lost. There is a safeguard against this. Take the case of 11111111b (255) and 11111111b (255). These are the largest numbers that can fit into an 8 bit register or memory location.You can add these as decimal numbers, since I gave you their values in decimal also, and you would get 510. The binary value for 510 is 111111110b (9 bits). The accumulator is only 8 bits wide, it is a byte. How do you fit a 9 bit number into 8 bits of space? The answer is, you can't, and its called an OVERFLOW condition. So how do we get around this dilemma? We do it with the CARRY (C) flag. If the result of the addition is greater than 8 bits, the CARRY (C) flag will hold the 9 th bit. In this case the accumulator would have a 11111110b (254) and the C flag would be a 1, or set. This 1 has the value of 256 because this is the 9th bit. We haven't covered a 9 bit number, but they come up all the time as overflows in addition. Since we are using base 2, and we found out in lesson 2 that the 8th bit (bit 7) in a byte is worth 128, then the 9th bit is worth 2 times that, or 256. Adding 254 and 256, we get 510, the answer, and we didn't loose anything, because of the C flag. Had the result of the addition not caused an overflow, the C flag would be 0, or cleared.

Subtraction

In the case of subtraction, the process is more difficult to explain, and as such, I'm not going to cover it here. It involves 1's compliment and 2's compliment representation. But I will tell you this, you can subtract two numbers and if there is an under flow, the C flag will be a 1, otherwise it will be a 0. An under flow is where you subtract a larger number from a smaller number.

Multiplication and Division

In the micro we will be using, are multiply and divide instructions, but I will wait till later to talk about them. They do, however, just what the names suggest.

Two other instructions are included in the arithmetic group. They are increment and decrement. These instructions are used to count events or loops in a program. Each time an increment is executed, the value is incremented by 1. A decrement, decrements the value by 1. These can be used with conditional jumps to loop a section of program, a certain number of times. We will see these used later.

LOGICAL

In micros there are other mathematical instructions called logical instructions. These are OR , AND, XOR, ROTATE, COMPLEMENT and CLEAR. These commands are usually not concerned with the value of the data they work with, but, instead, the value, or state, of each bit in the data.

OR

The OR function can be demonstrate by taking two binary numbers, 1010b and 0110b. When OR'ing two numbers, it doesn't matter at which end you start, right or left. Let's start from the left. In the first bit position there is a 1 in the first number and a 0 in the second number. This would result in a 1. The next bit has a 0 in the first number and a 1 in the second number. The result would be 1. The next bit has a 1 in the first number and a 1 in the second number. The result would be a 1. The last bit has a 0 in the first number and a 0 in the second number, resulting in a 0. So the answer would be 1110b. The rule that gives this answer says that with an OR, a 1 in either number results in a 1, or said another way, any 1 in, gives a 1 out.

AND

AND'ing uses a different rule. The rule here is a 0 in either number will result in a 0 , for each corresponding bit position. Using the same two numbers 1010b and 0110b the result would be 0010b. You can see that every bit position except the third has a zero in one or the other number. Another way of defining an AND is to say that a 1 AND a 1 results in a 1.

XOR (eXclusive OR)

XOR'ing is similar to OR'ing with one exception. An OR can also be called an inclusive OR. This means that a 1 in either number or both will result in a 1. An eXclusive OR says that if either number has a 1 in it, but not both, a 1 will result. A seemingly small difference, but crucial. Using the same two numbers, the result would be 1100b. The first two bits have a 1 in either the first or the second number but not both. The third bit has a 1 in both numbers which results in a 0. The fourth has no 1's at all, so the result is 0. The difference may seem small, even though the OR and XOR result in different answers. The main use of an XOR is to test two numbers against each other. If they are the same, the result will be all 0's, otherwise the answer will have 1's where there are differences.

Compliment

Complimenting a number results in the opposite state of all the 1's and 0's. Take the number 1111b. Complimenting results in 0000b. This is the simplest operator of all and the easiest to understand. It's uses are varied, but necessary, as you'll see later.

Rotate

These instructions rotate bits in a byte. The rotation can be left or right, and is done one bit each instruction. An example might be where the accumulator has a 11000011b in it. If we rotate left, the result will be 10000111b. You can see that bit 7 has now been moved into bit 0 and all the other bits have move 1 bit position in, the left direction.

Clear

This instruction clears, or zero's out the accumulator. This is the same as moving a 0 into the accumulator. This also clears the C flag and sets the Z flag.

BOOLEAN

There are other commands that deal with single bit values. In a program, an 8 bit byte location may be used as 8 separate flags or indicators, defined by the programmer, and used to signal events or conditions between separate parts of a program. This might be a bit that indicates whether a door is open or closed, or whether a key has been pressed or a switch is closed or open. These bits or flags are usually the mechanism that keeps law and order in the program, and insures that the program does not get "lost" or misdirected. We'll see how this works later.

There are instructions to do the same things with bits that are done with bytes by other instructions. You can AND, OR, SET, CLEAR, or MOVE bits. You will see in a later lesson how these are used.

BRANCHING

There are also program flow commands. These are branches or jumps. They have several different names reflecting the way they do the jump or on what condition causes the jump, like an overflow or under flow, or the results being zero or not zero. But all stop the normal sequential execution of the program, and jump to another location, other than the next instruction in sequence.

Jump on Bit

These instructions let you make a jump based on whether a certain bit is set (a 1) or cleared (a 0). This bit can be the CY (carry) flag, the Z (zero) flag, or any other bit.

Compare and jump

These instructions compare two values and jump based on the result. In lesson 1 we looked at a glass filling with water and compared it to a full one in our mind, and if it wasn't full we looked at it some more. This caused that program to loop, or wait, until the glass was full before continuing on.

Call

There is also a variation on a jump that is referred to as a CALL. A CALL does a jump, but then eventually comes back to the place where the CALL instruction was executed and continues with the next instruction after the CALL. This allows the programmer to create little sub-programs, or subroutines, that do repetitive tasks needed by the main program. This saves programming time because once the subroutine is written, it can be used by the main program when ever it needs it, a kind of way to create your own instructions.

DATA TRANSFER

Moving

These instructions do exactly what you would think. They move data around between the various registers and memory.

Exchanging

Exchanging is a variation on the move instruction. Here data is exchanged between two places.

Exchange Digit

This is a variation on the exchange instruction. Here the lower nibble, or digit, is exchanged with the lower nibble of the other location. The upper nibble is the left 4 bits (bits 4 through 7) and the lower nibble is the right 4 bits (bits 0 through 3).

Swapping

This is another variation on the exchange instruction, but here the data exchange occurs between the upper nibble and the lower nibble in the accumulator.

This is the end of lesson 3. I've tried to show all the possible instructions without actually showing each. I will, in a later lesson, go into each of the 110 different instructions and explain each one. In the next lesson we will learn more about memory and all the possible ways to get to a memory location.

My home page is http://www.hkrmicrop.com/personal/index.html .

On to lesson 4.