flat assembler
Message board for the users of flat assembler.

Index > Main > MUL & DIV


What is the most horrible bug ever ?
Boreland Turbo PASCAL`s "Illegal division by zero in CRT" bug
54%
 54%  [ 6 ]
Something else
45%
 45%  [ 5 ]
Total Votes : 11

Author
Thread Post new topic Reply to topic
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Anyone has good informations / resources about how to perform MUL and DIV on integers bigger than the CPU natively supports ?

There is some code for 64-bit MUL and DIV for 32-bit CPU's around ... mostly without any comments Neutral

What I'd like to see:

1. A simple (2nd simplest after repeated addition or brute-force, I'm not chasing for picoseconds Wink ) code for UINT64-> UINT64 MUL and DIV for 32-bit CPU's with comments
2. UINT64-> UINT64 for 8086 (quad-size)
3. Code to perform MUL and DIV of 2 vars (var vs const is trivial) using additions and shifts, without MUL or DIV instructions

The DIV's should return both the quotient and remainder.

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 05 May 2008, 07:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17658
Location: In your JS exploiting you and your system
revolution
Multiplies are easy, just use the basic "schoolboy" cross multiply and add. For small numbers (<10000bits) this is the best for both speed and size with modern CPUs.

Divides can be done with shift/compare/subtract on a bit-by-bit basis but a faster (and slightly more complex) method is Barretts method for numbers larger than ~256bits. Google it. If you have a fixed divisor then you can always use the standard reciprocal multiply method. Depends on your applications needs.
Post 05 May 2008, 07:34
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
I think those algos are present in AMD optimization manual.
Post 05 May 2008, 09:31
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Post 05 May 2008, 16:58
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> I think those algos are present in AMD optimization manual.

For 8086 also ?

> http://www.geocities.com/snoopimeanie/32math64.txt

Thanks ... but that's the standard 32->64 extension that can be found at various other places also Neutral And unusable for 8086 ...

Now I found a MUL algo at least (test ASAP) ...

Any ideas about division ?

- var / var
- var / const ( frac(lb(const))<>0 !!! )
Post 07 May 2008, 08:52
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17658
Location: In your JS exploiting you and your system
revolution
DOS386 wrote:
For 8086 also ?
So just remove all the 'e' from the register names and change all the offsets into 2 instead of 4. I don't see the problem. It is the method you need right? Surely you don't want someone to write all your code for you. Right?

Besides, 16bit code is wasting the CPU unless you have a genuine 808x or 8028x, but somehow I doubt that there are many systems around still in service and using one of those.
Post 07 May 2008, 10:36
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4241
Location: 2018
edfed
Quote:

Besides, 16bit code is wasting the CPU unless you have a genuine 808x or 8028x, but somehow I doubt that there are many systems around still in service and using one of those.


totally true one more time.

808x & 8028x are completelly OBSOLETE.

for multiply, i have a simple idea, based on shifting and adding.
32 iterations for 32 bits, 64 iterations for 64 bits

for divide....sub and shift. 64 iterations for 64 bits.
Post 07 May 2008, 10:56
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
http://www.dunfield.com/downloads.htm

Quote:

BIGNUM.ZIP (4K) Very large math ops (64+bits)with C source.


And since this is done under Micro-C, it's 8086 compatible (and you can easily output to .ASM to see what it's doing).
Post 24 May 2008, 22:23
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Quote:
So just remove all the 'e' from the register names and change all the offsets into 2 instead of 4. I don't see the problem. It is the method you need right?


NO:

1. Removing "E" is no big deal really Sad
2. No SHLD & SHRD, no easy shift by > 1
3. This way I would get 32 bits only (maybe I could "chain" the trick but very dirty way then)

Quote:
Surely you don't want someone to write all your code for you.


Confused

Quote:
{text_size=0.00000000001}Besides, 16bit code is wasting the CPU unless you have a genuine 808x or 8028x, but somehow I doubt that there are many systems around still in service and using one of those.


COOL.

edfed wrote:

> totally true one more time.
> 808x & 8028x are completelly OBSOLETE.

Hey, then you must "upgrade" your famous Windaube 98 ASAP Very Happy

Quote:
BIGNUM.ZIP (4K) Very large math ops (64+bits)with C source.


Smile That's what I needed ... but it's in C ... if someone has similar in 8086 assembly, please post Smile

And YES, 8086 assembly (avoiding AAM & Co) still is the preferred way to write down algorithms, ways better than Cobol, C++, Javas**t, ...
Post 01 Jun 2008, 22:42
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4241
Location: 2018
edfed
Quote:
Hey, then you must "upgrade" your famous Windaube 98 ASAP

i don't see what i can use instead of this daube.

windaube XP? windaube 2000? windaube Vistahhhhh? windaube NT4?
Linuxme? debian? ubuntu? Freebsd?

no way, the only reason to change of OS is to have my own ready to use.

Code:
mov ax,n1
mov bx,n2
mov cx,1
mov dx,0
next:
test ax,cx
je @f
add dx,bx
@@:
shl bx,1
shl cx,1
jne .next
    
Post 01 Jun 2008, 23:06
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> windaube Vistahhhhh?

YES. Or Server 2008.

Thanks for the Tomasz-style code Smile
Post 02 Jun 2008, 23:30
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.