execution will always start at the main label

this is much better, we can have labels referenced before
This commit is contained in:
2023-05-13 10:25:53 +02:00
parent 5c776e4a84
commit 09fe418e4d
3 changed files with 30 additions and 22 deletions

View File

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

View File

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

View File

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