diff --git a/Makefile b/Makefile index e38386b..ac7e147 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,9 @@ TARGET_EXEC ?= stack_vm BUILD_DIR ?= ./build SRC_DIRS ?= ./src -SRCS := $(shell find $(SRC_DIRS) -name *.c) +SRCS := $(shell find $(SRC_DIRS) -name "*.c") OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) +DEPS := $(OBJS:.o=.d) INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) @@ -12,6 +13,7 @@ INC_FLAGS := $(addprefix -I,$(INC_DIRS)) CPPFLAGS ?= $(INC_FLAGS) -MMD -MP -D IO_OPERATIONS $(BUILD_DIR)/$(TARGET_EXEC): $(OBJS) + echo $(SRC_DIRS) $(CC) -ggdb3 $(OBJS) -o $@ $(LDFLAGS) $(BUILD_DIR)/%.c.o: %.c @@ -25,4 +27,6 @@ all: $(BUILD_DIR)/$(TARGET_EXEC) clean: $(RM) -r $(BUILD_DIR) +-include $(DEPS) + MKDIR_P ?= mkdir -p \ No newline at end of file diff --git a/README.md b/README.md index 05103ab..4aec7ce 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Stack based virtual machine and assembly code compiler ## Features +- Binary code execution (assembly language compilation coming soon) - Assembly - Stack operations (push & pop) - Arithmetics operations @@ -36,7 +37,6 @@ Stack based virtual machine and assembly code compiler ## TODO -- VM running - Assembly compiler - Variables - Arithmetics operators diff --git a/src/main.c b/src/main.c index cdf7ed3..f47155d 100644 --- a/src/main.c +++ b/src/main.c @@ -67,7 +67,10 @@ int main(int argc, char *argv[]) command_t **commands = read_bin_file(argv[2], res); if (commands == NULL) + { + fprintf(stderr, "Couldn't read file\n"); return 1; + } return run(commands, stack); } diff --git a/src/read_bin.c b/src/read_bin.c index 4d7edc3..f1d4091 100644 --- a/src/read_bin.c +++ b/src/read_bin.c @@ -9,7 +9,7 @@ command_t **read_bin_file(char src[], int *res) int size = -1; fread(&size, sizeof(int), 1, file); - command_t **buffer = malloc(sizeof(command_t *) * size); + command_t **buffer = malloc(sizeof(command_t *) * (size + 1)); if (buffer == NULL) { *res = -1; @@ -20,11 +20,16 @@ command_t **read_bin_file(char src[], int *res) { buffer[i] = malloc(sizeof(command_t)); - fread(buffer[i]->code, sizeof(int), 1, file); + fread(&buffer[i]->code, sizeof(int), 1, file); - buffer[i]->arg_v = malloc(sizeof(int) * cmd_desc[buffer[i]->code].argc); - fread(buffer[i]->arg_v, sizeof(int), cmd_desc[buffer[i]->code].argc, file); + if (cmd_desc[buffer[i]->code].argc) + { + buffer[i]->arg_v = malloc(sizeof(int) * cmd_desc[buffer[i]->code].argc); + fread(buffer[i]->arg_v, sizeof(int), cmd_desc[buffer[i]->code].argc, file); + } } + buffer[size] = NULL; + return buffer; } diff --git a/src/run.c b/src/run.c index 5a3b358..f90bbeb 100644 --- a/src/run.c +++ b/src/run.c @@ -151,7 +151,7 @@ int exec(command_t cmd, stack_t *stack) if (res != NULL) return *res; - while (ch != NULL) + while (ch != '\0') { putchar(ch); ch = stack_pop(stack, res); diff --git a/src/stack.c b/src/stack.c index e5713f3..5e5e568 100644 --- a/src/stack.c +++ b/src/stack.c @@ -6,14 +6,14 @@ stack_t *stack_new(int size, int *result) if (stack == NULL) { *result = -1; - return *result; + return NULL; } stack->mem = (int *)malloc(sizeof(int) * size); if (stack->mem == NULL) { *result = -1; - return *result; + return NULL; } stack->size = size; diff --git a/src/utils.c b/src/utils.c index 5e6c8ae..c8eb7bb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,7 +4,7 @@ int str_len(char str[]) { int pos = 0; - while (str[pos] != NULL) + while (str[pos] != '\0') pos++; return pos;