Post Reply 
(16C) Mask and Sign Extend
03-26-2019, 11:48 PM
Post: #1
(16C) Mask and Sign Extend
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)

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

Remember kids, "In a democracy, you get the government you deserve."
Find all posts by this user
Quote this message in a reply
04-09-2019, 09:38 AM
Post: #2
RE: (16C) Mask and Sign Extend
Just a hint:

The HP-16C has a nice feature to generate bit mask. Instead of the loop in your program (lines 002 -008) you could simply use
Code:

  Enter
f MASKR
f NOT

Hartmut
Find all posts by this user
Quote this message in a reply
04-09-2019, 07:25 PM
Post: #3
RE: (16C) Mask and Sign Extend
Thanks for the hint, wynen! Here then is a shorter, simpler version of the original program. This is slightly different than the original in that you specify which bit is the sign bit rather than how many bits are in the offset. In both cases though, the offset is assumed to be in the lower bits of the instruction word.

Usage:
Enter the instruction followed by the bit number of the offset sign. 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.

Before:
Y - Instruction
X - bit # of the offset sign bit

After
X - Masked and sign extended offset

Example (PDP-11)
Set word size to 16, mode to Hex. The offset is the lower 8 bits of the instruction, so the sign bit would be bit 7.

BNE $+4 ; offset would be 3
Enter 0203h, 7h
Result is 3h

BEQ $-4 ; offset would be -5
Enter 03FBh, 7h
Result is FFFBh (-5)

Code:

001  LBL A        | 43 22 A      Mask & Sign Extend
002  MASKR        |    42 8      Bit # of ones
003  x<>y         |       34     X is instruction
004  LST X        |    43 36     Bit number of sign
005  B?           |    42 6      Sign bit high?
006  GTO 0        |    22 0      Yes
007  AND          |    42 20     Mask offset
008  RTN          |    43 21
009  LBL 0        | 43 22 0      Sign bit high
010  x<>y         |       34     Get mask
011  NOT          |    42 30     sign bit extended
012  OR           |    42 40
013  RTN          |    43 21

Remember kids, "In a democracy, you get the government you deserve."
Find all posts by this user
Quote this message in a reply
04-12-2019, 08:33 PM
Post: #4
RE: (16C) Mask and Sign Extend
Sorry if this is off-topic, but are you debugging PDP-11 assembly language?
Find all posts by this user
Quote this message in a reply
04-13-2019, 02:32 AM
Post: #5
RE: (16C) Mask and Sign Extend
I spent a couple of years as a graduate student programming in PDP-11 assembly and teaching an undergraduate lab in the same. Afterwards, spent a few years with 680x0 assembly. Having recently built a PiDP-11 I'm considering getting back into RT-11/RSX-11 programming. It would probably take all of five minutes to refresh my memory of the instruction set Smile

The program could just as easily be used with other CPUs including the 41 Nut branch instructions...

~Mark

Remember kids, "In a democracy, you get the government you deserve."
Find all posts by this user
Quote this message in a reply
04-16-2019, 01:56 AM
Post: #6
RE: (16C) Mask and Sign Extend
I keep going back looking at the PiDP-11, but while it is cool I have never used one and struggle to find motivate myself why I really need it. But I keep going back, looking and checking the forum about it.

While it is cool to look at and playing with the root of UNIX, any modern Linux or *BSD is probably a better choice for fun and they are easily found and dirt cheap today. If I played with the another OS on it like RT-11 it would be different.

Still, I found this interesting book (Lions' Commentary on UNIX 6th Edition, with Source Code) that I never heard of before while pondering over this: http://www.lemis.com/grog/Documentation/Lions/
It was "new" to me, so maybe it is new to others too?

There is a PiDP-10 in the works it seems, I have used those back in the days, but still... and the PDP-11 is way cooler looking and more historically significant.

In the end I always come down to not buying the PiDP-11. The little time I can spare is probably better to do with the HP-41 and AROS. Besides, I can always play with simh for a start on the RPi and that Lion's book interests me.

It is always good to see HP-16C programs, they are far between unfortunately. I could not resist making my version of it with Ladybug, but that is HP-41 so it is off-topic here. Maybe I post it later elsewhere.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)