News for hackers. Stuff that matters.
Re:Show HN: A small virtual machine written in C by axman6 on Monday August 04, 2014 @02:13AM (#8130577)
Seems like [1] could benefit from use of the X macro [2], should make adding new instructions much easier and you can avoid the hassle of having to keep two separate tables in sync. There's probably quite a few places where the code could be made clearer by using it. Also in the implementation of your functions, there's a hell of a lot of repetition in all the binary operations, another macro which you pass in the operation and the function name would make life earier:

    #define binop(NAME, OP) definstr (NAME) { \
        long long b, a; \
        if (carp_stack_pop(&m->stack, &b) == -1)\
            carp_vm_err(m, CARP_STACK_EMPTY);\
        if (carp_stack_pop(&m->stack, &a) == -1)\
            carp_vm_err(m, CARP_STACK_EMPTY);\
        carp_stack_push(&m->stack, a OP b);}
    
    binop(ADD,+)
    binop(MUL,*)
    ...
The repetition of

    if (carp_stack_pop(&m->stack, &a) == -1)
        carp_vm_err(m, CARP_STACK_EMPTY);
seems like a good place to just use a function to encapsulate all the error checking and handling.

[1] https://github.com/tekknolagi/carp/blob/master/src/carp_inst...

[2] http://www.embedded.com/design/programming-languages-and-too... and the following parts

Re:Show HN: A small virtual machine written in C by spatulon on Monday August 04, 2014 @06:05AM (#8130995)
This is a technique I first came across in the code for lcc, as described in David R. Hanson's book "A Retargetable C Compiler: Design and Implementation".

https://sites.google.com/site/lccretargetablecompiler/

The code in that book feels pretty dated in a lot of places, but this was a neat trick. Every piece of information related to a token is defined in one place:

https://github.com/drh/lcc/blob/master/src/token.h

Then here's how it's used:

https://github.com/drh/lcc/blob/master/src/expr.c#L8

https://github.com/drh/lcc/blob/master/src/output.c#L107

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:16AM (#8130585)
Definitely interesting — I shall take a look! Feel free to make a pull request if you're game.
Re:Show HN: A small virtual machine written in C by axman6 on Monday August 04, 2014 @02:25AM (#8130612)
If I had time and a job where I got to code I would, but it'll be a few days before I have time.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:44AM (#8130646)
Quick question. Is that code you've given ok to use in the project? I just realized that I added and pushed without asking.

GPL?

Re:Show HN: A small virtual machine written in C by axman6 on Monday August 04, 2014 @03:18AM (#8130695)
The binop stuff you can certainly use freely, the X macro stuff is quite well known so I guess that it's also fine to use. I certainly won't be claiming any copyright on the code I shared =)

My personal preference for licensing is one of the BSD or MIT licenses, I'd rather see things of mine used to makie the world a better place than force people to share what they've done with it.

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @03:21AM (#8130701)
Thanks :) Having some weird pasting issues with a macro for POP expansion. http://i.imgur.com/4HiltgI.png

Edit: Just kidding. That was dumb.

Re:Show HN: A small virtual machine written in C by zura on Monday August 04, 2014 @09:29AM (#8131617)
Btw, I remember there was a law stating that the code less than 10(?) lines is not copyright-able, no?
Re:Show HN: A small virtual machine written in C by IgorPartola on Monday August 04, 2014 @02:26PM (#8133258)
My first reaction was to say "it should be based on the number of CPU/GPU/*PU instructions". But then, there are things like this: http://www.norvig.com/spell-correct.html. I wonder how much actual new-ness you can pack into, say, 100 instructions on a modern CPU.
Re:Show HN: A small virtual machine written in C by voidlogic on Monday August 04, 2014 @01:10PM (#8132802)
That would be a a silly rule, for better or worse 10 lines of Haskell might perform the computation of 500 line of Java (even if they took you the same amount of time to write.. heh).
Re:Show HN: A small virtual machine written in C by zura on Monday August 04, 2014 @02:17PM (#8133202)
Well, maybe they made this rule in days when COBOL was in hype? ;)
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:03PM (#8133127)
Or, say, Befunge...
Re:Show HN: A small virtual machine written in C by Alupis on Monday August 04, 2014 @05:29PM (#8134132)
Wrong -- Google lost the first of the Android/Java battles to Oracle for 9 lines of "copy/pasted" range check code.[1][2][3]

[1] https://news.ycombinator.com/item?id=3951575

[2] https://news.ycombinator.com/item?id=3951480

[3] http://www.theverge.com/android/2012/5/9/3010404/judge-denie...

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:27AM (#8130616)
Ah, I feel you — no problem! I do really love the binop macro.
Re:Show HN: A small virtual machine written in C by panic on Monday August 04, 2014 @02:18AM (#8130593)
The implementation is really clean! The mixing of general-purpose registers, special-purpose registers and the stack makes the instruction set a bit weird, though.

For example, instead of using registers, why not have OR pick values off the stack like ADD, or vice versa? Why use EAX for the conditional jump instructions when you could look at the top of the stack? Why have REM when you can just MOV from ERX?

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:20AM (#8130598)
You know, I was thinking about that this morning actually. All great suggestions! If you could make an issue, that would be fantastic.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:38AM (#8130636)
I just changed OR/XOR/AND to be stack based, and good idea for the jump instructions.
Re:Show HN: A small virtual machine written in C by papaf on Monday August 04, 2014 @01:30AM (#8130480)
The code looks really clean to me.

If you feel the urge to convert the VM to a JIT, rather than calling function pointers for each instruction, you might find this blog post useful: http://blog.reverberate.org/2012/12/hello-jit-world-joy-of-s...

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @01:32AM (#8130488)
Thanks! The goal was readability and ease of use, so that's probably why it's slow :P

Not sure I understand. What would it do differently?

Edit: Hang on, so I should generate C or asm? or something?

Re:Show HN: A small virtual machine written in C by papaf on Monday August 04, 2014 @02:23AM (#8130604)
I think that the method you use is "Token Threaded Code" [1]. It's one good way of implementing a VM.

I only added the blog link in case you were interested in extending the VM to use Just In Time compiling. Using a JIT would mean generating assembly.

I like your VM - there is no need to change it. Thanks for sharing.

[1] http://realityforge.org/code/virtual-machines/2011/05/19/int...

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:29AM (#8130619)
Thanks for the info! :)
Re:Show HN: A small virtual machine written in C by TheHydroImpulse on Monday August 04, 2014 @02:22AM (#8130602)
You'd essentially be generating machine code on the fly and letting the cpu execute it, instead of interpreting anything yourself.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:25AM (#8130611)
Interesting. Is that significantly harder?
Re:Show HN: A small virtual machine written in C by TheHydroImpulse on Monday August 04, 2014 @02:33AM (#8130628)
It does make it more complex since you now have another layer to deal with. I wouldn't roll by own JIT compiler though, otherwise it would definitely be a crap ton harder. Using something like libjit or llvm's JIT compiler are two good options.

It's nice having a normal, interpreted VM then building a JIT compiler later on for extra speed. A recent Rust project called `js.rs`[0] started out with a simple interpreter and swapped it for a JIT compiler using libjit.

The biggest difference being you now have to interface with the actual CPU and such.

[0] https://github.com/TomBebbington/js.rs

Re:Show HN: A small virtual machine written in C by chewxy on Monday August 04, 2014 @02:37AM (#8130632)
if you're already writing in C, no. Just write some functions to convert those instructions into asm, and write an assembler (or use one of the many out there) to output them to bytes. Then use mprotect(), tadah!
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:44AM (#8130644)
Interesting.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @01:11AM (#8130432)
I have been working on this project on and off for about 6 months. I would love feedback on the code, design - whatever!
Re:Show HN: A small virtual machine written in C by dsturnbull2049 on Monday August 04, 2014 @01:27AM (#8130470)
Very concise implementation. I keep thinking there's missing files or at least I'm missing something in my understanding. :)

I wrote a 0-operand VM in C a few years ago that used a lot of the same concepts but considerably more code, like 10x at least. I will learn a lot from this.

Re:Show HN: A small virtual machine written in C by listic on Sunday August 10, 2014 @05:46AM (#8159327)
Unfortunately, with the ever-increasing processor-memory gap (the growing discrepancy between processor and memory speeds, also called Memory Wall, if I'm not mistaken), 0-operand, or stack VM's are seeing ever-increasing performance penalties.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @01:27AM (#8130472)
What kinds of files look missing? I'm curious.

What is a 0 operand VM? I'd love to see it!

Re:Show HN: A small virtual machine written in C by chewxy on Monday August 04, 2014 @02:00AM (#8130552)
Using only PUSH and POP I think. i.e a stack machine.

also, OP, your code looks great.

Re:Show HN: A small virtual machine written in C by dsturnbull2049 on Wednesday August 06, 2014 @03:29AM (#8141437)
Yeah, a stack machine. https://github.com/dsturnbull/stack_cpu

What I mean by missing files is that it's so clean that at first glance it didn't look like the whole deal :)

Re:Show HN: A small virtual machine written in C by tekknolagi on Wednesday August 06, 2014 @11:36AM (#8142998)
Oh well thanks — that's quite a compliment.

This is pretty cool!

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:03AM (#8130556)
Oh interesting. That sounds... complicated.

Thanks! It's my first big C project that isn't hideous :D

Re:Show HN: A small virtual machine written in C by Hytosys on Monday August 04, 2014 @02:08AM (#8130564)
Just for anyone looking for more information: http://en.wikipedia.org/wiki/Instruction_set#Number_of_opera...
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:16AM (#8130586)
Awesome!
Re:Show HN: A small virtual machine written in C by ghkbrew on Monday August 04, 2014 @02:12AM (#8130572)
Is there a reason for the combination of register-based (logical ops) and stack-based (arithmetic ops) operations?

Otherwise fun project.

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:25AM (#8130613)
Probably just my sloppiness. Things like OR were implemented while looking at other projects like tinyvm, so it's not perfectly consistent.
Re:Show HN: A small virtual machine written in C by zura on Monday August 04, 2014 @09:36AM (#8131639)
Nice. Any particular tutorial/article/book you're following?
Re:Show HN: A small virtual machine written in C by wsc981 on Monday August 04, 2014 @05:05PM (#8134023)
Perhaps, if you're interested in learning to write well-written C code, you might be interested in the book "C Programming: A Modern Approach"[0] by King. Coding style is an important topic in his book.

[0]: http://www.amazon.com/Programming-Modern-Approach-2nd-Editio...

Re:Show HN: A small virtual machine written in C by zura on Tuesday August 05, 2014 @05:06PM (#8139427)
Thank you, seems very interesting. Usually, I only write pure C code when I really have to. Otherwise, I see no reason why someone should restrict "shimself" from using C++.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:03PM (#8132395)
Nope :) Lots of small web resources.
Re:Show HN: A small virtual machine written in C by lindig on Monday August 04, 2014 @06:03AM (#8130993)
As inspiration, I would suggest to look at the virtual machine (byte code interpreter) of the Lua language. You can find several papers describing the design at http://www.lua.org/docs.html. The code base is also very small and clean.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:02PM (#8132390)
Awesome!
Re:Show HN: A small virtual machine written in C by akkartik on Monday August 04, 2014 @02:09AM (#8130565)
Can you give some directions for how to run it?

  $ git clone https://github.com/tekknolagi/carp
  $ make
  $ ./carp
  $ ./carp --help  # after peeking in carp.c
  help msg
There's a tantalizing target called 'tests' in the Makefile, but it doesn't work.

There's some examples, but no makefile to build them. How do you run them?

I see you used to have some .carp files but you just deleted them: https://github.com/tekknolagi/carp/commit/fa16eeb443

I tried restoring one, but it doesn't work:

  $ git checkout fa16eeb443~1 examples/reg.carp
  $ ./carp -f examples/reg.carp
  Unknown label <add>
So at this point I'm ready to give up..
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:11AM (#8130570)
Hi there! Sorry for this. I actually have a help message but for some reason I forgot to commit it. It's coming soon!

You run the examples like so: carp -f examples/carp/call.carp

You can compile C files that use the Carp API, but I have not written any documentation yet.

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:12AM (#8130574)
Oh that's weird. the carp folder from examples disappeared. Gimme a sec.
Re:Show HN: A small virtual machine written in C by proxyswapi on Monday August 04, 2014 @03:55AM (#8130748)
A year (or two) ago, I wrote a simple virtual machine [1] of my own. Fun time. Interesting exercise. Wish I had courage to post it here and get feedback from the HN. Anyways, yours is much cleaner and concise code. Kudos.

[1] https://github.com/swapi/z9

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @03:58AM (#8130757)
Definitely post it! I've written muuuuch worse and it's still public :D

Check out http://github.com/tekknolagi/gecho — shudder

Re:Show HN: A small virtual machine written in C by proxyswapi on Monday August 04, 2014 @05:12AM (#8130905)
Posted my reply on wrong comment (Got confused between akkartik and tekknolagi) :). Anyways, thanks for sharing. Are you planning to add memory management opcodes (Allocate memory, free memory)? I see DBS, DBG instruction, but it is like key-value store (since it uses carp_ht internally).
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:02PM (#8132388)
Umm perhaps. Could you give a use case? Not sure how/why.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:14AM (#8130582)
Alright, try now. Also — thanks for the pull request!
Re:Show HN: A small virtual machine written in C by akkartik on Monday August 04, 2014 @02:21AM (#8130600)
You're welcome. I pulled the new commits, but I'm still having trouble running the examples:

  $ ./carp -f examples/carp/call.carp 
  Unknown label <add>
(I am very interested in building easy-to-understand codebases: http://akkartik.name/post/wart)
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:24AM (#8130608)
That's strange: http://i.imgur.com/TbF7Izc.png

Try rebuilding carp? o.O

That was some very nice documentation!

Re:Show HN: A small virtual machine written in C by akkartik on Monday August 04, 2014 @02:28AM (#8130618)
Nope, rebuilding didn't help.. never mind, I'll look into it later tonight.

Edit: I just noticed you changed the target (why?) and are also immediately deleting carp.out. So I was indeed using the stale version in spite of using 'make clean' :)

Anyways, so I did this, but still no luck.

  $ git diff
  diff --git a/Makefile b/Makefile
  index f921d96..7c55b4e 100644
  --- a/Makefile
  +++ b/Makefile
  @@ -10,7 +10,6 @@ all:
	  $(CC) $(CFLAGS) $(SRCS)
	  ar cr libcarp.a $(OBJS)
	  $(CC) src/carp.c libcarp.a -o $(PROG)
  -       make clean_objs
   
   #.PHONY: tests
   
  $ make
  gcc  -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static   src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
  ar cr libcarp.a *.o
  gcc  src/carp.c libcarp.a -o carp.out
  $ ./carp.out -f reg.carp 
  Unknown label <add>
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:39AM (#8130638)
Yeah I realized that I had changed PROG to carp.out but not changed clean_objs. Already pushed a fix!
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:29AM (#8130620)
So strange.
Re:Show HN: A small virtual machine written in C by akkartik on Monday August 04, 2014 @04:29AM (#8130808)
In the process of debugging it I noticed that you're compiling but not linking in c99 mode. When I fix that the problem seems to go away:

  $ git diff
  diff --git a/Makefile b/Makefile
  index 1263dd8..6592c59 100644
  --- a/Makefile
  +++ b/Makefile
  @@ -1,16 +1,16 @@
   CC = gcc #/usr/local/bin/gcc-4.2
   PREFIX = /usr/local
   NDEBUG ?= 
  -CFLAGS = -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG) 
  +CFLAGS = -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG) 
   SRCS = src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
   #$(wildcard src/*.c src/lib/*.c)
   OBJS = *.o
   PROG = carp.out
   
   all:
  -       $(CC) $(CFLAGS) $(SRCS)
  +       $(CC) -c $(CFLAGS) $(SRCS)
	  ar cr libcarp.a $(OBJS)
  -       $(CC) src/carp.c libcarp.a -o $(PROG)
  +       $(CC) $(CFLAGS) src/carp.c libcarp.a -o $(PROG)
   
   #.PHONY: tests
Still, I'm concerned that you might be triggering some sort of undefined behavior, which means it might work for you but not on a slightly different machine or compiler version. I tried printing out token lexemes in carp_run_program, right after the call to tokenize(), and some of the tokens printed binary garbage, suggesting that they might be missing a terminating null, or worse. Does this look right?

  $ git diff
  ...
  diff --git a/src/carp.c b/src/carp.c
  index 9268d98..19e16d9 100644
  --- a/src/carp.c
  +++ b/src/carp.c
  @@ -88,6 +88,8 @@ void carp_print_conditions () {
   
   void carp_run_program (char *fn) {
     carp_tok *tokens = carp_lex_tokenize(fn);
  +  for (carp_tok* tt = tokens; tt; tt=tt->next)
  +    printf("%s %d\n", tt->lexeme, tt->type);
     if (tokens == NULL) {
       fprintf(stderr, "Something went wrong with tokenization.\n");
       exit(1);

  $ make && ./carp.out -f ./examples/carp/call.carp
  gcc  -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static   src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
  ar cr libcarp.a *.o
  gcc  -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static   src/carp.c libcarp.a -o carp.out
  add 3
  gload 6
  -5l 1
  gload 6
  -4l 1
  add 6
  ret 6
  main 3
  push 6
  7�l 1
  push 6
  9�l 1
  call 6
  add 4
  2 1
  ptop 6
  halt 6
  0 1
  16
The trouble with C/C++ is that it's so easy to end up with undefined behavior :/
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @04:36AM (#8130837)
Actually I can't link with the same CFLAGS; I get an error about missing lcrt0.o or something of the sort.

Also, yeah your code looks fine; I added it in and it worked fine. That's unfortunate. I'll debug soon.

Re:Show HN: A small virtual machine written in C by akkartik on Monday August 04, 2014 @04:46AM (#8130854)
What machine/os are you on?

(Perhaps we should take this offline. My email is in my profile.)

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:50PM (#8132680)
Sent an email :)
Re:Show HN: A small virtual machine written in C by zencoder on Monday August 04, 2014 @06:32AM (#8131048)
>The goal is to try and build a small (and decently reliable) VM from the ground up, learning more and more C as I go.

The author explicitly mentions, he is in the process of learning C while he's building something using it.

This is interesting because the folks at [osdev.org](http://wiki.osdev.org) keep stressing that you must be a god-level expert in C, before you even think of getting into systems programming.

Re:Show HN: A small virtual machine written in C by TheSoftwareGuy on Monday August 04, 2014 @09:51AM (#8131691)
I think the guys at osdev mean you need to be good at C before doing systems programming at any form of a professional level. If you don't intend for your code to actually do anything (I mean this is just a side project or something, not for somebody to use in production) than you can really do whatever you want.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:07PM (#8132422)
Definitely a side project.
Re:Show HN: A small virtual machine written in C by wtetzner on Tuesday August 05, 2014 @08:14AM (#8136480)
I don't know about other people, but there's no way I'd be able to become an expert in a language without building things with it.
Re:Show HN: A small virtual machine written in C by abhorrence on Monday August 04, 2014 @02:06PM (#8133138)
This is neat. For others interested in this sort of thing, https://challenge.synacor.com/ specifies a virtual machine, and comes with a binary that performs a self-test -- it is perhaps a neat way to get started.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:24PM (#8133248)
Cool!
Re:Show HN: A small virtual machine written in C by cobookman on Monday August 04, 2014 @03:38AM (#8130725)
Very cool!

Georgia Tech offers a class where you code a VM for the LC3b instruction set (http://users.ece.gatech.edu/~moin/s13a/hw.html).

It was a great learning experience, and pretty fun too.

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @03:48AM (#8130740)
Ah, that's neat! I am not headed off to Georgia Tech though :)
Re:Show HN: A small virtual machine written in C by manish_gill on Monday August 04, 2014 @04:17AM (#8130789)
Can anyone link me to some literature on VMs? Sure, I can analyse the code, but from a design perspective, I would love to learn more about how VMs work, where they can be used, etc etc.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @04:29AM (#8130809)
So I started off following this (http://en.wikibooks.org/wiki/Creating_a_Virtual_Machine/Regi...) which was interesting but clearly never super influential in my design (except in earlier stages, but that died).

Then I read this: http://stackoverflow.com/questions/2034422/tutorial-resource...

and this: http://courses.cms.caltech.edu/cs11/material/c/mike/lab8/lab...

and took a look at the MSP430's instruction set.

As far as where they can be used... anywhere? I don't know the answer to your question - sorry :)

Re:Show HN: A small virtual machine written in C by VLM on Monday August 04, 2014 @10:09AM (#8131776)
"and took a look at the MSP430's instruction set."

I wonder in a general sense if there are many VMs that explicitly copy good older CPUs. I've always thought the 6809 would make an awesome VM. After all, it was pretty awesome to code on in the real world. Or a VM based on the classic 1802, 6502, or Z80 with some minor mods.

If you really want to warp peoples minds give them a VM based on IBM HAL assembler, basically turn Hercules and VM into a hypervisor rather than an application.

A PDP-11 inspired/based virtual machine. Hmm.

Re:Show HN: A small virtual machine written in C by astrobe_ on Monday August 04, 2014 @12:10PM (#8132444)
CISC would be better than RISC because one should keep in mind that for many operation most of the execution time is VM overhead.

But if you want performance you'd rather tailor your instruction set to the language it will execute (assuming you're not making a general purpose VM). CPU makers like Intel do actually keep a close look on what programs are doing in order to evolve their instruction set.

Re:Show HN: A small virtual machine written in C by manish_gill on Monday August 04, 2014 @04:34AM (#8130827)
Thanks for all those links!
Re:Show HN: A small virtual machine written in C by astrobe_ on Monday August 04, 2014 @11:14AM (#8132078)
Re:Show HN: A small virtual machine written in C by ravyoli on Monday August 04, 2014 @04:38PM (#8133877)
Out of curiosity - How are you testing your VM? meaning, how do you know it actually works correctly? I didn't see any tests in the github repository
Re:Show HN: A small virtual machine written in C by tekknolagi on Tuesday August 12, 2014 @11:19PM (#8171303)
I am now starting to write some tests with libtap. If you are game, come help out! :D
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @05:37PM (#8134192)
I am not testing — and I know this is horrifying. I need to write some tests. D:
Re:Show HN: A small virtual machine written in C by jheriko on Monday August 04, 2014 @07:58AM (#8131297)
good stuff.

the power of function pointers for stuff like this is quite enormous. making a vm like this is great practice for a real compiler too... :)

i'm curious though, why it isn't a direct copy of x86? other than the obvious 'thats really complicated' it would save a lot of wheel reinventing...

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:07PM (#8132419)
I started off following MSP430 but just kind of wanted to make my own thing.
Re:Show HN: A small virtual machine written in C by n1ghtmare_ on Monday August 04, 2014 @04:12AM (#8130772)
"NOP (): Does nothing. Seriously." - I love this :)
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @04:13AM (#8130779)
Me too :D
Re:Show HN: A small virtual machine written in C by n1ghtmare_ on Monday August 04, 2014 @04:51AM (#8130865)
Good job man, code looks tight, wish I had some more time to jump on this. Keep it up.

EDIT: Btw, do you happen to have a book or some study material on this subject ? Anything you can recommend ?

Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @12:08PM (#8132430)
Whenever you get a chance just send a stray pull request :)

I don't - I wasn't really following anything unified. I just pulled together a bunch of crappy web resources :)

[deleted]
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:43AM (#8130643)
Pardon?
Re:Show HN: A small virtual machine written in C by ourjs on Monday August 04, 2014 @02:50AM (#8130650)
I mean it's a big story, if you can do part of job of VMWare
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @02:56AM (#8130659)
This is a totally different type of VM than VMWare makes.

This is what's called a process virtual machine, and they make application virtual machines (I think).

Basically, this acts as a small CPU-type-thing and interprets instructions. Theirs emulate whole operating systems.

Re:Show HN: A small virtual machine written in C by ourjs on Monday August 04, 2014 @03:19AM (#8130697)
Sound more like container
Re:Show HN: A small virtual machine written in C by jsmeaton on Monday August 04, 2014 @03:42AM (#8130734)
It's similar to the Java Virtual Machine (JVM) or the .NET CLR VM if you're familiar with either of those.
Re:Show HN: A small virtual machine written in C by tekknolagi on Monday August 04, 2014 @03:21AM (#8130702)
Mmm nope. Nothing about kernels in here.
Re:Show HN: A small virtual machine written in C by bronson on Monday August 04, 2014 @03:41AM (#8130727)
Think VM as in JVM.