-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.c
154 lines (144 loc) · 3.42 KB
/
code.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
#include "code.h"
#include "parser.h"
//Takes a dest token and returns binary string
void getBinDest(char* in, char* out)
{
strcpy(out, "000");
if(in == NULL || strcmp(in, "") == 0)
return;
int i;
for(i = 0; i < 3; i++)
{
if(in[i] == '\0')
return;
if(in[i] == 'A')
out[0] = '1';
if(in[i] == 'D')
out[1] = '1';
if(in[i] == 'M')
out[2] = '1';
}
}
//Takes a jump token and returns binary string
void getBinJump(char* in, char* out)
{
if(in == NULL || strcmp(in, "") == 0)
strcpy(out, "000");
else if(strcmp(in, "JGT") == 0)
strcpy(out, "001");
else if(strcmp(in, "JEQ") == 0)
strcpy(out, "010");
else if(strcmp(in, "JGE") == 0)
strcpy(out, "011");
else if(strcmp(in, "JLT") == 0)
strcpy(out, "100");
else if(strcmp(in, "JNE") == 0)
strcpy(out, "101");
else if(strcmp(in, "JLE") == 0)
strcpy(out, "110");
else if(strcmp(in, "JMP") == 0)
strcpy(out, "111");
}
//Takes a comp token and returns binary string
void getBinComp(char* in, char* out)
{
if (strchr(in, 'M') == NULL)
{
if(strcmp(in, "0") == 0)
strcpy(out, "0101010");
else if(strcmp(in, "1") == 0)
strcpy(out, "0111111");
else if(strcmp(in, "-1") == 0)
strcpy(out, "0111010");
else if(strcmp(in, "D") == 0)
strcpy(out, "0001100");
else if(strcmp(in, "A") == 0)
strcpy(out, "0110000");
else if(strcmp(in, "!D") == 0)
strcpy(out, "0001101");
else if(strcmp(in, "!A") == 0)
strcpy(out, "0110001");
else if(strcmp(in, "-D") == 0)
strcpy(out, "0001111");
else if(strcmp(in, "-A") == 0)
strcpy(out, "0110011");
else if(strcmp(in, "D+1") == 0)
strcpy(out, "0011111");
else if(strcmp(in, "A+1") == 0)
strcpy(out, "0110111");
else if(strcmp(in, "D-1") == 0)
strcpy(out, "0001110");
else if(strcmp(in, "A-1") == 0)
strcpy(out, "0110010");
else if(strcmp(in, "D+A") == 0)
strcpy(out, "0000010");
else if(strcmp(in, "D-A") == 0)
strcpy(out, "0010011");
else if(strcmp(in, "A-D") == 0)
strcpy(out, "0000111");
else if(strcmp(in, "D&A") == 0)
strcpy(out, "0000000");
else if(strcmp(in, "D|A") == 0)
strcpy(out, "0010101");
}
else
{
if(strcmp(in, "M") == 0)
strcpy(out, "1110000");
else if(strcmp(in, "!M") == 0)
strcpy(out, "1110001");
else if(strcmp(in, "-M") == 0)
strcpy(out, "1110011");
else if(strcmp(in, "M+1") == 0)
strcpy(out, "1110111");
else if(strcmp(in, "M-1") == 0)
strcpy(out, "1110010");
else if(strcmp(in, "D+M") == 0)
strcpy(out, "1000010");
else if(strcmp(in, "D-M") == 0)
strcpy(out, "1010011");
else if(strcmp(in, "M-D") == 0)
strcpy(out, "1000111");
else if(strcmp(in, "D&M") == 0)
strcpy(out, "1000000");
else if(strcmp(in, "D|M") == 0)
strcpy(out, "1010101");
}
}
//Takes an address and returns binary string
void getBinAddress(char* in, char* out)
{
int n = atoi(in);
int i;
for(i=14; i >= 0; i--)
{
if(n & 1)
out[i] = '1';
else
out[i] = '0';
n = n >> 1;
}
out[15] = '\0';
}
//Outputs binary A command
void getABinary(char* addr, char* out)
{
out[0] = '0';
getBinAddress(addr, out+1);
}
//Outputs a binary C command
void getCBinary(char* dest, char* comp, char* jump, char* out)
{
out[0] = out[1] = out[2] = '1';
getBinComp(comp, out+3);
getBinDest(dest, out+10);
getBinJump(jump, out+13);
}
//Outputs a binary command from a ParsedCommand struct
void getBinOut(struct ParsedCommand* p, char* out)
{
if(p->ct == C_TYPE)
getCBinary(p->dest, p->comp, p-> jump, out);
else
getABinary(p->memLocation, out);
}