PDA

View Full Version : DIV32 Understanding



enauman
- 19th January 2013, 03:20
I have a problem I can't seem to figure out involving the DIV32 command. I am filling an array with 11 successive time measurements. I need to divide a large constant by each member of the array
and output the result over a serial port to my PC.

for i = 0 to 10
velocity = speed[i]
dummy = a * b 'RPM = 240000/velocity
RPM = div32 velocity
speed[i] = RPM
next i
for i = 0 to 10
debug dec speed[i],", "
next i
debug 10,13

goto main

I tried to use:

a con 240
b con 1000

and got screwy results.

If I use:

a var word
b var word
a = 240
b = 1000

I get the right answers.

Why can't I multiply 2 constants together?

I know there is a simple answer. I just don't see it

Normnet
- 19th January 2013, 04:28
It may be best to simply follow the DIV32 "help" example as it is likely designed to use variables in place of constants in it's preceding line.

Norm

enauman
- 19th January 2013, 05:40
Yes. The example is where I got the idea to use variables instead of constants. But my question is WHY can't constants be used.

Normnet
- 19th January 2013, 05:47
Yes. The example is where I got the idea to use variables instead of constants. But my question is WHY can't constants be used.
You are asking the inner workings of the compiler probably in assembly.
Command DIV32 is unique as it is directly linked to its preceding line as part of the command.

Norm

enauman
- 19th January 2013, 06:00
Maybe it should be called out in the command description. It's a trivial point. I was just curious.

Darrel Taylor
- 19th January 2013, 06:08
DIV32 requires certain system variables to have a 32-bit value in them.
They are initialized by a multiplication operation with at least 1 word (the second value can be a constant).
Then a library routine can be executed to do the multiplication.

Part of the optimization of PBP is called "Constant Folding"
If both values in the multiplication are constants ... the compiler multiplies them at "Compile-Time" and uses a single value (a constant).
No multiplication is performed at run-time, and the system variables are never loaded with the 32-bit value.

I've talked about DIV32 and the System vars it uses a few years ago ... http://www.picbasic.co.uk/forum/showthread.php?t=1942

enauman
- 19th January 2013, 17:57
Thanks for the explanation Darrel. Exactly what I was looking for.

Acetronics2
- 19th January 2013, 20:44
Buuuuut ...

The most charming girl this forum ever saw ( of course Melanie ! ) gave us long time ago the way to load those registers with a macro ... if my memory still works fine. :rolleyes: AND ... I might still have a print of that ...

Here's Darrel's edition : http://www.picbasic.co.uk/forum/showthread.php?t=1942 ...

Alain