diff --git a/README.md b/README.md index 5120fc0..05103ab 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Stack based virtual machine and assembly code compiler - Assembly - Stack operations (push & pop) - Arithmetics operations + - I/O operations ## Assembly documentation @@ -28,6 +29,10 @@ Stack based virtual machine and assembly code compiler Removes two top values in stack and pushes integer part of division - **MOD** Removes two top values in stack and pushes modulo +- **IN** + Inputs string with C I/O operators +- **OUT** + Outputs string with C I/O operators ## TODO diff --git a/command.c b/command.c index d97a84e..f364f6c 100644 --- a/command.c +++ b/command.c @@ -8,4 +8,8 @@ cmd_desc_t cmd_desc[] = { {MUL, 0, "MUL"}, {DIV, 0, "DIV"}, {MOD, 0, "MOD"}, +#ifdef IO_OPERATIONS + {IN, 0, "IN"}, + {OUT, 0, "OUT"}, +#endif {NONE, 0, "NONE"}} diff --git a/command.h b/command.h index dd340d0..1b0738c 100644 --- a/command.h +++ b/command.h @@ -10,6 +10,10 @@ enum command_e MUL, DIV, MOD, +#ifdef IO_OPERATIONS + IN, + OUT, +#endif NONE }; diff --git a/run.c b/run.c index 90d400a..e843d1d 100644 --- a/run.c +++ b/run.c @@ -123,8 +123,45 @@ int exec(command_t cmd, stack_t *stack) return *res; } break; +#ifdef IO_OPERATIONS + /** + * Inputs string with C I/O operators + */ + case IN: + { + char input[512]; + scanf("%s", &input); + int size = str_len(input); + + for (int pos = size; size >= 0; size--) + { + stack_push(stack, input[pos], res); + if (res != NULL) + return *res; + } + } + break; + /** + * Outputs string with C I/O operators + */ + case OUT: + { + char ch = stack_pop(stack, res); + if (res != NULL) + return *res; + + while (ch != NULL) + { + putchar(ch); + ch = stack_pop(stack, res); + if (res != NULL) + return *res; + } + } + break; +#endif default: - return -1; + return 1; } return 0; diff --git a/run.h b/run.h index 7ee3f5f..6107be6 100644 --- a/run.h +++ b/run.h @@ -3,6 +3,11 @@ #include "./stack.h" #include "./command.h" +#include "./utils.h" + +#ifdef IO_OPERATIONS +#include +#endif int run(command_t **buff, stack_t *stack); int exec(command_t cmd, stack_t *stack); diff --git a/stack.c b/stack.c index f67d285..3c72d6c 100644 --- a/stack.c +++ b/stack.c @@ -30,12 +30,12 @@ int stack_push(stack_t *stack, int value, int *result) if (stack->cursor > stack->size - 1) { - *result = 1; + *result = 2; return *result; } stack->mem[stack->cursor + 1] = value; - stack->cursor += 1; + stack->cursor++; return 0; } @@ -51,7 +51,7 @@ int stack_pop(stack_t *stack, int *result) } int value = stack->mem[stack->cursor]; - stack->cursor -= 1; + stack->cursor--; return value; } diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..5e6c8ae --- /dev/null +++ b/utils.c @@ -0,0 +1,11 @@ +#include "./utils.h" + +int str_len(char str[]) +{ + int pos = 0; + + while (str[pos] != NULL) + pos++; + + return pos; +} \ No newline at end of file diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..a733f8b --- /dev/null +++ b/utils.h @@ -0,0 +1,7 @@ +#ifndef UTILS_H + +#include + +int str_len(char str[]); + +#endif \ No newline at end of file