Added arithmetics operations, updated readme

This commit is contained in:
Dmitriy Shishkov 2021-04-16 20:47:25 +05:00
parent 0e5fa6c854
commit f40633f4f1
No known key found for this signature in database
GPG Key ID: 7CAE12ED13853CAC
4 changed files with 57 additions and 1 deletions

View File

@ -1,6 +1,12 @@
# stack_vm # stack_vm
Stack based virtual machine Stack based virtual machine and assembly code compiler
## Features
- Assembly
- Stack operations (push & pop)
- Arithmetics operations
## TODO ## TODO

View File

@ -3,4 +3,9 @@
cmd_desc_t cmd_desc[] = { cmd_desc_t cmd_desc[] = {
{PUSH, 1, "PUSH"}, {PUSH, 1, "PUSH"},
{POP, 0, "POP"}, {POP, 0, "POP"},
{ADD, 0, "ADD"},
{SUB, 0, "SUB"},
{MUL, 0, "MUL"},
{DIV, 0, "DIV"},
{MOD, 0, "MOD"},
{NONE, 0, "NONE"}} {NONE, 0, "NONE"}}

View File

@ -5,6 +5,11 @@ enum command_e
{ {
PUSH, PUSH,
POP, POP,
ADD,
SUB,
MUL,
DIV,
MOD,
NONE NONE
}; };

40
run.c
View File

@ -25,6 +25,46 @@ int exec(command_t cmd, stack_t *stack)
case POP: case POP:
stack_pop(stack); stack_pop(stack);
break; break;
case ADD:
{
int b = stack_pop(stack);
int a = stack_pop(stack);
stack_push(stack, a + b);
}
break;
case SUB:
{
int b = stack_pop(stack);
int a = stack_pop(stack);
stack_push(stack, a - b);
}
break;
case MUL:
{
int b = stack_pop(stack);
int a = stack_pop(stack);
stack_push(stack, a * b);
}
break;
case DIV:
{
int b = stack_pop(stack);
int a = stack_pop(stack);
stack_push(stack, a / b);
}
break;
case MOD:
{
int b = stack_pop(stack);
int a = stack_pop(stack);
stack_push(stack, a % b);
}
break;
case NONE: case NONE:
break; break;
default: default: