15 void Computer::executeADD(
const InstructionWord& instruction) {
18 int address = getIndexedAddress(instruction,
true);
19 copyToRegister5(instruction,
memory[address], &word);
20 int32_t valueM = word.value();
21 int32_t result = valueA + valueM;
22 rA.
set(checkRange(result));
29 void Computer::executeSUB(
const InstructionWord& instruction) {
32 int address = getIndexedAddress(instruction,
true);
33 copyToRegister5(instruction,
memory[address], &word);
34 word.negative = !word.negative;
35 int32_t valueM = word.value();
36 int32_t result = valueA + valueM;
37 rA.
set(checkRange(result));
44 void Computer::executeMUL(
const InstructionWord& instruction) {
47 int address = getIndexedAddress(instruction,
true);
48 copyToRegister5(instruction,
memory[address], &word);
49 int32_t valueM = word.value();
50 int64_t result =
static_cast<int64_t
>(valueA) *
static_cast<int64_t
>(valueM);
51 rA.
set(result / (1 << 30));
52 rX.
set(result % (1 << 30));
63 void Computer::executeDIV(
const InstructionWord& instruction) {
64 int32_t valueA = std::abs(
rA.
value());
65 int32_t valueX = std::abs(
rX.
value());
66 int64_t dividend = (
static_cast<int64_t
>(valueA) << 30) +
static_cast<int64_t
>(valueX);
71 int address = getIndexedAddress(instruction,
true);
72 copyToRegister5(instruction,
memory[address], &word);
73 int32_t divisor = word.value();
75 throw RuntimeError(_lineOffset,
"Divisor cannot be 0");
77 int64_t quotient = dividend / divisor;
78 if (std::abs(quotient) >= (1 << 30)) {
80 quotient %= (1 << 30);
82 int32_t remainder = dividend % divisor;
83 rA.
set(
static_cast<int32_t
>(quotient));