-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
178 lines (124 loc) · 4.07 KB
/
commands.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <string.h>
#include "commands.h"
#include "assembler.h"
#define NUM_OF_COMMANDS 16
#define ADR_MODE_IMD_BITS "000"
#define ADR_MODE_DIRECT_BITS "001"
#define ADR_MODE_INDEX_BITS "010"
#define ADR_MODE_INDEX2D_BITS "011"
#define ADR_MODE_REGISTER_BITS "100"
#define ALL_ADR_MODES ADR_MODE_IMD_FLAG | ADR_MODE_DIRECT_FLAG | ADR_MODE_INDEX_FLAG | ADR_MODE_INDEX2D_FLAG | ADR_MODE_REGISTER_FLAG
#define ADR_MODES_1_TO_4 ADR_MODE_DIRECT_FLAG | ADR_MODE_INDEX_FLAG | ADR_MODE_INDEX2D_FLAG | ADR_MODE_REGISTER_FLAG
/* Definition of all the assembly command names, base2 values and allowed addressing modes */
Command commands[NUM_OF_COMMANDS] = {
{"mov", "0000", ALL_ADR_MODES , ADR_MODES_1_TO_4 },
{"cmp", "0001", ALL_ADR_MODES, ALL_ADR_MODES},
{"add", "0010", ALL_ADR_MODES, ADR_MODES_1_TO_4},
{"sub", "0011", ALL_ADR_MODES, ADR_MODES_1_TO_4},
{"not", "0100", NO_OPERAND, ADR_MODE_INDEX_FLAG | ADR_MODE_INDEX2D_FLAG | ADR_MODE_REGISTER_FLAG},
{"clr", "0101", NO_OPERAND, ADR_MODES_1_TO_4},
{"lea", "0110", ADR_MODE_DIRECT_FLAG | ADR_MODE_INDEX_FLAG | ADR_MODE_INDEX2D_FLAG, ADR_MODES_1_TO_4},
{"inc", "0111", NO_OPERAND, ADR_MODES_1_TO_4},
{"dec", "1000", NO_OPERAND, ADR_MODES_1_TO_4},
{"jmp", "1001", NO_OPERAND, ADR_MODES_1_TO_4},
{"bne", "1010", NO_OPERAND, ADR_MODES_1_TO_4},
{"red", "1011", NO_OPERAND, ADR_MODES_1_TO_4},
{"prn", "1100", NO_OPERAND, ALL_ADR_MODES},
{"jsr", "1101", NO_OPERAND, ADR_MODE_DIRECT_FLAG},
{"rts", "1110", NO_OPERAND, NO_OPERAND},
{"stop", "1111", NO_OPERAND, NO_OPERAND}
};
/* Gets the assembly base2 command code by command name */
/* The function assumes that the command name is valid */
char *getCommandBase2Code(char *command_name)
{
Command the_command;
the_command = getCommandByCommandName(command_name);
return the_command.base2code;
}
/* Gets the allowed addressing modes for operand 1 of the given assembly command name */
/* The function assumes that the command name is valid */
short getSrcOperandAddressingModes(char *command_name)
{
Command command;
command = getCommandByCommandName(command_name);
return command.srcOperandAddressingModes;
}
/* Gets the allowed addressing modes for operand 2 of the given assembly command name */
/* The function assumes that the command name is valid */
short getDstOperandAddressingModes(char *command_name)
{
Command command;
command = getCommandByCommandName(command_name);
return command.dstOperandAddressingModes;
}
/* Gets the command by command name */
/* If the command name is not valid the function returns an empty command (name = NULL) */
Command getCommandByCommandName(char *command_name)
{
int i;
Command command;
command.name = NULL; /*Initialize the name for validation purposes, this is how we know that the command name is not valid*/
for (i=0;i<=NUM_OF_COMMANDS; i++ )
{
if(strcmp(commands[i].name, command_name) == 0)
{
command = commands[i];
}
}
return command;
}
/*Converts an addressing mode number to the addressing mode flag*/
short convertToAddressingModeFlag(short adderssingMode)
{
short flag;
switch(adderssingMode)
{
case IMMEDIATE_ADDRESSING_MODE:
flag = ADR_MODE_IMD_FLAG;
break;
case DIRECT_ADDRESSING_MODE:
flag = ADR_MODE_DIRECT_FLAG;
break;
case INDEX_ADDRESSING_MODE:
flag = ADR_MODE_INDEX_FLAG;
break;
case INDEX2D_ADDRESSING_MODE:
flag = ADR_MODE_INDEX_FLAG;
break;
case REGISTER_ADDRESSING_MODE:
flag = ADR_MODE_REGISTER_FLAG;
break;
default:
flag = NO_OPERAND;
break;
}
return flag;
}
/*Converts the addressing mode integer value to the binary string */
char *getAddressingModeBits(int addressingMode)
{
char *bits;
switch(addressingMode)
{
case IMMEDIATE_ADDRESSING_MODE:
bits = ADR_MODE_IMD_BITS;
break;
case DIRECT_ADDRESSING_MODE:
bits = ADR_MODE_DIRECT_BITS;
break;
case INDEX_ADDRESSING_MODE:
bits = ADR_MODE_INDEX_BITS;
break;
case INDEX2D_ADDRESSING_MODE:
bits = ADR_MODE_INDEX2D_BITS;
break;
case REGISTER_ADDRESSING_MODE:
bits = ADR_MODE_REGISTER_BITS;
break;
default:
break;
}
return bits;
}