Bit fiddling on the HP33s



#2

Bit fiddling on the HP33s

One of the omissions on the 33s is bit fiddling commands - and, or, not. This is odd, since numbers in bases 16, 8, and 2 can be entered and used. So, I decided to fix this.

The routines shown here work on 32 bit unsigned integers, though any number of bits (up to 35) can be used. The method used is to get the corresponding bits of the 2 words to be operated upon, get a value from 0 to 3, and use that as a lookup in a 4 bit table. Separate tables are used for and, or, and not. Any 4 bit table can be used.

The routine uses the integer divide and remainder operations on the 33s; I do not know if they are on the 32sii.

The routine uses only integer values, so it can be run in hex, oct, or bin mode. However, you have to be in decimal mode when entering the "y^x" command.

The code uses lbl H to return the number of bits to process.

The registers used are:

V: First operand
U: Second operand
T: 4 bit table
S: 2^k for k from 31 to 0
R: Result

Usage:

And: V U xeq A
Or: V U xeq O
Not: U xeq N

Result in X, U in Y, and V in Z (so you can roll down to get parameters)

General; V U table xeq B

Code: (comments indicated by "//")

And:
LBL A
8 // decimal - 1000 binary
gto B

Or:
LBL O
14 // decimal - 1110 binary
gto B

Not:
LBL N
5 // decimal - 0101 binary
gto B

Number of bits:
LBL H
16 // or 8 or 32 or ...
rtn

Bit fiddler:

LBL B
sto T // the 4 bit table
Rv // roll down
abs sto U // make it unsigned
Rv abs sto V // got the operands and table
2 xeq H 1 - y^x sto S // bit extractor
0 sto R // the eventual result

LBL C // start of the loop
rcl V rcl S int/ // integer part of V/2^k
2 rmdr 2 * // 2 times the bit
rcl U rcl S int/ 2 rmdr // same for U
+ // 2*v_bit + u_bit
2 x<>y y^x // 2^u_v value
rcl T x<>y int/ // x<>y could be saved if T loaded earlier
2 rmdr // that's the bit
rcl* S sto+ R // scale up and store
rcl S 2 int/ sto S // get next lower power of 2
x>0? gto C // if anything there, continue
rcl V rcl U rcl R // load inputs and output
rtn // done

Possible improvements/enhancements:

1) Get 2 bits at a time, and use a 16-bit table. This would cut the running time by about 50% - it is now about 4 seconds on a 33s.

2) have 3 or more parameters - an arbitrary truth table of up to 5 parameters (n parameters takes 2^n bits) could be handeled.

My usual statement: All comments and suggestions will be considered.

One nice thing about the 33s - you can't run out of program space - you will run out of labels first!

Martin Cohen
5/4/04

Note added later: I have written a 2-bit version, as mentioned in (1) above. If anyone is interested, I can post it.


Possibly Related Threads…
Thread Author Replies Views Last Post
  bit manips on WP 34S Kiyoshi Akima 8 2,385 10-06-2013, 06:25 AM
Last Post: Paul Dale
  Where to the 32-bit version of User Code Utiltiy for HP-41 ? Olivier (Wa) 2 1,481 09-26-2013, 01:55 AM
Last Post: Olivier (Wa)
  OT: Simulating a TI calculator with crazy 11-bit opcodes Egan Ford 8 2,592 08-13-2013, 12:06 AM
Last Post: Paul Dale
  WP-34S Overlay - making it a bit more permanent? Marcel Samek 1 1,084 07-05-2013, 09:02 PM
Last Post: htom trites jr
  HP-35--then and a wee bit later Matt Agajanian 1 893 04-12-2012, 07:52 AM
Last Post: Jeff O.
  WP34 S: Fonts (a bit OT) Marcus von Cube, Germany 18 4,418 02-24-2012, 11:07 AM
Last Post: Luiz C. Vieira (Brazil)
  32-bit MCODE tool chain for the HP41 incl. D41 now MichaelG 4 1,962 02-12-2012, 08:57 PM
Last Post: Kerem Kapkin (Silicon Valley, CA)
  32-bit version of User Code Utiltiy for HP-41 MichaelG 4 1,708 02-08-2012, 07:12 AM
Last Post: MichaelG
  Interest in 32-bit version of User Code Utiltiy for HP-41 MichaelG 16 3,934 01-30-2012, 02:01 PM
Last Post: MichaelG
  HP-41 MCode Assembler/Linker 32-bit version MichaelG 19 4,742 01-22-2012, 12:44 PM
Last Post: MichaelG

Forum Jump: