I'd be remiss if I didn't add this bit of code as well.
This is from my assembler, so, perhaps, more relevant.
Code:
int result = branchInst.getOperand() - (branchInst.getAddress() + 2);
Let's consider this simple use case:
Code:
0200 BNE LABEL
0202 NOP
0203 NOP
0204 LABEL: LDA #0
When my assembler first assembles the BNE line, it stores in the Operand value of the instruction, that actual address of the destination. This is no different than if I were assembling, say, a JMP or an absolute LDA.
The Address value is the address of the instruction.
So, in this case, the values for Address is 0200, and the value for Operand is 0204
From the math, we get: result = 0204 - (0200 + 2), which equals 0204 - 0202, which equals 2.
And that's right!
Code:
0000 0200 .ORIGIN $0200
0001 0200
0002 0200 d0 02 BNE LABEL
0003 0202 ea NOP
0004 0203 ea NOP
0005 0204 a9 00 LABEL LDA #0
0006 0206
Let's try it in reverse.
Code:
0200 LABEL LDA #0
0202 NOP
0203 NOP
0204 BNE LABEL
Here, Operand = 0200, and Address = 0204.
So, result = 0200 - (0204 + 2), which equals 0200 - 0206, which equals -6.
-6 is FA in two's complement.
To convert positive 6 to -6, in two's complement, you negate the value and add 1.
Code:
6 = 0000 0110
Negate = 1111 1001
Add 1 = 1111 1010
FA = 1111 1010
Thus:
Code:
0000 0200 .ORIGIN $0200
0001 0200
0002 0200 a9 00 LABEL LDA #0
0003 0202 ea NOP
0004 0203 ea NOP
0005 0204 d0 fa BNE LABEL
So, this code checks out.
This code is a little different from what the simulators are doing at the instruction level, so this little snippet may be more germane.