-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassembler.c
83 lines (72 loc) · 2.56 KB
/
assembler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "assembler.h"
#include "external_vars.h"
#include "scanFunctions.h"
#include "functions.h"
/*commands List*/
const char* commands[] = { "add", "sub", "and", "or", "nor", "move", "mvhi", "mvlo", "addi", "subi", "andi", "ori", "nori", "bne", "beq", "blt", "bgt", "lb", "sb", "lw", "sw", "lh", "sh", "jmp", "la", "call", "stop"};
/*guidance List*/
const char* guidance[] = { ".entry", ".db", ".dh", ".asciz", ".dw", ".extern" };
unsigned int instructions_image[MAX_LINES];
unsigned char data_image[MAX_LINES];
int ic;
int dc;
int error;
labelPtr symbols_table;
extPtr ext_list;
boolean entry_exists, extern_exists, error_exist;
/* This fucnction resets all flags for next file */
void reset_flags() {
symbols_table = NULL;
ext_list = NULL;
entry_exists = FALSE;
extern_exists = FALSE;
error_exist = FALSE;
}
/* Check if file end with .as*/
int check_file_name(char *name){
int len = strlen(name)-3;
int i=0;
for (i = 0; i < len; i++)
{
name++;
}
return (strcmp(name,".as")==0);
}
/* The main function of the program */
int main(int argc, char* argv[]) {
int i;
FILE* fp;
if (argc < 2) {
fprintf(stdout, "No argument found, please enter a file name to process\n");
exit(1);
}
else {
for (i = 1; i < argc; i++) {
if (!check_file_name(argv[i]))
fprintf(stderr, "\nInvalid file type: %s\n", argv[i]);
else{
if ((fp = fopen(argv[i], "r")) == NULL) { /*Open the file*/
fprintf(stderr, "\nCannot open file: %s\n", argv[i]);
}
else {
if (feof(fp) == 1)/* Checks for empty file */
fprintf(stderr, "\nThe file is empty\n");
else {
/*shadi say remove this msg */
reset_flags();/* Resets all flags for the next file */
scanOne(fp);
if (error == 0) {/* If there are no errors, continue with second pass */
rewind(fp);/*Starts the second pass from the start of the file*/
scanTwo(fp, argv[i]);
}
else {/* First pass contains errors, stop processing the file */
fprintf(stdout, "Errors found in file: %s, stoped working on file\n", argv[i]);
}
fclose(fp); /* Close file */
}
}
}
}
}
return 0;
}