This commit is contained in:
Smaug123
2017-08-05 14:25:45 +01:00
parent 2e4681b321
commit 4ed94aee49
3 changed files with 76 additions and 22 deletions

View File

@@ -12,6 +12,10 @@
#include "hvm.h" #include "hvm.h"
/*
* PRINT_BUFFER: the length of the buffer for an HVM print statement
*/
#define PRINT_BUFFER 50
#define ERR_IS_OK 0 #define ERR_IS_OK 0
#define ERR_PC_EOB 1 #define ERR_PC_EOB 1
#define ERR_UNKNOWN_INST 2 #define ERR_UNKNOWN_INST 2
@@ -28,6 +32,7 @@
* ERR_MEM: tried to access invalid memory location * ERR_MEM: tried to access invalid memory location
*/ */
#define ERR_MEM 6 #define ERR_MEM 6
#define ERR_UNEXPECTED_OUTPUT 7
#define ERR_OTHER 255 #define ERR_OTHER 255
#define MEMORY_SIZE 16384 #define MEMORY_SIZE 16384
@@ -58,9 +63,9 @@ size_t curr_allocated;
* IN - number of values to copy from `initialise` into the program * IN - number of values to copy from `initialise` into the program
*/ */
void void
set_memory (int *initialise, set_memory (const int *const initialise,
int *memory, int *const memory,
size_t n) size_t n)
{ {
size_t i; size_t i;
@@ -74,11 +79,23 @@ set_memory (int *initialise,
} }
} }
/*
* pop
*
* Removes the top element of the input stack, and returns it in an out arg.
*
* Argument: output
* OUT - integer which was at the top of the stack
*
* Argument: stack
* INOUT - stack to pop from.
*
* Returns: integer return code
*/
int int
pop (int* output, pop (int *const output,
int* stack) int *const stack)
{ {
// printf("Stack pop: ");
int rc = ERR_IS_OK; int rc = ERR_IS_OK;
if (stack_depth == 0) { if (stack_depth == 0) {
@@ -89,7 +106,6 @@ pop (int* output,
*output = stack[stack_depth - 1]; *output = stack[stack_depth - 1];
stack_depth -= 1; stack_depth -= 1;
} }
//printf("%i\n", *output);
return (rc); return (rc);
} }
@@ -98,11 +114,20 @@ pop (int* output,
* delete * delete
* *
* Deletes the `index`th element of arr, and shifts all further elements left. * Deletes the `index`th element of arr, and shifts all further elements left.
*
* Argument: arr
* INOUT - array from which to delete an element
*
* Argument: index
* IN - index which will be deleted
*
* Argument: len
* INOUT - length of arr
*/ */
void void
delete (int *arr, delete (int *const arr,
size_t index, size_t index,
size_t *len) size_t *const len)
{ {
size_t i; size_t i;
@@ -114,18 +139,25 @@ delete (int *arr,
} }
/* /*
* push
* *
* Pushes the given value to a stack.
*
* Argument: val
* IN - value to push to the stack
*
* Argument: stack
* INOUT - stack to push to.
*/ */
int int
push (int val, push (int val,
int **stack) int **stack)
{ {
//printf("Stack push: %i\n", val);
int rc = ERR_IS_OK; int rc = ERR_IS_OK;
if (stack_depth >= curr_allocated) { if (stack_depth >= curr_allocated) {
/* /*
* * More storage required for the stack, since it's full at the moment
*/ */
*stack = realloc(*stack, 2*curr_allocated); *stack = realloc(*stack, 2*curr_allocated);
if (*stack == NULL) { if (*stack == NULL) {
@@ -164,18 +196,35 @@ int_leq_sizet (int a,
return (result); return (result);
} }
/*
* execute_program
*
* Executes a HVM program.
*
* Argument: program
* IN - string program to execute
*
* Argument: initial_mem
* IN - the HVM instance's memory. Only the first MEMORY_SIZE ints will
* be considered; the rest will never be encountered by the HVM.
* This is copied to an internal array. If there are not
* MEMORY_SIZE entries in initial_mem, the internal memory will be
* padded with zeros.
*
* Argument: mem_len
* IN - length of the initial memory array
*/
int int
execute_program (char *program, execute_program (const char *const program,
int *initial_mem, const int *const initial_mem,
int mem_len) const int mem_len)
{ {
int program_counter = 0; int program_counter = 0;
size_t end = strlen(program); size_t end = strlen(program);
bool done = false; bool done = false;
int rc = 0; int rc = 0;
int s0; /* variable into which we pop from stack */ int s0; /* variable into which we pop from stack */
int s1; /* variable into which we pop from stack */ int s1; /* variable into which we pop from stack */
int val_to_push; int val_to_push;
int *stack; int *stack;
int memory[MEMORY_SIZE]; /* HVM interpreted program's memory buffer */ int memory[MEMORY_SIZE]; /* HVM interpreted program's memory buffer */

View File

@@ -12,8 +12,8 @@
#include <stdio.h> #include <stdio.h>
int int
execute_program (char *program, execute_program (const char *const program,
int *initial_mem, const int *const initial_mem,
int mem_len); const int mem_len);
#endif /* hvm_h */ #endif /* hvm_h */

View File

@@ -7,11 +7,16 @@
// //
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "hvm.h" #include "hvm.h"
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
int initial_mem[3] = {105,13,98}; /*
execute_program("10>0<<0:1+1-26*?0<1+0>055*-g0<p", initial_mem, 3); * Example program from www.hacker.org/hvm, should print out 945321
*/
execute_program("123451^2v5:4?9p2g8pppppp",
NULL,
0);
return 0; return 0;
} }