Compare
Our comparison instruction has this mnemonic:
CMP reg0, *(reg1), *(reg2)
Registers reg1
and reg2
both contain a pointer to memory. The result of the comparison is saved in register reg0
.
Comparison can be be implemented as a substraction. Let’s say the memory cell that reg1
points to has value val1 and the memory cell reg2
points to value val2. Then we have:
condition | reg0 |
---|---|
val1 > val2 | >0 |
val1 = val2 | 0 |
val1 < val2 | <0 |
…
elif (n3 == 0xF): # CMP reg0, *(reg1), *(reg2)
self.R[n2] = self.mem.peek(self.R[n1]) - self.mem.peek(self.R[n0])
…
Note: This implementation does not take care to wrap this back into the proper bit width.