BIT Manipulation
Assembler coding - Lesson 7
In this lesson we will learn how to turn BIT's ON or OFF. Let me describe what BIT's are.
The C64 is an 8 BIT machine. 8 BITS = 255 Decimal or FF Hexadecimal. How ?
All computers operate in BINARY, i.e. it works on 1's and 0's (1=ON 1=OFF). A BIT is one of these 1's or 0's and thus for the C64 there are 8 of them.
For example:
255 Decimal is the highest number we can have for 8 bits, why ?
Here's the 8 BIT's
BIT 7 6 5 4 3 2 1 0
Decimal 128 64 32 16 8 4 2 1
As we can see each BIT has a value. Bit 0 is the Least Significant Bit (LSB) and Bit 7 is the Most Significant Bit (MSB). Each bit has a decimal value. If all bits were set to 1, i.e. 1111 1111 then we would add:
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
The answer = 255
So anyway I hope you see now what I mean by BIT's.
The point is, we need to manipulate these bit's sometimes. So what do I mean here ?
Lets assume we want to blank the screen to the border colour. If you look in the programmers reference guide you will see that location 53265 BIT 4 allows us to do this. However the other bits are EXTREMELY IMPORTANT. If we change the value that these are currently we will probably crash the computer which is not good.
So we manipulate the BIT CONTENTS using LOGIC instructions.
- AND - Logical AND
- ORA - Logical OR
- EOR - Logical Exclusive OR
Let me explain these to you one at a time.
LOGICAL AND (AND)
Here's the truth table for AND
A | B | Result |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
You can see that we only get a result of 1 if both the inputs A & B are 1.
Let me write some code to explain
10 LDA #00
20 AND #00
30 STA ...
Line 10 (The Accumulator represents the input A)
Line 20 (The AND instruction represents the input B)
Line 30 (The Accumulator now holds the RESULT) - And as both inputs were zero the result = 0
Now assume we did
10 LDA #01
20 AND #01
30 STA ...
Here the result in the Accumulator = 1 as both inputs were = 1
Now assume we did
10 LDA #55
20 AND #88
30 STA ...
Line 10 in binary = 0011 0111
Line 20 in binary = 0101 1000
Now AND these = 0001 0000
Result = 0001 0000 (16 Decimal)
You see, we only get a 1 for the bit when BOTH inputs are = 1
LOGICAL OR (ORA)
Here's the truth table for OR
A | B | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
You can see that we get a result of 1 either or both of the inputs A & B are 1.
10 LDA #00
20 ORA #01
30 STA ...
Result = 1
10 LDA #00
20 ORA #00
Result = 0
10 LDA #55
20 ORA #88
30 STA ...
Line 10 in binary = 0011 0111
Line 20 in binary = 0101 1000
Now ORA these = 0111 1111
Result = 0111 1111 (127 Decimal)
LOGICAL Exclusive OR (EOR)
Here's the truth table for EOR
A | B | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Here we only get a 1 for the result if either inputs = 1, but NOT BOTH.
10 LDA #00
20 EOR #01
30 STA ...
Result = 1
10 LDA #01
20 EOR #01
Result = 0
10 LDA #55
20 EOR #88
30 STA ...
Line 10 in binary = 0011 0111
Line 20 in binary = 0101 1000
Now EOR these = 0110 1111
Result = 0110 1111 (111 Decimal)
So whats your point i hear you say ?
Lets go back to the problem - Screen to border colour BIT 4 of 53265. If you did this:
10 LDA #16
20 STA 53265
Yes, you would turn on BIT 4 to make the screen = border colour, but you also just set every other BIT = 0. This is very bad if these were not meant to be = 0.
So we use some LOGIC instructions to get around this and keep the other bits exactly the same but also changing bit 4 to equal 1 (Just assume all BITS in 532565 are 0):
10 LDA 53265
20 AND #255
30 ORA #16
40 STA 53265
Ok, firstly we get the value from 53265, we then AND this value with 255. The result of the AND is OR'ed with 16 to give the final result which is stored back at location 53265.
10 - Value 0000 0000 (We said assume that all bits in 53265 = 0)
20 - AND 1111 1111
The Result = 0000 0000
30 - ORA 0001 0000
The Result = 0001 0000
40 Store this back at 53265
As you can see bit 4 is a 1, but all the others are kept at 0.
OK Here's a very useful tip for you. To invert a BIT pattern i.e. 0101 1010 inverted to 1010 0101 , just simply Exclusive OR with 255.
Example
10 LDA #36
20 EOR #255
30 STA ...
Line 10 in binary = 0010 0100
Line 20 in binary = 1111 1111
Now EOR these = 1101 1011
Result = 1101 1011 the inverse of 0010 0100
Well this was rather a long lesson, but i hope you can see how and why we need to play with bits. In LESSON 8 we will be using these techniques when we do some smooth scrolling of some text.