Created
November 26, 2018 06:34
-
-
Save qbig/df2c12bbff816829ac8ae33269f23267 to your computer and use it in GitHub Desktop.
Minimalist VM in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This is almost identical to the articles | |
VM | |
https://github.com/felixangell/mac/blob/master/mac.c | |
**/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
bool running = true; | |
int ip = 0; | |
int sp = -1; | |
int stack[256]; | |
typedef enum { | |
PSH, | |
ADD, | |
POP, | |
HLT | |
} InstructionSet; | |
const int program[] = { | |
PSH, 5, | |
PSH, 6, | |
ADD, | |
POP, | |
HLT | |
}; | |
int fetch() { | |
return program[ip]; | |
} | |
void eval(int instr) { | |
switch (instr) { | |
case HLT: { | |
running = false; | |
printf("done\n"); | |
break; | |
} | |
case PSH: { | |
sp++; | |
stack[sp] = program[++ip]; | |
break; | |
} | |
case POP: { | |
int val_popped = stack[sp--]; | |
printf("popped %d\n", val_popped); | |
break; | |
} | |
case ADD: { | |
// first we pop the stack and store it as a | |
int a = stack[sp--]; | |
// then we pop the top of the stack and store it as b | |
int b = stack[sp--]; | |
// we then add the result and push it to the stack | |
int result = b + a; | |
sp++; // increment stack pointer **before** | |
stack[sp] = result; // set the value to the top of the stack | |
// all done! | |
break; | |
} | |
} | |
} | |
int main() { | |
while (running) { | |
eval(fetch()); | |
ip++; // increment the ip every iteration | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment