Building a Basic Assembly Machine
Over a weekend last month, I built a basic assembly machine. It includes the following architecture:
- 8 8 bit registers
- 256 8 bit memory addresses
- 4 opcodes
As of right now, I haven't implemented virtual hardware to implement the architecture, instead opting for a glorified match statement.
The structure of an instruction is as follows: aabbbccc
- aa: opcode. With 4 operations, two bits are needed to designate the different operations
- bbb: a primary register. If an operation writes, it writes to this register
- ccc: a secondary register for operations. Holds values needed for the operation
The four operations are:
- Add: add the values from the primary and secondary register, and write to the primary register
- Load: register b holds a memory address: write the value at the memory address into register a
- Store: register b holds a memory address: write the value of register a into memory at the address. This is the inverse of load
- Branch if not equal: compare the values at register a and register b; if they are equal, skip the next instruction in memory. If they are not equal, set the program counter to the value in the next memory address
Because of the resource constrained nature of the machine, I've not set any read only registers that hold values such as for true, false, zero, or one, which means that if these values are needed, they must be set in memory and then loaded into the register.
I like this exercise because the memory in this case being used to represent all the values and the program instructions makes the relationship between the stack and heap very apparent; with only so many instructions and memory, and no 'set' values (although one could suppose that, since all registers are started at 0, that that could be used), there has to be a balance in the memory loaded of both instructions and data, and some ability to play between them.
I wrote this mostly to play with a small instruction set computer that might be fun to write toy programs in. So far, I've been able to create a program that counts to six.
- Previous: Naive Classification