So now that I have a fancy programmable computer working with its guts bare to the world, what can I do with it?
Well, not much.
The 8 bits of information the computer can shift around necessarily has to be split into 4 bits for the memory address location and 4 bits for the program counter step. This means that there are only 24 = 16 memory address locations for us to store all the commands and variables for our program – not much to work with at all! While all computers are only notionally Turing Complete since formal Turing Completeness requires infinite memory, rarely do you actually bump up against such restrictive limits. Even just doubling this to a 16-bit computer would let you write far more
wasteful complex programs that work with far bigger numbers.
However even with the 8-bit constraint, it is possible with a little creativity to fit programs that are both interesting and useful in 16 lines of memory! I have copied over Ben’s multiplication program, and have also created two original programs: one which generates the Fibonacci Sequence, and one which checks if a number is prime (and spits out its largest factor if it isn’t).
And if you want to go even further, I thought of some ways that you could extend the instruction set to create programs that are effectively 2-3 times as long as is possible with Ben’s original list of instructions. If someone can think up a way to use this information to create a program that is interesting enough to elicit a “wow!” reaction, shoot me a comment or email and I’ll make a video showing it in action on my computer! I’ve listed a few challenges at the end of the post.
0: LDA 14 1: SUB 12 2: JC 6 3: LDA 13 4: OUT 5: HLT 6: STA 14 7: LDA 13 8: ADD 15 9: STA 13 10: JMP 0 11: - 12: 1 13: product 14: x 15: y
Calculates and displays the Fibonacci numbers less than 255: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233].
0: LDI 1 1: STA 14 2: LDI 0 3: STA 15 4: OUT 5: LDA 14 6: ADD 15 7: STA 14 8: OUT 9: LDA 15 10: ADD 14 11: JC 13 # to loop the program, replace with JC 0 12: JMP 3 13: HLT # to loop the program, this line is not necessary 14: x 15: y
Largest Divisor (Primality Test)
This takes a number and calculates and displays the largest number that evenly divides it. If the output is 1, that means the original number was prime! The number x you are investigating has to be entered in both addresses 14 and 15 for the first run, and re-entered in 14 for each repetition.
I’m pretty proud of coming up with this one – it may be the most interesting program that can physically fit on the computer without modifications. Any hey, it means that you could use this computer to help break really, really basic cryptography!
0: LDA 15 1: OUT 2: LDA 14 3: SUB 13 4: STA 14 5: LDA 15 6: SUB 14 7: JZ 10 8: JC 2 9: JMP 6 10: LDA 14 11: OUT 12: HLT 13: 1 14: x # must enter each run 15: x # must enter first run
The computer as designed by Ben only uses 11 of the 16 slots available for commands, which leaves a lot of room to expand the base instruction set. Here is a list of commands that I think could be useful and would work directly with no hardware changes:
- Increment (INC): Add a specified 4-bit number directly to what’s in the A register. Saves from having to use a memory address to store a 1 (or other number) to count up and down by.
- Decrement (DEC): Similar to INC, subtract a specified 4-bit number directly from what’s in the A register.
- Load B / Store B (LDB / STB): Duplicates the load and store functions for the A register onto the B register.
- Display (DSP): Outputs the value at a specified memory address to the screen.
- Display Immediate (DSI): Outputs a specified 4-bit number to the screen. For example, if your program results in a true/false statement, you could spit out a 1 or 0.
The real room for extensibility however comes from using all 8 of the microcode steps, rather than truncating at 5 to save execution time. Since the “meat” of each command takes only 1-3 steps (after the necessary 2 steps of loading the command instructions from memory), it’s possible to join together the meat of any 2 or 3 of the commands into one line. A useful one for example would be a Jump if Equal (JEQ), which would chain together a subtraction with a Jump if Zero.
And then if you really want to dive off the deep end, you could tailor-make your commands for a specific program, and fit in 2-3x the number of instructions you could otherwise. Every single address of program memory could have it’s own custom command – essentially you would be coding directly in microinstructions. I’d be really interested to see how complex a program someone could make with this approach!
Challenges to the reader which may or may not be possible, and will very likely require modifying the basic instruction set. If you figure out how to do any of these in 16 lines of memory, let me know and I’ll make a video of it running on my computer!
- Compute and display powers: ab = c
- 35 = 243
- Sort 3 (or more) numbers in memory from smallest to largest: sort(c, a, b) = [a, b, c]
- sort(3, 2, 1) = 1, 2, 3
- Display the number of 1’s in the binary representation of an input number: countbits(a) = b
- countbits(15) = countbits(00001111) = 4
- Compute the dot product of two vectors: dot(a, b, c, d) = e
- dot(1, 2, 3, 4) = [1, 2] · [3, 4] = 1*3 + 2*4 = 11
- Add together two 16-bit numbers: add16(a, b, c, d) = [e, f]
- add16(7, 208, 11, 184) = [19, 136] → (7*28 + 208) + (11*28 + 184) = 00000111 11010000 + 00001011 10111000 = 2000 + 3000 = 5000 = 00010011 10001000 = (19*28 + 136)
- Something else!