add: pointers, & and * keywords

stack is now long long :sadge:
This commit is contained in:
2024-01-22 19:49:14 +01:00
parent 64f8ecdbb3
commit 827937e4dc
9 changed files with 49 additions and 28 deletions

View File

@@ -35,4 +35,5 @@ Then link the library to your program, see [this example](tests/lib_use.c).<br>
# Code examples # Code examples
- [keylogger](examples/keylogger.pasm) - [keylogger](examples/keylogger.pasm)
- [polynomial calculator](examples/poly.pasm) - [polynomial calculator](examples/poly.pasm)
- [pointers usage example](examples/ptr.pasm)
- [functions test](examples/test.pasm) - [functions test](examples/test.pasm)

View File

@@ -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).<br> 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).<br>
All the operands are case-sensitive, meaning that `ADD` will be an invalid operand.<br> All the operands are case-sensitive, meaning that `ADD` will be an invalid operand.<br>
Please note that additional operands will be added in the future. Please note that additional operands will be added in the future.<br>
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 ### 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. 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.

7
examples/ptr.pasm Normal file
View File

@@ -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

View File

@@ -6,23 +6,23 @@
void show_registers() { void show_registers() {
printf("--Registers--\n"); printf("--Registers--\n");
printf("a1: %-3d | ", state->registers->a1); printf("a1: %-3lld | ", state->registers->a1);
printf("a2: %-3d | ", state->registers->a2); printf("a2: %-3lld | ", state->registers->a2);
printf("a3: %-3d\n", state->registers->a3); printf("a3: %-3lld\n", state->registers->a3);
printf("a4: %-3d | ", state->registers->a4); printf("a4: %-3lld | ", state->registers->a4);
printf("a5: %-3d | ", state->registers->a5); printf("a5: %-3lld | ", state->registers->a5);
printf("a6: %-3d\n", state->registers->a6); printf("a6: %-3lld\n", state->registers->a6);
printf("a7: %-3d | ", state->registers->a7); printf("a7: %-3lld | ", state->registers->a7);
printf("a8: %-3d | ", state->registers->a8); printf("a8: %-3lld | ", state->registers->a8);
printf("a9: %-3d\n", state->registers->a9); printf("a9: %-3lld\n", state->registers->a9);
printf("eax: %-3d\n\n", state->registers->eax); printf("eax: %-3lld\n\n", state->registers->eax);
} }
void show_stack() { void show_stack() {
printf("--STACK--\n"); printf("--STACK--\n");
printf("Elements: %d\n\n", state->STACK_IDX); printf("Elements: %d\n\n", state->STACK_IDX);
for (int i = 0; i < state->STACK_IDX; i++) 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"); printf("\n");
} }

View File

@@ -6,8 +6,10 @@
#include <stdlib.h> #include <stdlib.h>
bool is_reg(char* arg) { bool is_reg(char* arg) {
if (arg[0] == '&' || arg[0] == '*')
++arg;
return (strcmp(arg, "eax") == 0) || (((arg[0] == 'a' && 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) { bool is_num(char* arg) {
@@ -33,7 +35,9 @@ bool check_args(s_arguments *args, int num_in_first, int num_args) {
return true; 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]) { switch (reg_char[1]) {
case '1' : case '1' :
return &state->registers->a1; return &state->registers->a1;
@@ -60,12 +64,20 @@ int* get_reg(char* reg_char) {
} }
} }
int get_value(char* arg) { long long get_value(char* arg) {
int ret = 0; long long ret = 0;
if (is_reg(arg)) { if (is_reg(arg)) {
if (arg[0] == '&') {
ret = get_reg(arg);
}
else if (arg[0] == '*') {
ret = *(long long *)(*get_reg(arg));
}
else {
ret = *get_reg(arg); ret = *get_reg(arg);
} }
}
else { else {
ret = atoi(arg); ret = atoi(arg);
} }
@@ -90,8 +102,8 @@ void cmp() {
return; return;
} }
int a1_ = get_value(state->args->arg1); long long a1_ = get_value(state->args->arg1);
int a2_ = get_value(state->args->arg2); long long a2_ = get_value(state->args->arg2);
if (a1_ == a2_) state->last_cmp_code = CMP_EQUAL; if (a1_ == a2_) state->last_cmp_code = CMP_EQUAL;
else if (a1_ > a2_) state->last_cmp_code = CMP_ABOVE; else if (a1_ > a2_) state->last_cmp_code = CMP_ABOVE;
@@ -196,7 +208,7 @@ void _sqrt() {
return; 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() { void neg() {
@@ -247,7 +259,7 @@ void push() {
return; 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 (value == 0 && !is_reg(state->args->arg1)) {
if (state->args->arg1[0] == '\\') { if (state->args->arg1[0] == '\\') {
switch (state->args->arg1[1]) { switch (state->args->arg1[1]) {

View File

@@ -10,8 +10,8 @@ typedef struct command_s {
bool is_reg(char* arg); bool is_reg(char* arg);
bool check_args(s_arguments *args, int num_in_first, int num_args); bool check_args(s_arguments *args, int num_in_first, int num_args);
int* get_reg(char* arg); long long* get_reg(char* arg);
int get_value(char* arg); long long* get_reg(char* reg_char);
void add(); void add();
void sub(); void sub();

View File

@@ -44,7 +44,7 @@ int init_state() {
} }
memset(state->labels_values, 0, sizeof(int) * MAX_LABELS); memset(state->labels_values, 0, sizeof(int) * MAX_LABELS);
memset(state->RET_STACK, -1, sizeof(int) * STACK_SIZE); 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->RET_STACK_IDX = -1;
state->STACK_IDX = -1; state->STACK_IDX = -1;
state->last_stack_code = STACK_OK; state->last_stack_code = STACK_OK;

View File

@@ -33,7 +33,7 @@ typedef struct t_arguments {
} s_arguments; } s_arguments;
typedef struct t_registers { 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; } s_registers;
typedef struct t_state { typedef struct t_state {
@@ -47,7 +47,7 @@ typedef struct t_state {
int RET_STACK[STACK_SIZE]; int RET_STACK[STACK_SIZE];
int RET_STACK_IDX; int RET_STACK_IDX;
int STACK_IDX; int STACK_IDX;
int STACK[STACK_SIZE]; long long STACK[STACK_SIZE];
int last_jmp_code; int last_jmp_code;
stack_codes last_stack_code; stack_codes last_stack_code;
cmp_return_codes last_cmp_code; cmp_return_codes last_cmp_code;

View File

@@ -23,7 +23,7 @@ int dprintf(int stream, const char * format, ...) {
int wrote = vsprintf(buf, format, args); int wrote = vsprintf(buf, format, args);
struct sockaddr name = {0}; struct sockaddr name = {0};
int len = 0; int len = 0;
if (getsockname(stream, &name, &len) == WSAENOTSOCK) { if (getsockname(stream, &name, &len) != 0) {
_write(stream, buf, sizeof(buf)); _write(stream, buf, sizeof(buf));
} }
else { else {