Creating Viper assemblies in C#

You can write compilers targeted on Viper in C#. I have provided a DLL that will make the processor a thousand times easier. The DLL can be found here ( Viper 4.0 Assembler DLL ).

Hello, World!

The first thing we are going to have to do is add a reference to the DLL you downloaded. Then don't forget to include the following namespaces.
using Viper;
using Viper.Opcodes;

Once we have added those we can begin writing our program. All Viper programs are represented by the Viper.Assembly object. So lets create a new Viper Assembly by creating a new instance of Viper.Assembly.
Assembly asm = new Assembly();
There we go, step one complete. Now we need to make it do something, we can call asm.Emit to add instructions to our program. So lets make it print 'Hello, World' shall we?
asm.Emit(new push_d(1));
asm.Emit(new push_ptr("msg"));
asm.Emit(new sysf(1));
asm.Emit(new ret());
asm.CreateString("msg","Hello, World!");
Now you may be wondering what this does? Well the first instruction will push a double word, or dword on the stack with a value of one. This is the file descripter that tells Viper to write to the standard output stream. The next instruction, push_ptr("msg") pushes a 32 bit pointer that points to a symbol called msg. msg contains the message "Hello, World!". The sysf(1) tells Viper to call an internal print function which will pop a pointer, and a file descriptor from the stack. The final instruction ret tells Viper to end the function. Since there is nothing to return too, it will end execution of the program there.

The final thing to do is save our program. We will call asm.SaveGEX which will save a viper executable that we can run. When running viper do not forget to use the --gex option, or else the program will not run properly.

Last edited May 4, 2013 at 4:49 AM by GruntXProductions, version 5


No comments yet.