Hacker News new | ask | show | jobs
by ls65536 2182 days ago
This is neat! I had some thoughts recently about creating something similar as well.

For a simple but still practical instruction set, you can probably sanely get away with about two dozen or so different instructions, but at the expense of having larger programs (by number of instructions) to accomplish the same tasks compared to a more featureful instruction set.

Here's a fairly simple set that might be a reasonable place to start...

ALU: and, or, xor, complement, shift left/right, rotate left/right, add, subtract, multiply, divide, compare (or just use subtract and set flags for this one)

Memory: move (copy between registers), load and store (between memory and a register), maybe also push and pop for sanity if you want to provide explicit stack instructions.

Control flow: jump, branch (maybe depending on flags from ALU), call/return

It might also be helpful to look at the 8-bit AVR instruction set (quite popular for small microcontrollers, including what you might find on many Arduino boards). It contains about a hundred or so instructions, with all of the above list present in one form or another (although many are just aliases for some other instruction with the same opcode but otherwise fixed parameters). In the case of AVR, where code size is often the limiting constraint, having a larger vocabulary of instructions is useful for expressing a program in the smallest amount of space possible, but it certainly isn't necessary to have all of them just for completeness.

2 comments

Hey, thanks for the recommendations! I'll definitely look into the AVR instruction set. I'm not opposed to multibyte instructions but I'm trying for 8-bit instructions. I'm actually planning to use a special register that holds the ALU input so that I have more space for instructions in the actual instruction. It'll take more instructions since you'll have to set the ALU each time you need it changed, but as it's not a physical microcontroller, I'm not worried about running out of room. Do you think this is a feasible plan?
It's certainly possible, but remember that with 8 bits you have just 256 different possible representations, and every instruction opcode with its set of all distinct valid parameters will map to a number of those limited 256 states. For example, this makes it a big challenge to encode anything other than very small ranges of immediate values inside an instruction.

Typically whenever there's a reference to an N-bit instruction set, it's referring to the widths of the registers and data paths. Very often this is not the same as the instruction size, and in many cases instructions may even be of variable length.

So while it's much more restricting to commit yourself to only an 8-bit instruction size, it should still be possible to build something with it.

You might be interested in the LC-3, which is 15 instructions. I just recently wrote an assembler for it in Python https://github.com/pepaslabs/lc3as.py