I had never seen this mini-assembler-based educational computer before. wikipedia.org/Little_man_computer.
I couldn’t find a good online emulator, so I wrote one: Little Man Computer Emulator.
Enter the program on the left, click “Assemble”, enter some inputs if your program needs them, and then step through the execution.
It’s probably got some bugs since it was a quick hack, but it worked on the examples I tried it on.
Here’s some example programs.
The last two are the most interesting: LMC doesn’t have indirect addressing – the only load/store instructions are
LDA xx and
STA xx where
xx is a fixed address. To work around that, the code modifies itself, writing the appropriate
And here’s code that sorts input. It reads N, then a sequence of N numbers from the input and sorts them.
This code has a subroutine, which requires the branch instruction of the subroutine to be overwritten with a branch instruction to the correct return address before calling. The code depends on integer addition wrapping modulo 1000 – my implementation of the LMC does this, but this code may need adapting to work on other versions of the machine, as the spec leaves the overflow behavior of addition and subtraction unspecified.