-
Notifications
You must be signed in to change notification settings - Fork 0
/
condition.cpp
95 lines (71 loc) · 5.18 KB
/
condition.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Created by erthax on 15.01.2021.
//
#include "instruction_generators.h"
//______________________________EqualCondition_________________________________
void EqualCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Register* secondHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value1); // conditionRegister has value1
Command::loadValueToRegister(firstHelperRegister, value2); // firstHelperRegister has value2
code->reset(secondHelperRegister->getName());
code->add(secondHelperRegister->getName(), conditionRegister->getName()); // secondHelperRegister has value1 copy
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // conditionRegister = value1 - value2
code->sub(firstHelperRegister->getName(), secondHelperRegister->getName()); // firstHelperRegister = value2 - value1
code->add(conditionRegister->getName(), firstHelperRegister->getName()); // if conditionRegister = 0 its ok.
//here we add jzero that does commands specified in command if it hits zero
code->jzero(conditionRegister->getName(), 2);
}
//______________________________DiffCondition_________________________________
void DiffCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Register* secondHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value1); // conditionRegister has value1
Command::loadValueToRegister(firstHelperRegister, value2); // firstHelperRegister has value2
code->reset(secondHelperRegister->getName());
code->add(secondHelperRegister->getName(), conditionRegister->getName()); // secondHelperRegister has value1 copy
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // conditionRegister = value1 - value2
code->sub(firstHelperRegister->getName(), secondHelperRegister->getName()); // firstHelperRegister = value2 - value1
code->add(conditionRegister->getName(), firstHelperRegister->getName()); // if conditionRegister = 0 its NOT ok.
//here we add jzero that skips commands specified in command if it hits zero
code->jzero(conditionRegister->getName(), 2);
code->jump(2); // this jumps over big jump that skips all commands if jzero before didn't execute
}
//____________________________LessThanCondition________________________________
void LessThanCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value2); // conditionRegister has value2
Command::loadValueToRegister(firstHelperRegister, value1); // firstHelperRegister has value1
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // firstHelperRegister = value2 - value1
//here we add jzero that skips commands specified in command if it hits zero
code->jzero(conditionRegister->getName(), 2);
code->jump(2);// this jumps over big jump that skips all commands if jzero before didn't execute
}
//____________________________GreaterThanCondition_______________________________
void GreaterThanCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value1); // conditionRegister has value1
Command::loadValueToRegister(firstHelperRegister, value2); // firstHelperRegister has value2
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // firstHelperRegister = value1 - value2
//here we add jzero that skips commands specified in command if it hits zero
code->jzero(conditionRegister->getName(), 2);
code->jump(2);// this jumps over big jump that skips all commands if jzero before didn't execute
}
//____________________________LessEqualCondition_______________________________
void LessEqualCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value1); // conditionRegister has value1
Command::loadValueToRegister(firstHelperRegister, value2); // firstHelperRegister has value2
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // firstHelperRegister = value1 - value2
//here we add jzero that skips jump that skips all commands if its zero
code->jzero(conditionRegister->getName(), 2);
}
//____________________________GreaterThanCondition_______________________________
void GreaterEqualCondition::generateConditionValue(Register *conditionRegister) {
Register* firstHelperRegister = memory->getFreeRegister();
Command::loadValueToRegister(conditionRegister, value2); // conditionRegister has value2
Command::loadValueToRegister(firstHelperRegister, value1); // firstHelperRegister has value1
code->sub(conditionRegister->getName(), firstHelperRegister->getName()); // firstHelperRegister = value2 - value1
//here we add jzero that skips jump that skips all commands if its zero
code->jzero(conditionRegister->getName(), 2);
}