# Board index » delphi » Fixed point numbers and 32-bit numbers(longints)

## Fixed point numbers and 32-bit numbers(longints)

Hi there!

I hope there someone who has been crazy enough to do this before!

I hope you understand the term fixed point numbers!
This is where u use a 32bit number to represent a "real" number!
You use the top 16.bits for the integer part and the bottum 16-bits for the
fraction part!
The advantage is that you can do fairly accurate maths without the maths
co-processor!
I.E. FASTER

Know I have a bit off a problem!
How do I multiply 2 off these fixed point numbers!
And how do I divide them!

I use turbo pascal 7.0 wich is a 16-bit programming language!
So it doesn't make use off the extended registers!
So if I like to use extended registers I must use NASM and use the inline
function!

Does someone know how pascal uses 32-bit numbers in it's code.

How do I go about writing a object file with nasm?

http://homes.arealcity.com/Heinrichshomepage/

Heinrich

## Re:Fixed point numbers and 32-bit numbers(longints)

In article <81bqok\$56...@ctb-nnrp1.saix.net>,

##### Quote
NLHG <Heinr...@mailroom.com> wrote:
>Hi there!

>I hope there someone who has been crazy enough to do this before!

>I hope you understand the term fixed point numbers!
>This is where u use a 32bit number to represent a "real" number!
>You use the top 16.bits for the integer part and the bottum 16-bits for the
>fraction part!
>The advantage is that you can do fairly accurate maths without the maths
>co-processor!
>I.E. FASTER

>Know I have a bit off a problem!
>How do I multiply 2 off these fixed point numbers!
>And how do I divide them!

Well if you want fast then Inline is the way to go. Assuming the numbers
are unsigned one can use the following:

type fixed=longint;
conbst overflow:word=0;

Function mul(x,y:fixed):fixed;
inline(
\$66/\$5B/              {POP     EBX}
\$66/\$58/              {POP     EAX}
\$66/\$F7/\$E3/          {MUL     EBX}
\$66/\$C1/\$E8/\$10/      {SHR     EAX,10H}

\$66/\$89/\$D3/          {MOV     EBX,EDX}
\$66/\$C1/\$EB/\$10/      {SHR     EBX,10H}
\$09/\$1E/Overflow  {OR      Overflow,BX}
);

Function dvd(x,y:fixed):fixed;
inline(
\$66/\$5B/              {POP     EBX}
\$66/\$31/\$D2/          {XOR     EDX,EDX}
\$58/              {POP     AX}
\$5A/              {POP     DX}
\$66/\$C1/\$E0/\$10/      {SHL     EAX,10H}
\$66/\$F7/\$F3/          {DIV     EBX}
\$66/\$0F/\$A4/\$C2/\$10   {SHLD    EDX,EAX,10h}
);

Division gives RTE200 if the result does not fit into the range so no
extra overflow information is carried out like with multiplication. (if
there is overflow in multiplication the overflow will be >0).

Sometimes one needs to do division after multiplication. If one writes
a separate routine for it the intermediate result can freely overflow
provided that the final results is in the range (otherwise one gets
RTE200).

Function muldiv(x,y,z:fixed):fixed;
inline(
\$66/\$59/              {POP     ECX}
\$66/\$5B/              {POP     EBX}
\$66/\$58/              {POP     EAX}
\$66/\$F7/\$E3/          {MUL     EBX}
\$66/\$F7/\$F1/          {DIV     ECX}
\$66/\$0F/\$A4/\$C2/\$10   {SHLD    EDX,EAX,10h}
);

That is of course x*y/z

The last btw works also on normal longints provided they are positive (or
unsigned)

##### Quote

>I use turbo pascal 7.0 wich is a 16-bit programming language!
>So it doesn't make use off the extended registers!

Actually the RTL uses them for longint multiplication and division. The
actual compiler does not use them.

##### Quote
>So if I like to use extended registers I must use NASM and use the inline
>function!

????

##### Quote
>Does someone know how pascal uses 32-bit numbers in it's code.

Well it uses two 16 bit registers. For example function results are
returned in DX:AX.

????

Osmo

## Re:Fixed point numbers and 32-bit numbers(longints)

##### Quote
"NLHG" <n...@worldonline.co.za> wrote:
>The advantage is that you can do fairly accurate maths without the maths
>co-processor!

There have been programs that used that, FractInt is one.  But
I'm not sure if there is a need to do that now (on modern CPUs).

(

Jud McCranie

## Re:Fixed point numbers and 32-bit numbers(longints)

##### Quote
NLHG wrote:
> I hope you understand the term fixed point numbers!
> This is where u use a 32bit number to represent a "real" number!
> You use the top 16.bits for the integer part and the bottum 16-bits for the
> fraction part!
> Know I have a bit off a problem!
> How do I multiply 2 off these fixed point numbers!
> And how do I divide them!

Simply do the IMUL, then shift right the result 16 times arithmetically (the SAL
instruction, don't use here SHR!!!).
In order to divide, shift left dividend 16 times using SAL or SHL. Then perform
the IDIV. Result will be already in fixed point format.

##### Quote
> Second about the 32-bit numbers!
> I use turbo pascal 7.0 wich is a 16-bit programming language!
> So it doesn't make use off the extended registers!

It calls run-time library subroutines for computing products and quotients. It
also uses subroutines for shifting 32-bit values left and right.
Probably there are few subroutines for adding and subtracting. But maybe I'm
mistaking here.

##### Quote
> So if I like to use extended registers I must use NASM and use the inline
> function!

Just use that! :) There is no any serious problem.

##### Quote
> Does someone know how pascal uses 32-bit numbers in it's code.

It doesn't use 32-bit integers itself at all.

##### Quote
> How do I go about writing a object file with nasm?

Just compile with the "-f obj" switch. This makes NASM produce standard
Microsoft/Intel object files. You need this in order to link obj with pascal
projects.

Not at all. :)

##### Quote
> http://homes.arealcity.com/Heinrichshomepage/

And mine: http://www.chat.ru/~alexfru

Good Luck
Alexei A. Frounze

## Re:Fixed point numbers and 32-bit numbers(longints)

In article <3839BF03.9840B...@mtu-net.ru>,
Alexei A. Frounze <alex....@mtu-net.ru> wrote:

##### Quote

>It calls run-time library subroutines for computing products and quotients. It
>also uses subroutines for shifting 32-bit values left and right.
>Probably there are few subroutines for adding and subtracting. But maybe I'm
>mistaking here.

Of course there is no subroutine for adding or subtracting. They are
simply done inline:

mov ax,word ptr x
mov dx,word ptr x+2

Osmo

## Re:Fixed point numbers and 32-bit numbers(longints)

##### Quote
NLHG wrote in message <81bqok\$56...@ctb-nnrp1.saix.net>...
>Hi there!

>I hope there someone who has been crazy enough to do this before!

>I hope you understand the term fixed point numbers!
>This is where u use a 32bit number to represent a "real" number!
>You use the top 16.bits for the integer part and the bottum 16-bits for the
>fraction part!
>The advantage is that you can do fairly accurate maths without the maths
>co-processor!
>I.E. FASTER

>Know I have a bit off a problem!
>How do I multiply 2 off these fixed point numbers!
>And how do I divide them!

>I use turbo pascal 7.0 wich is a 16-bit programming language!
>So it doesn't make use off the extended registers!
>So if I like to use extended registers I must use NASM and use the inline
>function!

>Does someone know how pascal uses 32-bit numbers in it's code.

>How do I go about writing a object file with nasm?