-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
94 lines (75 loc) · 1.67 KB
/
main.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
#include <stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
int main(int argc, char* argv[]) {
//Open the file into a file stream
FILE *file;
if((file = fopen(argv[1], "r")) <= 0){
int retcode = errno;
perror("File error: ");
exit(retcode);
}else{
printf("Successfuly opened file: %s \n", argv[1] );
}
//Obtain the file size to allocate appropriate buffer space
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char buff[size];
printf("Buffisze is: %d\n Reading %s into buffer...\n", size, argv[1]);
size_t length;
//Read file stream into buffer
if((length = fread(buff, 1, size, file)) <= 0){
perror("Buffer input error: ");
int retcode = errno;
exit(errno);
}else{
printf("Buffer read into Memory successfully!\n Amount of bytes read: %d", length);
}
//While loop for parsing buffer
int cycle_count = 0;
int i = 0;
while(i != size){
printf("0x%x: ", i);
switch(buff[i]){
//opcode mappings
case 0x00:
printf("NOP\n");
i++;
break;
case 0x01:
printf("LXI %x, %x\n", buff[i+1], buff[i+2]);
i = i+3;
break;
case 0x02:
printf("STAX B\n");
i++;
break;
case 0x03:
printf("INX B\n");
i++;
break;
case 0x04:
printf("INR B");
i++;
break;
case 0x05:
break;
case 0xc3:
printf("JMP $%x%x\n", buff[i+2], buff[i+1]);
i = i+3;
break;
case EOF:
printf("NULL TERMINATOR, ENDING DISASSEMBLY");
break;
//Catch all default for unknown cases
default:
printf("Unknown opcode detected: %x exiting\n", buff[i]);
exit(0);
break;
}
}
return 0;
}