03-26-2019, 11:48 PM
Here's a nice little program to handle instruction sets that have a signed offset in the lower bits of an instruction. Suppose you're working with a disassembly listing and need to mask the offset in a branch instruction and then add it to the program counter value. You'll need to sign extend the offset first if it happens to be negative.
A good example would be the PDP-11 branch instructions. The high eight bits of the instruction encode the branch type while the lower eight bits are a signed offset that gets multiplied by two and added to the PC for the target address. The PC has been incremented by the time the offset is added to form the new PC value.
Usage:
Enter the instruction followed by the number of bits in the offset. Press GSB A. The signed offset will be in X. You can now use the signed offset and incremented program counter value to calculate the target address of the branch instruction. Don't forget to Shift Left the offset first!
Before:
Y - Instruction
X - # of bits in offset
After
X - Masked and sign extended offset
Example (PDP-11)
Set word size to 16, mode to Hex.
BNE $+4 ; offset would be 3
Enter 0203h, 8h
Result is 3h
BEQ $-4 ; offset would be -5
Enter 03FBh, 8h
Result is FFFBh (-5)
A good example would be the PDP-11 branch instructions. The high eight bits of the instruction encode the branch type while the lower eight bits are a signed offset that gets multiplied by two and added to the PC for the target address. The PC has been incremented by the time the offset is added to form the new PC value.
Usage:
Enter the instruction followed by the number of bits in the offset. Press GSB A. The signed offset will be in X. You can now use the signed offset and incremented program counter value to calculate the target address of the branch instruction. Don't forget to Shift Left the offset first!
Before:
Y - Instruction
X - # of bits in offset
After
X - Masked and sign extended offset
Example (PDP-11)
Set word size to 16, mode to Hex.
BNE $+4 ; offset would be 3
Enter 0203h, 8h
Result is 3h
BEQ $-4 ; offset would be -5
Enter 03FBh, 8h
Result is FFFBh (-5)
Code:
(from DM16L)
001 LBL A | 43 22 A Mask & Sign Extend
002 STO I | 44 32 Bit count
003 0 | 0 Mask
004 NOT | 42 30 All ones
005 LBL 0 | 43 22 0
006 SL | 42 A Shift n 0's for mask
007 DSZ | 43 23
008 GTO 0 | 22 0
009 x<>y | 34 Retrieve # of bits
010 1 | 1 Decrement for bit #
011 - | 30 to test
012 Rv | 33 Swap mask and instruction
013 x<>y | 34
014 R^ | 43 33
015 B? | 42 6 High bit set?
016 GTO 1 | 22 1 No, high bit clear
017 x<>y | 34 Bring mask back to X
018 NOT | 42 30
019 AND | 42 20 Mask off high bits
020 RTN | 43 21
021 LBL 1 | 43 22 1
022 OR | 42 40 Set all high bits for sign
023 RTN | 43 21