riscemu.types.instruction module
- class riscemu.types.instruction.Immediate(abs_value: Union[int, Int32], pcrel_value: Union[int, Int32])
Bases:
object
This class solves a problem of ambiguity when interpreting assembly code.
- Let’s look at the following four cases (assuming 1b is 16 bytes back):
beq a0, a1, 1b // conditional jump 16 bytes back
beq a0, a1, -16 // conditional jump 16 bytes back
addi a0, a1, 1b // subtract (pc - 16) from a1
addi a0, a1, -16 // subtract 16 from a1
We want a and b to behave the same, but c and d not to.
The Immediate class solves this problem, by giving each instruction two ways of interpreting a given immediate. It can either get the “absolute value” of the immediate, or it’s relative value to the PC.
In this case, the beq instruction would interpret both as PC relative values, while addi would treat them as absolute values.