Shop OBEX P1 Docs P2 Docs Learn Events
32 bit math (input 32 bit value from keypad) what''s wrong? — Parallax Forums

32 bit math (input 32 bit value from keypad) what''s wrong?

ArchiverArchiver Posts: 46,084
edited 2003-10-01 04:53 in General Discussion
From: <azeasi@a...>


I should probably type in the code before committing myself, but offhand I'd
say that:

> x0=9999
> x1=8999
> y0=1
> y1=0
>
> gosub add1616
> debug dec z1,dec z0
>
would yield:

100008999

The problem lies in trying to simply type in the decimal digits to separate
the value of X into two sixteen-bit words. If you want to test the addition
algorithm, you must initialize X0 and X1 properly.

X1=89,999,999 modulo 65536
X0=89,999,999 rem 65536

Darned if I can remember the function keys to do that automatically on my
calculator, but I think it's:

X1=1373
X0=19071


Now set Y1=0 and Y0=1 and try the algorithm again.

You should get Z1=1373 and Z1=19072 because there is no carry with the
values you chose. Not in sixteen bit world anyway, only in the decimal form.
(You still can't display the result properly using dec(Z1),dec(Z0), but
that's a whole different problem.)

If you want to test the carry function start with something like 90,046,463
which loads into the X words as:

X1=1373
X0=65535

When you add one to that, you will get:

Z1=1374
Z0=0

Going back to your original question, you said you were trying to take
keyboard input and create a 32-bit value. You can't do that with only
addition, so you need more of Allen Tracy's algorithms than this one. You
need to do multiplies as well.

First, initialize X1:X0 to zero.
Then, as each keystroke is read, multiply the current value of X by 10. THEN
add the value of the new digit.
Check for overflow and somehow signal an error if necessary.
Go back to read another key until you receive the key that indicates
completion.

Obviously, that is a 32-bit multiply _and_ a 32-bit addition in step two..

Gary
Sign In or Register to comment.