-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooasm_example.cc
133 lines (117 loc) · 4.42 KB
/
ooasm_example.cc
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
#include "ooasm.h"
#include "computer.h"
#include <string>
#include <sstream>
#include <cassert>
// Funkcja z rafała.
namespace {
std::string memory_dump(Computer const& computer) {
std::stringstream ss;
computer.memory_dump(ss);
// std::cout << ss.str() << "\n";
return ss.str();
}
}
int main()
{
auto lang_move = program({
mov(mem(num(0)), num(42))});
auto lang_data = program({
inc(mem(lea(("a")))),
data(("a"), num(0)),
data(("b"), num(2)),
data(("c"), num(3))});
auto lang_operations = program({
data(("a"), num(4)),
data(("b"), num(3)),
data(("c"), num(2)),
data(("d"), num(1)),
add(mem(lea(("a"))), mem(lea(("c")))),
sub(mem(lea(("b"))), mem(lea(("d")))),
mov(mem(lea(("c"))), num(0)),
mov(mem(lea(("d"))), num(0))});
auto lang_helloworld = program({
mov(mem(mem(num(10))), num('h')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('e')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('l')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('l')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('o')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num(' ')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('w')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('o')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('r')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('l')),
inc(mem(num(10))),
mov(mem(mem(num(10))), num('d'))});
Computer computer1(1);
computer1.boot(lang_move);
assert(memory_dump(computer1) == "42 ");
Computer computer2(3);
computer2.boot(lang_data);
assert(memory_dump(computer2) == "1 2 3 ");
Computer computer3(4);
computer3.boot(lang_operations);
assert(memory_dump(computer3) == "6 2 0 0 ");
Computer computer4(11);
computer4.boot(lang_helloworld);
assert(memory_dump(computer4) == "104 101 108 108 111 32 119 111 114 108 100 ");
try {
Computer computer5(10);
computer5.boot(lang_helloworld);
assert(false);
}
catch(std::exception e)
{
assert(true);
}
try
{
auto lang_id = program({data((""), num(4))});
Computer computer6(10);
computer6.boot(lang_id);
assert(false);
}
catch(std::exception e)
{
assert(true);
}
try
{
auto lang_long_id = program({data(("1234567890p"), num(4))});
Computer computer7(10);
computer7.boot(lang_long_id);
assert(false);
}
catch(std::exception e)
{
assert(true);
}
auto wrong_id = program({add(mem(lea(("a"))),
mem(lea(("c")))),
data(("2137"), num(4))});
try {
Computer computer8(10);
computer8.boot(wrong_id);
assert(false);
}
catch(std::exception e)
{
assert(true);
}
auto twicer = program({
inc(mem(lea(("a")))),
data(("a"), num(0))});
Computer computer9(1);
computer9.boot(twicer);
computer9.boot(twicer);
assert(memory_dump(computer9) == "1 ");
}