This simulated CPU has a single register, "current" and supports a very small number of instructions. Since it is so simple, it provides the ability to experiment.
There is no binary format to the instructions; only a simplified text format. Memory is available: 1024 32-bit int slots.
Here's a python implementation via Google Colab.
This is what works with the current system:
# the input
load 5
# save it to locations 0 (fact) and 1 (n)
store @1
store @0
# n --
load @1
sub 1
store @1
# if zero, go to done
goto-rel 3 ifz
# fact *= n;
mul @0
store @0
# go back to n--
goto-abs 3
# load the computed factorial
load @0
Consider a nicer text version of this program: this is what we would like to work.
# take a value as input
prompt
store n
store fact
loop:
# n -= 1
load n
sub 1
store n
# if (n == 0) finished!
goto done ifz
# fact *= n
mul fact
store fact
goto loop
done:
# load the computed value to the "current" register
load fact
print
- Create the "prompt" instruction.
- Create an Opcode for Prompt.
- Support converting the "prompt" to an Opcode.
- Implement it in the CPU-switch statement.
- Add a
ScanneraroundSystem.inas a field toCPUSim, callnextInt? - Read a number from the user, after some kind of generic prompt.
- Add a
- Create a "print" instruction, following similar steps.
- Construct a class
ASMLinesimilar to Instruction that has its own parse method (arguments can now be strings!).
- Loop through this program, collecting all unique variables mentioned.
- Assign these variables numeric identifiers.
- Output raw assembly.
- Convert your
ASMLinelabels to not be part of the instruction sequence, but owned by whatever instruction follows them. - You'll need a way to mark labels on your
ASMLineclass. - Convert the higher-level "goto" statement to either "goto-rel" or "goto-abs" lower-level commands.
- Support directly converting
ASMLineprograms toInstructionprograms so that they can be run directly.