PDA

View Full Version : Retrieving Div32 Remainder



Darrel Taylor
- 29th July 2003, 00:46
Here's an easy way to get the remainder or Modulus result after a Div32 statement.
This result is not directly available thru PBP commands, but is contained in PBP system var R2.

To retrieve it only takes 1 line and a place to put the word value.


Remainder Var Word

@ MOVE?WW R2, _Remainder ; Get remainder of Div32
The variable Remainder now contains the Modulus of the Div32

Here's an example

Define LOADER_USED 1

' SetUp Hardware USART
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 25 ' 9600 Baud @ 4mhz


Result Var Word
Remainder Var Word
DivideBy Var Word

hserout ["Value = 1,000,000",13,10]
hserout ["DivideBy = ", Dec DivideBy,13,10]

DivideBy = 55
Result = 1000
Result = Result * Result ' create 1,000,000 for Div32
Result = Div32 DivideBy
@ MOVE?WW R2, _Remainder ; Get remainder of Div32

hserout ["Result = ", dec Result,13,10]
hserout ["Remainder = ", dec Remainder,13,10]

' Change remainder to Decimals
Remainder = Remainder * 10000 ' Multiply remainder * 10,000
Remainder = div32 DivideBy ' Divide remainder by original divisor

' Display result with 4 decimal places
Hserout ["Decimal = ", Dec Result,".",dec4 Remainder,13,10,10]


stop

And this is the output via HyperTerminal


Value = 1,000,000
DivideBy = 55
Result = 18181
Remainder = 45
Decimal = 18181.8181

HTH,
Darrel Taylor

Art
- 8th August 2003, 19:47
Hi Darrel,
Result Var Word
... and later...
Result = Result * Result ' create 1,000,000 for Div32

a 32 bit word?
I must look at how the decimal value of 1 million is handled.


A $5 calculator from the shop is beginning to look like a great deal
more than $5 worth of programming. Consider the value you get
when you pay $5 for a calculator with 8 decimal places and a
decimal point thrown in.

My calculator program can add decimal integers so far.
When a key is pressed, the value is shifted into an array that is
rotated.. leading zero blanking makes it look like a calculator too.

The bytes of the work array are limited 0-9, and I have created
my own carry flag for decimal math. Then I can write routines
that do integer math as we do it on paper.

Is that a stupid way to do it?
I assume a real calculator works on the hex array, and converts
the result to decimal only for the silly human to read, as is done
in the example you posted above.
Cheers, Art.

Darrel Taylor
- 10th August 2003, 09:23
No, I don't think that's a stupid way to do it. In fact, it could have some advantages. For one, your not limited by a certain number of bits 8,16,32 Your only limited by the number of digits you can fit in the array. I'll bet the divide routine's going to be a pain in the butt though.


Result Var Word
... and later...
Result = Result * Result ' create 1,000,000 for Div32

a 32 bit word?
I must look at how the decimal value of 1 million is handled.


Actually, 1,000,000 isn't stored in the Result variable. The 32 bit result is stored in PBP system variables for use with DIV32, which then has to be the next statement.

Art
- 12th August 2003, 08:30
Lucky the application is a customised stock counting calc.
that's why no decimals are needed, and neither are div and
subtract routines.
I will have a go at anything I can do on paper though,
and add a decimal place just for division.
Cheers, Art.

Darrel Taylor
- 20th August 2003, 03:53
Well, the more I learn about PBP, the more I look back on my older programs and say to myself "What was I thinking!".

In my example above, I stated that the PBP system variables were not available to PBP commands. enhhhh, wrong answer.

I just found out that all the PBP system vars are just like any other variables. They are defined in the PBPPICxx.RAM file.

Knowing this, getting the remainder of a Div32 is even easier. It's in R2.

So with the previous example, after the
  Result = Div32 DivideBy
you don't need the @ MOVE?WW, The remainder is in R2

Remainder = R2

Still learning,
Darrel