Working With Bits
Change one bit without disturbing the rest. Meet ora, and and eor — set, clear and flip individual bits — the everyday way to throw a single switch in a byte.
Back in The Machine Can Hear You you tested a single bit by masking it. Often you want to change one — set a flag, clear a flag, flip a flag — without touching the seven bits around it. Three instructions do exactly that, one bit at a time:
ora n— OR with A: set every bit that's 1 inn, leave the rest alone.and n— keep every bit that's 1 inn, clear the rest.eor n— Exclusive-OR: flip every bit that's 1 inn. (On the 6510, XOR is spelteor.)
A byte is often not one number but eight little switches — the joystick port from Unit 8 was exactly that, five direction bits in one byte. These three instructions are how you throw one switch and leave the others where they were.
What you'll see by the end
A cyan border — colour 3. We started with colour 2 (red), ora'd in bit 0, and got 3. In bit terms it's "set the bottom bit and leave the rest"; in colour terms, red became cyan. One switch thrown.
Setting a bit
; Meet the Machine - Unit 13: Working With Bits
; Assemble with: acme -f cbm -o bits.prg bits.asm
*= $0801
!byte $0c,$08,$0a,$00,$9e,$32,$30,$36,$31,$00,$00,$00
*= $080d
lda #%00000010 ; colour 2 — red
ora #%00000001 ; SET bit 0, leave the rest alone
sta $d020 ; %00000011 = 3 = cyan
loop jmp loop
%00000010 is colour 2. ora #%00000001 sets bit 0 without disturbing bit 1, giving %00000011 = 3. The power of ora is that it's surgical: it only ever turns bits on, so whatever you already had survives.
Assemble and run
acme -f cbm -o bits.prg bits.asm
Red, with one bit set, becomes cyan.
Try this: clear a bit with and
Start from lda #%00000111 (colour 7, yellow) and and #%00000110. The and keeps only the bits that are 1 in the mask — so bit 0 is cleared and you're left with %00000110 = 6 (blue). and is the surgical off switch, the mirror of ora.
Try this: flip a bit with eor
Take any value and eor #%00000111. Each of the bottom three bits flips — on becomes off, off becomes on. Run it twice on the same value and you're back where you started: flipping twice undoes itself. (That reversibility is exactly why eor is the heart of sprite drawing — a topic for later.)
If it doesn't work
ora/and/eorwon't assemble. They take an immediate mask:ora #%00000001. The#matters — without it you'd OR with the contents of an address, not the bits you wrote.- The colour didn't change as expected. Work the mask bit by bit against the starting value:
oraturns bits on,andkeeps the masked ones,eorflips. Sketch the eight bits if you have to. - You set bits above bit 3 and the border looks unchanged. The border only shows the low four bits; higher bits are still set (check the register view) — they just don't colour the border.
What you've learnt
ora sets bits, and clears them, eor flips them — so you can change one bit of a byte and leave the other seven untouched.
What's next
A byte tops out at 255 — too small for a screen address, which runs to 65535. Next — Bigger Than a Byte — we hold a number in two bytes and add it by hand, and the carry flag from Unit 12 turns out to be the bridge that makes it work.