a2sm
What you need to know
This script does nothing more than text replacement. As such, it
does not produce any sort of optimized code, and hand-written code is
better. However, it's still a useful tool. It's a disgusting mess
QUALITY SOLUTION of
regular expressions, and here it is:
a2sm.sh
(Last updated Jan 25, 5:12pm PST)
Usage
First, you'll need execute permissions on the script itself. cd
to the directory with the script, and run
chmod +x a2sm.sh
C code is not directly handled by a2sm, but instead it reads the
x86 assembly (intel syntax) that gcc generates from C files. In order to
get this assembly, you run the following command:
gcc -S -masm=intel my-code.c
Which will produce a file named my-code.s in the same directory. Now
you're ready to translate it!
The script takes the filename to read from as the only argument, and
writes the translated assembly to stdout. So to capture it into a
file of its own, simply redirect the output like so:
./a2sm.sh my-code.s > sm213-out.s
And if you're lucky, all will have gone well. Due to the fact that a2sm is
entirely pattern matching, most things will cause it to produce incorrect
assembly without any warning. Check out the next section for what to
not do.
What to Avoid...
- Don't use anything in C that doesn't translate right into SM213
assembly! This is the general rule.
- At the moment, do not use local variables as I have not
bothered to see if I can transform them out to global status. This will
take more than simple text replacement I think.
- The only supported datatypes are ints and
longs and pointers to them.
- The only supported arithmetic so far is: addition, bitwise
and, bitwise not, bitwise or, shift left/right. I'll soon simulate
subtraction. No xor, multiplication, division, or modulo is available.
- Do not use any functions besides int main(). Not
only does a2sm mark the first function defined as the entry point, but
we don't have any call or jmp instructions in SM213
anyways (yet).
- Conditional statements are not supported because they require
things like cmp/test and jmp (and its related
instructions) which we do not have in SM213 yet.
- Bit-shifting by a variable number of bits (ie x = x << y;)
is not supported because we can only shift in SM213 by a constant
number of bits. I could get around this later when we have looping
capabilities, but not yet.
Newest Changes
- I've posted an example C file and the generated SM213 code. You can
look at it here
- You can now assign to global variables as you're declaring them.
- Using variable indices (ie. b[i] = 3) is now supported.
The Future?
- I don't think local variables will ever be supported due to how they
are stack-allocated by GCC, and referenced by offsets from
ebp...
- We have begun to discuss jumping and branching (/relative jumps). As
this is a slightly different model than x86 uses (everything is a
jmp of some conditional variant thereof, and there are special
registers assigned for comparisons), I hope to be able to successfully
port this across too. It's kinda crucial.