Original A Bit Mask Resulting A Bit 0 0 0 1 0 1 0 1 1 1 1 1
C64 Assembly language Bytes
In the book Machine Language for the Commodore 64 and other Commodore computers, Jim Butterfield uses an example of turning on numbers in a byte, such as 4, 5, and 6 in the value of $C7. A byte can be any number between 0-255. Each byte is made up of 8 bits (binary number as a whole).
Example (as listed):
Original value: 11000111 Mask EOR 01110000
EOR is used to invert bits in a byte (flips them over).
Example (as listed):
Original value: 11 Mask EOR 01110000
A good example of an using AND in a C64 Assembly Language Bit Manipulation example is turning sprites on or off. The example below turns on sprites 0 & 2
LDA $d015 ; Examine sprite memory (enable byte)
and #%00000101 ; Turn on sprite 0 and 2
sta $d015 ; Save the sprite data
So bits zero and two are active. The bits start at the right side (at “zero”) and move to the left side. So the last bit would be bit 8 (currently “zero”) also.
For this example, the AND command will turn off all sprites that are active. Perhaps this is not the best example, but for the time allotted it will have to do for now. In the future, I may change this to mask with the ORA command instead.
Also when you turn off other bits (sprites, etc), all of the other bits are unaffected. That is where you could utilize an ORA instruction to restore specific bits without impacting the others as well.
C64 Assembly Language Bits
You could also just look at the individual bits and see which sprite is active. According to our example, that would pan out as follows. When you see a “1” in that position, that indicates that the sprite has been turned on and can be seen on the screen once it is positioned at its’ horizontal and vertical position.
The black image below indicates that no sprite was found there, therefore setting it to bit “0”.
An example below from the Parkour game (accessible through the Paid Membership) demonstrates how to walk a sprite across the street by flipping the bits after reaching the 256th position. On the Commodore 64, a sprite’s X position ranges from 0-255. Anything beyond this requires writing to the highest bit. The program below demonstrates this. The BIT_TABLE can handle either left or right range limits (according to the value in memory register $d010).
lda BIT_TABLE, x
eor SPRITE_POS_X_EXTEND ; Sprite X value saved here
sta SPRITE_POS_X_EXTEND ; Sprite X value saved here
sta VIC_SPRITE_X_EXTEND ; Sprite’s X MSB position
BIT_TABLE byte 1,2,4,8,16,32,64,128
SPRITE_POS_X_EXTEND byte 0
VIC_SPRITE_X_EXTEND = $d010
Now listed below is a the number and binary bit values found based on the values that exist in the BIT_TABLE variab le. It is necessary to examine the bits (not the byte) to actually understand what is going on mathematically in memory. Here you can examine that setting one of these bits to one adds 256 to the horizontal position of the sprite (stored within SPRITE_POS_X_EXTEND). Notice that each increase in the number table increases by the power of two.
Steve has always had a passion for computers even before he owned one. His first personal computer was an Atari 65xe purchased at Children's Palace around 1986. In later years he attended DeVry University and received a Computer Science degree and worked as a Web Developer for a short season. To this day he currently works for WordPress Live and runs a YouTube channel for the Commodore 64.