-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode.c
102 lines (85 loc) · 2.08 KB
/
opcode.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "opcode.h"
hnptr op_table[HASH_SIZE] = { NULL, };
//read opcode.txt and store in hash table
//if file open error or file close error return 0 and print error message
//else return 1
int read_opcode_file()
{
FILE* fp = fopen("opcode.txt", "r");
if (fp == NULL)
{
error_print(ERROR_FILE_OPEN);
return 0;
}
char buffer[FILE_BUFFER_SIZE];
char tmp_buffer[FILE_BUFFER_SIZE];
while (fgets(buffer, FILE_BUFFER_SIZE, fp) != NULL)
{
strcpy(tmp_buffer, buffer);
refine_argument(tmp_buffer, buffer);
char *arg = buffer;
char *parse;
onptr pnew = (onptr)malloc(sizeof(*pnew));
parsing_argument(&arg, &parse);
pnew->opcode = hex_to_int(parse);
parsing_argument(&arg, &parse);
strcpy(pnew->mnemonic, parse);
parsing_argument(&arg, &parse);
strcpy(pnew->format, parse);
int hash_number = hash_function(pnew->mnemonic);
add_hash(op_table, pnew, hash_number);
}
if (fclose(fp) == EOF)
{
error_print(ERROR_FILE_CLOSE);
return 0;
}
return 1;
}
//find precise mnemonic in hash table
//if success to find, return the mnemonic node else return null pointer
onptr find_mnemonic(char mnemonic[])
{
int hash_number = hash_function(mnemonic);
for (hnptr p = op_table[hash_number]; p; p = p->link)
{
onptr op = p->data;
if (strcmp(op->mnemonic, mnemonic) == 0)
return op;
}
return NULL;
}
//execute opcodelist command
//show all opcode in each hash table number
void cmd_opcodelist()
{
for (int i = 0; i < HASH_SIZE; i++)
{
printf("%d : ", i);
for (hnptr p = op_table[i]; p; p = p->link)
{
onptr data = p->data;
printf("[%s,%02X] ", data->mnemonic, data->opcode);
if (p->link)
printf("-> ");
}
printf("\n");
}
}
//execute opcode mnemonic command
//show opcode of the mnemonic
int cmd_opcode_mnemonic(char mnemonic[])
{
onptr op = find_mnemonic(mnemonic);
if (op == NULL)
{
return ERROR_NOT_MNEMONIC;
}
printf("opcode is %02X\n", op->opcode);
return 1;
}
//get op_table address
hnptr* get_op_table_addr()
{
return op_table;
}