-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (60 loc) · 1.42 KB
/
main.cpp
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
/*
* main.cpp
*
* Created on: Apr 9, 2014
* Author: Pimenta
*/
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;
typedef int32_t word_t;
typedef uint32_t uword_t;
#ifndef MEM_WORDS
#define MEM_WORDS 0x2000
#endif
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage mode: subleq-sim <meminit_file>\n");
return 0;
}
uword_t* mem = new uword_t[MEM_WORDS];
// read data
ifstream f(argv[1]);
for (uword_t words = 0; ; words++) {
string tmp;
// skip address
f >> tmp;
if (tmp == "END;") {
break;
}
while (f.get() != ':');
f.get();
// read word
tmp = "";
for (char c = f.get(); c != ';'; c = f.get())
tmp += c;
sscanf(tmp.c_str(), "%x", &mem[words]);
}
f.close();
// run
uword_t A, B, J;
word_t sub;
uint64_t instructions = 0;
for (uword_t ip = 0; ; ip = sub <= 0 ? J : ip) {
if (mem[ip] == mem[ip + 1] && mem[ip + 2] == ip) {
break;
}
A = mem[ip++];
B = mem[ip++];
J = mem[ip++];
sub = mem[B] - mem[A];
printf("0x%08X: A=0x%08X(0x%08X) B=0x%08X(0x%08X) J=0x%08X sub=0x%08X\n", ip - 3, A, mem[A], B, mem[B], J, sub);
mem[B] = sub;
instructions++;
}
printf("Instructions executed: %lld\n", instructions);
delete[] mem;
return 0;
}