Question

I'm writing my own bytecode and virtual machine (on .NET) and one thing i can't figure out is how to embed strings into my bytecode. Any ideas now how i should do it?

Was it helpful?

Solution

Apparently you're defining your very own byte code. this has nothing to do with the syntax/grammar of .NET CIL, right ?

If so, and if you concern is how to encode strings (as opposed to other instructions such as jumps, loops, etc.), you can just invent your own "instruction" for it.

For example, hex code "01xx" could be for a string containing xx bytes (0 -255). Your language interpreter would then be taught to store this string on the stack (or whereever) and move to decode the following byte code located xx bytes further down the bytecode stream.

If you concern is how to mix character data and numeric data in whatever storage you have for the bytecode, please provide specifics and maybe someone can help...

OTHER TIPS

If you can store numbers in an array, then you can store ASCII data in the same array. Ignoring the idea of a string as a class, a simple string is just a character array anyway -- and in C, a byte with a value of 0 indicates the end of the string.

As a simple proof-of-concept in C:

int main()
{
    putchar(104); // h
    putchar(101); // e
    putchar(108); // l
    putchar(108); // l
    putchar(111); // o
    putchar(10);  // \n
    return 0;
}

Output:

$ ./a.out
hello

Maybe a reference on character arrays as strings would help?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top