To test the existing functionality of LilyVM and its assembler, I
decided to implement a simple, well-known cryptography algorithm,
RC4. It's not the
kind that anyone should use today, because it's got a lot of known
effective attacks against it, but it's an easy real-world example of
a simple algorithm to play with.
I also like to use it as a random number generator.
Lessons learned tonight:
Some more addressing modes to handle things like de-reference
pointers without separate load/store instructions would make
things MUCH more concise. I think I can add more addressing modes
without any overhead.
I thought about adding a "swap" instruction because it's done
twice in this example. Still not sure about this one.
I need better debugging tools! (You'd think inspecting the state
of your own VM would be easy.)
The code is getting a bit messy and needs a cleanup pass.
I really really really need a way to define arbitrary data. In
this example I used instructions in place of data just so I had
data to work with on the algorithm. Something to just occupy a
block of some number of Words, literal numbers, and possibly
literal strings would go a long way here.
Emacs's asm-mode is barely suitable for this. Maybe I just need to
get used to it.
I ended up using the stack as though it was just a big series of
registers. I guess it's how local variables really would be used
normally, so maybe it's not so bad.
Being able to create labels that map to arbitrary values instead
of just memory locations could have replaced a lot of the
arbitrarily-numbered stack positions.
I lack sufficient error handling. I want to avoid excessive error
checking, and I want to run in places where exceptions are
disabled. (I'm looking at you, Unreal 4.) I might have to resort
to the evil black magic that is setjmp()/longjmp().
I have need function calls. I have no kind of calling convention
or anything. There was only a little bit of duplicated code here,
the RC4 implementation I made a long time ago that I tested it
The actual code follows below. Excuse the lack of syntax
highlighting. I guess I need to make the syntax a little more like
some common assembler syntax to use an off-the-shelf syntax
This code probably will not work for much longer because the
assembler and VM are both works in progress. Not that it matters,
because I still haven't actually put the source code out anywhere
yet. This is mostly a message to any future users (and myself) that
this code is not a good example to use.
Final disclaimer: There may be lots of bugs in this implementation.
It'll probably take a while before I get all the fixes and changes
done I mentioned here. So I'm not sure when the next time I'll post
is going to be.