You are on page 1of 7

Conundrums, Puzzles, and Posers

The Problem: Generating the Even-Parity Bit for a Byte


Parity bits provide the simplest form of error-checking codes. There are two types of parity: even
parity and odd parity. In the case of even parity, an extra parity bit is set to 1 if the number of
ones in a given set of bits is odd, thereby resulting in the total number of ones being even.

OK, here's the problem, assume that the DIY Calculator's 8-bit accumulator is loaded with some
unsigned binary value between %00000000 and %11111111 (where the '%' characters indicate
binary values). We want the least-significant bit of the accumulator to end up containing the
even parity bit associated with this byte. For example, if the accumulator starts off containing a
value of %00110111 (an odd number of 1s), then we want it to end up containing %00000001;
but if it started off containing say %00110110 (an even number of 1s), then we want it to end up
containing %00000000.

Your mission is to create two programs in the DIY Calculator's assembly language that perform
this task: (a) using the least number of instructions and (b) using the smallest amount of
memory.
Getting Started
This section is intended to set some ground rules to ensure that we're all playing on a level field.
If you havent already done so, download the latest version of the DIY Calculator from the
"Downloads" page on the www.DIYCalculator.com website (this page also provides installation
instructions). Following installation, double-click the DIY Calculator icon on your desktop:


Next, use the Tools > Workbench #1 command to access the DIY Calculator's simple
workbench interface containing switches, LEDs, and other displays (this is much more
appropriate for this type of problem than the main interface):



The two banks of 8-bit switches are connected to two of the DIY Calculators input ports;
meanwhile, the 8-bit LED display and the three different flavors of 7-segment displays are
connected to four of the DIY Calculators output ports as follows:

Port Type Port Address I/O Device
Input $F000 8-bit Switch Bank #1
Input $F001 8-bit Switch Bank #2
Output $F020 8-Bit LED Display
Output $F021 Single Un-decoded 7-Segment Display
Output $F022 Single Decoded 7-Segment Display
Output $F023 Dual Decoded 7-Segment Display

Drag this workbench away from the main calculator interface to an open area on your screen.
Now try clicking some of the switches on one or both of the 8-bit switch banks: nothing happens
(apart from the switches themselves flipping up and down), because (a) we havent powered our
virtual computer up yet and (b) we dont have a program to run even if the virtual computer was
powered up.


Creating a Skeleton Program
Our first task is to create a skeleton program. We will be able to use this program as the basis
for a variety of puzzles and posers (if you've already created this program, skip to the next
section). Use the Tools > Assembler command to launch the DIY Calculator's assembler:



Enter the following program into the assembler-editor:


######################################################################
# #
# Skeleton program #1 for puzzles and posers #
# #
######################################################################

WB_SWTA: .EQU $F000 # Input port for top bank of switches
WB_LEDS: .EQU $F020 # Output port for eight LEDs

.ORG $4000 # Set origin to first byte in RAM

MAINLOOP: LDA [WB_SWTA] # Load accumulator from switches

### Your code goes here

STA [WB_LEDS] # Copy accumulator to LEDS
JMP [MAINLOOP] # Jump back and do it all again

.END # That's all folks


As you can see, this is a very simple program whose sole purpose in life is to loop around
reading a value from the upper bank of switches and copying this value to the bank of LEDs.
Use the assembler's File > Save As command to save this program as puzzle-skeleton-1.asm
in the DIY Calculator's \Work folder, then use the assembler's File > Assemble command to
ensure that you dont have any finger-slip errors in your code.
Creating your Own Program
Use the assembler to open the puzzle-skeleton-1.asm file described in the previous section; use
the File > Save As command to rename this program to puzzle-even-parity.asm; and then use
the assembler's File > Assemble command to convert this program into a corresponding
machine code file called puzzle-even-parity.ram.

As you can see from the previous page, our skeleton program comprises only three instructions:
the LDA ("load accumulator"), STA ("store accumulator"), and JMP ("unconditional jump"). But
how many bytes of memory does it consume? Of course, we can calculate this by hand for such
a simple program, but why make life hard on ourselves? The point is that, as soon as you've
assembled a program, you can use the assembler's Window > View List File command to see
how much memory it consumes. If you do this now, you'll discover that our skeleton program
occupies nine bytes of memory.

Before we do anything else, click the On/Off button on the calculator's front panel to power-up
the DIY Calculator, and then use the Memory > Load RAM command to locate your puzzle-
even-parity.ram file and load it into the DIY Calculator's memory.

Now, click the Run button on the calculator's front panel to set this program running. Click some
of the switches in the upper bank and observe that not surprisingly the corresponding LEDs
light up to reflect which switches are Up / Active / Logic 1 as illustrated below:



Click the Reset button to terminate the program. Your task is to modify this program to generate
the even parity bit corresponding to the number of switches in their Up (logic 1) position and to
display this bit on the single decoded 7-segment display as illustrated below:


Remember that your mission is to create two programs in the DIY Calculator's assembly
language that perform this task:
a) Using the least number of instructions.
b) Using the smallest amount of memory.

Some possible solutions are described on the following pages can you come up with
something better? If you do come up with an alternative technique (smaller, faster, better, novel,
interesting, or outrageously silly) please send it to max@diycalculator.com and I'll add it in to
this paper (crediting you, of course).



Note: Dont look at the following solutions until you've tried working this out for yourself,
otherwise you'll spoil the fun!










Solution #1: Least Number of Instructions (6)

Solution #2: Smallest Amount of Memory (39 bytes)

You might also like