-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembler.c
161 lines (142 loc) · 4.55 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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include "assembler.h"
void generate_opcodes(struct opcode opcodes[])
{
opcodes[0].value = 0b00000;
opcodes[1].value = 0b00001;
opcodes[2].value = 0b00010;
opcodes[3].value = 0b00011;
opcodes[4].value = 0b00100;
opcodes[5].value = 0b00101;
opcodes[6].value = 0b00110;
opcodes[7].value = 0b00111;
opcodes[8].value = 0b01001;
opcodes[9].value = 0b01010;
opcodes[10].value = 0b01011;
opcodes[11].value = 0b01100;
opcodes[12].value = 0b001101;
opcodes[13].value = 0b101101;
opcodes[14].value = 0b01110;
opcodes[15].value = 0b01111;
opcodes[16].value = 0b10000;
opcodes[17].value = 0b10001;
opcodes[18].value = 0b10010;
opcodes[19].value = 0b10011;
opcodes[20].value = 0b10100;
opcodes[21].value = 0b10101;
opcodes[22].value = 0b10110;
opcodes[23].value = 0b10111;
opcodes[0].key = "STOP"; //stop
opcodes[1].key = "TR"; //transfer
opcodes[2].key = "TR OV"; //transfer on overflow
opcodes[3].key = "TR +"; // Transfer on plus
opcodes[4].key = "TR 0"; // Transfer on 0
opcodes[5].key = "SUB"; // Subtract
opcodes[6].key = "R SUB"; // Reset and subtract
opcodes[7].key = "SUB AB"; // Subtract absolute value
opcodes[8].key = "ADD"; // Add
opcodes[9].key = "R ADD"; // Reset and add
opcodes[10].key = "ADD AB"; // Add absolute
opcodes[11].key = "STORE"; // Store
opcodes[12].key = "STORE A"; // Store address
opcodes[13].key = "EXTR"; // Extract
opcodes[14].key = "STORE MQ"; // Store MQ
opcodes[15].key = "LOAD MQ"; // Load MQ
opcodes[16].key = "MPY"; // Multiply
opcodes[17].key = "MPY ROUND"; //Mutiply and Round
opcodes[18].key = "DIV"; // Divide
opcodes[19].key = "ROUND"; //Round
opcodes[20].key = "L LEFT"; // Long left shift
opcodes[21].key = "L RIGHT"; // Long right shift
opcodes[22].key = "A LEFT"; // Accumulator left shift
opcodes[23].key = "A RIGHT"; // Accumulator right shift
opcodes[0].key_size = 4;
opcodes[1].key_size = 2;
opcodes[2].key_size = 5;
opcodes[3].key_size = 4;
opcodes[4].key_size = 4;
opcodes[5].key_size = 3;
opcodes[6].key_size = 5;
opcodes[7].key_size = 6;
opcodes[8].key_size = 3;
opcodes[9].key_size = 5;
opcodes[10].key_size = 6;
opcodes[11].key_size = 5;
opcodes[12].key_size = 7;
opcodes[13].key_size = 4;
opcodes[14].key_size = 8;
opcodes[15].key_size = 7;
opcodes[16].key_size = 3;
opcodes[17].key_size = 9;
opcodes[18].key_size = 3;
opcodes[19].key_size = 5;
opcodes[20].key_size = 6;
opcodes[21].key_size = 7;
opcodes[22].key_size = 6;
opcodes[23].key_size = 7;
}
void create_instruction(struct program_counter *pc, int* Machine_Memory, struct opcode command_opcode,int address)
{
int *instruction = Machine_Memory;
int pow = 1;
for(int i=0; i<OPCODE_SIZE; i++)
{
instruction[i] = (command_opcode.value & pow ) >> i;
pow*=2;
}
int* address_array = create_byte_value(address,INSTRUCTION_SIZE-OPCODE_SIZE);
for(int i=OPCODE_SIZE; i<INSTRUCTION_SIZE; i++)
instruction[i] = address_array[i-OPCODE_SIZE];
for(int i=INSTRUCTION_SIZE; i<WORD_SIZE; i++)
instruction[i] = 0;
}
void display_opcodes(struct opcode opcodes[])
{
int i=0;
printf("Opcode : Binary value\n");
for( i=0; i<NUM_OPCODES; i++)
{
printf("%s : ",opcodes[i].key);
print_binary(opcodes[i].value);
printf("\n");
}
}
void print_binary(int value)
{
int i=0;
for( i=7; i>=0; i--)
{
printf("%d",(value & (1 << i)) >> i);
}
}
struct opcode get_opcode(char* code, struct opcode* opcodes, int in_size)
{
int total_length = 0;
int temp_length = 0;
struct opcode out_opcode;
out_opcode.value = -1;
out_opcode.key = "null";
for(int i=0; i<NUM_OPCODES; i++)
{
if(in_size < opcodes[i].key_size)
continue;
temp_length = 0;
for(int j=0; j<opcodes[i].key_size; j++)
{
if(opcodes[i].key[j] == code[j])
temp_length++;
else
break;
}
if(temp_length > total_length && temp_length == opcodes[i].key_size && in_size > temp_length+1)
{
total_length = temp_length;
out_opcode = opcodes[i];
}
}
return out_opcode;
}