An Assembly Machine (in a weekend)
This weekend, I started a new project: a basic assembly machine. It has the following constraints:
- eight registers, each storing eight bits
- two hundred fifty-six memory addresses, each storing eight bits
- four instructions, each occupying two bits
I'm not as obsessed as I once was with Turing-completeness, but I did want to ensure the machine could be mostly Turing-complete. It does suffer the halting problem, somewhat: it stops when it has executed all the commands in the loaded program, or it has its program counter set to a number greater than the number of instructions loaded.
Right now, there is no compiler or interpreter, and all programs are loaded as binary. I wanted to simulate an old punch card machine, where cards would be inserted into the machine, which would then be written to memory, then executed. Program end state is dictated by the state of registers and memory spaces, which are dumped after program execution ends.
Perhaps the hardest problem to solve in this was the branch instruction. In this case, the branch instruction sets the program counter to the value at the address after the comparison instruction, or skips that address if the comparison yields an equality. This is a valid approach to longer instructions known as direct addressing, but I'd be lying if I didn't feel like it was cheating some.
And that leads me to halting. I've yet to implement a HALT instruction, and without an additional instruction, I don't know that I can, save to have an BNE REG1 REG0 in one slot of memory, and 255 in the next, pushing the program to the end of the memory block, but this also means that the program has to yield the last memory block with a BNE REG1 REG1 so the PC will increment out of range.
Another thing that was interesting to think about this was how manual the heap had to be--some addresses had to be written as data, which, without clever hacks, meant they could not also be used as an instruction, leading to lots of BNE instructions to prevent them from being executed. It gives me lots of respect for the hacking that happened in early computers and video games.
Now that I have a linear instruction machine, I'd like to test my ability to implement subroutines, functions, and the like, and to do meta-programming and rewrite the program as it runs.
- Previous: Change Cadence
- Next: Twenty Six Weeks of Code