x86 Assembly is My Scripting Language! colorful lead photo

Lots of people talk about the advantages of using supposedly "high level" languages for scripting. Languages like Python and Ruby supposedly handle all the icky details of "type" and "memory management" for us, while exposing "easy-to-use" data structures and exhaustive library functions. But that ignores the real cost of these languages, which is that you have to learn an entire language just to do simple things!

Now, I really don't see why I can't use the syntax and libraries of languages I already know for my scripting. David Bergman apparently doesn't either, because he created a program that mimics a C++ interpreter.

Now, I think this is just about the coolest thing to ever come out of southern California, but it doesn't quite go far enough, because who has time to keep up with all this new fangled C++? No I think the highest level of productivity can only be achieved by scripting directly in assembly, the language we all know best. And cpsh let's me do that, indirectly:

#!/usr/bin/cpsh
int i = 5;
int o = 1;

printf("i=%d\n",i);
printf("o=%d\n",o);

// swap two variables
asm("movl %1, %%ebx;"
    "push %%ebx;"
    "movl %0, %%ebx;"
    "movl %%ebx, %1;"
    "pop %%ebx;"
    "movl %%ebx, %0;"
    : "=r" ( o ) , "=r" (i)  // outputs
    : "0" ( o ), "1" (i)     // inputs (same as outputs)
    : "%ebx"      // not strictly required
);

printf("i=%d\n",i);
printf("o=%d\n",o);

just write a wrapper around your code, use the asm() command to drop into assembly, and have at it. Here's it at work:

$ chmod +x asm.cpsh
$ ./asm.cpsh
i=5
o=1
i=1
o=5

By the way, if you immediately said to yourself, "why doesn't he just write xchg %0, %1;," then this is definitely the scripting language for you.

- Oran Looney May 4th 2007

Thanks for reading. This blog is in "archive" mode and comments and RSS feed are disabled. We appologize for the inconvenience.