From 09fe418e4dab46c6e1cd4be7081533c9cdffb21b Mon Sep 17 00:00:00 2001 From: ALittlePatate Date: Sat, 13 May 2023 10:25:53 +0200 Subject: [PATCH] execution will always start at the main label this is much better, we can have labels referenced before --- examples/keylogger.pasm | 39 +++++++++++++++++++-------------------- src/instructions.c | 1 - src/main.c | 12 +++++++++++- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/keylogger.pasm b/examples/keylogger.pasm index 81003b0..e2911c4 100644 --- a/examples/keylogger.pasm +++ b/examples/keylogger.pasm @@ -15,14 +15,30 @@ ; } ; } +; https://stackoverflow.com/a/18670716 +; note about returns : you can directly call ret from a je/jne/ja/jna/jb/jnb with "jb return". +; so return cannot be used as a label. +check: +cmp a1, 58 +jb return ; if < -loop: +cmp a1, 64 +ja return ; if > + +mov eax, 1 +ret + +inc_and_numbers: +add a1, 1 +jmp numbers + +main: mov a1, 47 ;set a1 to 47 numbers: cmp a1, 90 -je loop ; if == +je main ; if == jmp check cmp eax, 1 @@ -40,21 +56,4 @@ mov a2, a1 ; necessary ? push "%c" ; push format push a2 ; push char call printf -jmp numbers - -; https://stackoverflow.com/a/18670716 -; note about returns : you can directly call ret from a je/jne/ja/jna/jb/jnb with "jb return". -; so return cannot be used as a label. -check: -cmp a1, 58 -jb return ; if < - -cmp a1, 64 -ja return ; if > - -mov eax, 1 -ret - -inc_and_numbers: -add a1, 1 -jmp numbers +jmp numbers \ No newline at end of file diff --git a/src/instructions.c b/src/instructions.c index 231c8ac..509bca7 100644 --- a/src/instructions.c +++ b/src/instructions.c @@ -93,7 +93,6 @@ void cmp() { } void jmp() { - return; for (int i = 0; i < MAX_LABEL; i++) { if (labels[i] == NULL) break; if (strcmp(args->arg1, labels[i]) == 0) { diff --git a/src/main.c b/src/main.c index 27aa3ab..dae37fb 100644 --- a/src/main.c +++ b/src/main.c @@ -44,8 +44,8 @@ int main(int argc, char** argv) { int line_number = 1; size_t char_read = 0; const command_t* com = NULL; + int main_hit = 0; while (fgets(line, sizeof(line), fptr)) { - com = NULL; char_read += strlen(line); if (line[0] == ';' || line[0] == '\n') { @@ -59,6 +59,16 @@ int main(int argc, char** argv) { get_instruction(line, &args_pos, (int)char_read, &ins); if (args_pos == -1) { + if (!main_hit && strcmp("main", ins) == 0) { + main_hit = 1; + } + + free(ins); + ++line_number; + continue; + } + + if (!main_hit) { free(ins); ++line_number; continue;