home *** CD-ROM | disk | FTP | other *** search
/ The Grim Reaper 13 / Grim_Reaper_The_Issue_13_1994___BASIC.atr / as3.doc next >
Text File  |  2023-02-26  |  8KB  |  1 lines

  1. Assembly Language Tutorial¢¢by Chris Crawford¢¢LESSON THREE - LOGIC¢¢BOOLEAN LOGIC¢¢A great deal of programming involves the use of Boolean logic. This is a standardized system for handling logical manipulations. It's sort of like algebra for logic. You must understand Boolean logic if you are to write assembly language programs, so let's get started.¢ Where algebra deals with numbers, Boolean logic deals with propositions. A proposition is just a statement such as "Fred eats worms." It can take only two possible values - True or False. In our programs we seldom bother with broad and glorious propositions such as "Love is the universal language of truth" or "War is the extension of policy by other means". Instead, we normally deal with propositions such as "The joystick trigger has been pressed," or "There is a diskette in the disk drive."¢ When we use Boolean logic with a computer, we may think in terms of true and false, but the computer is actually working with 1's and 0's. We use the following convention: a 1 corresponds to a Boolean value of "true", while a 0 correspondes to a Boolean "false".¢ Using this system we can represent propositions inside the computer. However, programming requires more than the mere representation of data; we must also be able to manipulate that data. This brings us to the Boolean operators. There are four common Boolean operations necessary for most programming practices:¢*¢   NOT¢¢This is the simplest of Boolean operators. It takes a single Boolean value as an input and produces as its output the logical converse of the input. Thus, a true input yields output, while a false input generates a true input.¢*¢   OR¢¢This Boolean operator takes two Boolean values as its input and generates a single Boolean value as its output. The value of the output depends on the values of the inputs according to the following rule: If one input is true OR the other value is true, then the output is true. Otherwise, the output is false.¢*¢   AND¢¢This Boolean operator is just like the OR-operator, except that it uses a different rule. Its rule is: If one input is true AND the other input is true, then the output is true; otherwise the output is false.¢*¢   Exclusive-OR¢¢This Boolean operator is just like the or-operator, except that its rule is: If one input is true, OR the other input is true, BUT not both are true, then the output is true; otherwise, the output is false.¢*¢When we use the 6502 for Boolean operations, you must remember that the operations are eight bits wide. Instead of working with one bit at a time, we use all eight bits of a word in parallel. The bits in a byte are independent and do not affect each other in any way - at least as far as Boolean operations are concerned.¢ The 6502 has three instructions for performing Boolean operations. These are AND, EOR, and ORA. The first performs an and-operation. For example, consider the following code:¢¢    LDA     FISH¢    AND     GOAT¢¢This will first Load the accumulator with the value of FISH. It will then AND the contents of the accumulator with the contents of GOAT. The result of the and-operation will be left in the accumulator.¢ The AND-instruction can use an immediate operand if you desire, just as the ADC-instruction can.¢ The EOR-instruction provides the exclusive-or operator. It works just like the AND-instruction. The ORA instruction provides the or-operator in just the same way.¢ If you wish to obtain the NOT-operation, just use EOR #$FF; this will invert each bit in the accumulator. Because NOT is so easily reproduced with EOR, there is no special NOT instruction in the 6502.¢*¢APPLICATIONS OF BOOLEAN LOGIC¢¢If you have any sense at all, you are probably asking, "What good is all this Boolean nonsense? What would I use it for?" Four applications are available:¢*¢PROGRAM LOGIC¢¢Many times our programs encounter rather complex logical situations. The program must be able to load a file; if the FMS is in place and there is a diskette in the disk drive, and the diskette has the file we are looking for, or the file specification calls for a cassette load, then we will load the program. Many programming problems involve such Boolean operations, Keeping them straight is certainly a headache.¢*¢MASKING BITS¢¢Sometimes we need to isolate particular bits in a byte. For example, in Eastern Front (1941) I used the character value to store the unit type. The color of the unit was encoded in the upper two bits of the byte, the type in the lower six bits. If I wanted to get only the unit type, I had to mask out the upper two bits. This I did with the following code fragment:¢¢   LDA     UNITCODE¢   AND     #$3F¢¢The AND-instruction eliminated the upper two bits, leaving me with just the unit type. Bit-masking like this is useful in many situations. We use it frequently when we pack bits into a byte to save memory. It is also handy with input handling. If you want to read the joystick port, you frequently mask out the bits in turn to see which is active.¢ By the way, you mask out bits set to 1 with the AND-instruction. You mask out bits set to 0 with the ORA instruction. The logic is reversed.¢*¢SETTING AND CLEARING INDIVIDUAL BITS¢¢We also use the AND and ORA instructions to set or clear individual bits within a byte. This is most often useful for handling arrays of flag bits.¢*¢FOLDING BYTES TOGETHER¢¢This little fragment of code will fold bytes together:¢¢   LDA     FISH¢   EOR     GOAT¢   AND     MASK¢   EOR     GOAT¢   STA     ANSWER¢¢This is a magical piece of code. See if you can figure out what it does. Experiment with two values of MASK: $OF and $FO.¢*¢SHIFT AND ROTATE INSTRUCTIONS¢¢The 6502 also has instructions that allow you to shift the bits around inside a byte. The first of these are the shift instructions. One, ASL, shifts a byte to the left; the other, LSR, shifts a byte to the right. Thus, the byte %01101011, when shifted left, becomes %11010110. Each bit is shifted one position to the left. The leftmost bit is rudely pushed right out of the byte and falls away ("Aaaaaaaaarrrrrggggg!"). A zero is shifted into the rightmost bit. The LSR instruction does the same thing in the opposite direction.¢ Note that ASL also doubles the value of the byte, while LSR halves it. Two ASL's multiply by four; three multiply by eight. This makes it easy to do simple multiplication, but be careful with round-off error here. What happens if you try to multiply by 256? What do you get if you halve 3?¢ A variation on the shift instructions are the rotate instructions. There are two: rotate left (ROL) and rotate right (ROR). These function just like the shift instructions, except that the bit that gets shoved into the bottom is not necessarily a zero; it is the contents of the Carry bit. The bit that gets pushed off the edge of the byte goes into the Carry bit, so it is not lost. Thus, if you rotate either way nine times, you'll be right back where you started.¢ Rotate instructions are a handy way to get a particular bit into the carry bit where you can work on it. Conversely, once you get your desired bit into the carry bit the way you want it, you can put it back into a byte with some rotate instructions.¢*¢INCREMENT AND DECREMENT INSTRUCTIONS¢¢The last instructions I will cover are the increment and decrement instructions. These allow you to add one (increment) or subtract one (decrement) from a memory location. These are not considered to be arithmetic operations so they do not affect the Carry flag, nor are they affected by it.¢ You cannot increment or decrement the accumulator, only RAM location.¢