From 827937e4dca5fbe3bd24113ecbf1adcb2f10bf58 Mon Sep 17 00:00:00 2001 From: ALittlePatate Date: Mon, 22 Jan 2024 19:49:14 +0100 Subject: [PATCH] add: pointers, & and * keywords stack is now long long :sadge: --- README.md | 1 + docs/documentation.md | 3 ++- examples/ptr.pasm | 7 +++++++ src/debug.c | 22 +++++++++++----------- src/instructions.c | 30 +++++++++++++++++++++--------- src/instructions.h | 4 ++-- src/interpreter_states.c | 2 +- src/interpreter_states.h | 4 ++-- src/pasm.c | 4 ++-- 9 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 examples/ptr.pasm diff --git a/README.md b/README.md index 70d898c..10e2dd1 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,5 @@ Then link the library to your program, see [this example](tests/lib_use.c).
# Code examples - [keylogger](examples/keylogger.pasm) - [polynomial calculator](examples/poly.pasm) +- [pointers usage example](examples/ptr.pasm) - [functions test](examples/test.pasm) diff --git a/docs/documentation.md b/docs/documentation.md index 3ac5e51..cacdb6f 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -92,7 +92,8 @@ The syntax is very close to x86 Intel Assembly. Here is a list of the operands a All the `jmp`-related operands (`je`, `jna`, ...) can have a number as argument, this way the program will jump to `x` lines (ex: `jmp 3` will jump 3 lines down).
All the operands are case-sensitive, meaning that `ADD` will be an invalid operand.
-Please note that additional operands will be added in the future. +Please note that additional operands will be added in the future.
+You can use the `&` and the `*` keywords just like in C to get the address and/or dereference and address. Example : `mov a1, &eax` ### Calling APIs APIs can be added in the [api.c](https://github.com/ALittlePatate/pasm/blob/main/src/api.c) and [api.h](https://github.com/ALittlePatate/pasm/blob/main/src/api.h) files. diff --git a/examples/ptr.pasm b/examples/ptr.pasm new file mode 100644 index 0000000..85af9bc --- /dev/null +++ b/examples/ptr.pasm @@ -0,0 +1,7 @@ +;; pointers usage example + +main: +mov eax, 5 +mov a1, &eax ; move the address of eax in a1 +mov a1, *a1 ; dereference a1 in a1 +end \ No newline at end of file diff --git a/src/debug.c b/src/debug.c index 4f168a1..c4f6c7c 100644 --- a/src/debug.c +++ b/src/debug.c @@ -6,23 +6,23 @@ void show_registers() { printf("--Registers--\n"); - printf("a1: %-3d | ", state->registers->a1); - printf("a2: %-3d | ", state->registers->a2); - printf("a3: %-3d\n", state->registers->a3); - printf("a4: %-3d | ", state->registers->a4); - printf("a5: %-3d | ", state->registers->a5); - printf("a6: %-3d\n", state->registers->a6); - printf("a7: %-3d | ", state->registers->a7); - printf("a8: %-3d | ", state->registers->a8); - printf("a9: %-3d\n", state->registers->a9); - printf("eax: %-3d\n\n", state->registers->eax); + printf("a1: %-3lld | ", state->registers->a1); + printf("a2: %-3lld | ", state->registers->a2); + printf("a3: %-3lld\n", state->registers->a3); + printf("a4: %-3lld | ", state->registers->a4); + printf("a5: %-3lld | ", state->registers->a5); + printf("a6: %-3lld\n", state->registers->a6); + printf("a7: %-3lld | ", state->registers->a7); + printf("a8: %-3lld | ", state->registers->a8); + printf("a9: %-3lld\n", state->registers->a9); + printf("eax: %-3lld\n\n", state->registers->eax); } void show_stack() { printf("--STACK--\n"); printf("Elements: %d\n\n", state->STACK_IDX); for (int i = 0; i < state->STACK_IDX; i++) - printf("[%d]: %d\n", i, state->STACK[state->STACK_IDX]); + printf("[%d]: %lld\n", i, state->STACK[state->STACK_IDX]); printf("\n"); } diff --git a/src/instructions.c b/src/instructions.c index 6280d43..87dc5d6 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -6,8 +6,10 @@ #include bool is_reg(char* arg) { + if (arg[0] == '&' || arg[0] == '*') + ++arg; return (strcmp(arg, "eax") == 0) || (((arg[0] == 'a' && - ('0' <= arg[1] && arg[1] <= '9'))) && strlen(arg) == 2); + ('1' <= arg[1] && arg[1] <= '9'))) && strlen(arg) == 2); } bool is_num(char* arg) { @@ -33,7 +35,9 @@ bool check_args(s_arguments *args, int num_in_first, int num_args) { return true; } -int* get_reg(char* reg_char) { +long long* get_reg(char* reg_char) { + if (reg_char[0] == '&' || reg_char[0] == '*') + ++reg_char; switch (reg_char[1]) { case '1' : return &state->registers->a1; @@ -60,11 +64,19 @@ int* get_reg(char* reg_char) { } } -int get_value(char* arg) { - int ret = 0; +long long get_value(char* arg) { + long long ret = 0; if (is_reg(arg)) { - ret = *get_reg(arg); + if (arg[0] == '&') { + ret = get_reg(arg); + } + else if (arg[0] == '*') { + ret = *(long long *)(*get_reg(arg)); + } + else { + ret = *get_reg(arg); + } } else { ret = atoi(arg); @@ -90,8 +102,8 @@ void cmp() { return; } - int a1_ = get_value(state->args->arg1); - int a2_ = get_value(state->args->arg2); + long long a1_ = get_value(state->args->arg1); + long long a2_ = get_value(state->args->arg2); if (a1_ == a2_) state->last_cmp_code = CMP_EQUAL; else if (a1_ > a2_) state->last_cmp_code = CMP_ABOVE; @@ -196,7 +208,7 @@ void _sqrt() { return; } - *get_reg(state->args->arg1) = (int)sqrt(get_value(state->args->arg1)); + *get_reg(state->args->arg1) = (long long)sqrt(get_value(state->args->arg1)); } void neg() { @@ -247,7 +259,7 @@ void push() { return; } - int value = get_value(state->args->arg1); + long long value = get_value(state->args->arg1); if (value == 0 && !is_reg(state->args->arg1)) { if (state->args->arg1[0] == '\\') { switch (state->args->arg1[1]) { diff --git a/src/instructions.h b/src/instructions.h index 52f9db8..b2b0565 100644 --- a/src/instructions.h +++ b/src/instructions.h @@ -10,8 +10,8 @@ typedef struct command_s { bool is_reg(char* arg); bool check_args(s_arguments *args, int num_in_first, int num_args); -int* get_reg(char* arg); -int get_value(char* arg); +long long* get_reg(char* arg); +long long* get_reg(char* reg_char); void add(); void sub(); diff --git a/src/interpreter_states.c b/src/interpreter_states.c index ab28f78..af96d42 100644 --- a/src/interpreter_states.c +++ b/src/interpreter_states.c @@ -44,7 +44,7 @@ int init_state() { } memset(state->labels_values, 0, sizeof(int) * MAX_LABELS); memset(state->RET_STACK, -1, sizeof(int) * STACK_SIZE); - memset(state->STACK, 0, sizeof(int) * STACK_SIZE); + memset(state->STACK, 0, sizeof(long long) * STACK_SIZE); state->RET_STACK_IDX = -1; state->STACK_IDX = -1; state->last_stack_code = STACK_OK; diff --git a/src/interpreter_states.h b/src/interpreter_states.h index 2d1ae0d..c49f018 100644 --- a/src/interpreter_states.h +++ b/src/interpreter_states.h @@ -33,7 +33,7 @@ typedef struct t_arguments { } s_arguments; typedef struct t_registers { - int a1, a2, a3, a4, a5, a6, a7, a8, a9, eax; + long long a1, a2, a3, a4, a5, a6, a7, a8, a9, eax; } s_registers; typedef struct t_state { @@ -47,7 +47,7 @@ typedef struct t_state { int RET_STACK[STACK_SIZE]; int RET_STACK_IDX; int STACK_IDX; - int STACK[STACK_SIZE]; + long long STACK[STACK_SIZE]; int last_jmp_code; stack_codes last_stack_code; cmp_return_codes last_cmp_code; diff --git a/src/pasm.c b/src/pasm.c index 6591916..4e689c6 100644 --- a/src/pasm.c +++ b/src/pasm.c @@ -23,7 +23,7 @@ int dprintf(int stream, const char * format, ...) { int wrote = vsprintf(buf, format, args); struct sockaddr name = {0}; int len = 0; - if (getsockname(stream, &name, &len) == WSAENOTSOCK) { + if (getsockname(stream, &name, &len) != 0) { _write(stream, buf, sizeof(buf)); } else { @@ -89,7 +89,7 @@ int pasm_run_script(const char *filename, char **file, size_t lines, int _fstrea fstream = _fstream; if (filename && read_script(filename, &file, &lines) == 1) - return 1; + return 1; if (init_state() == 1) { dprintf(fstream, "Failed to initialize the interpreter.\n"); free_script(file);