COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to perform division ?

• Follow

```Dear Sir,
As I understand assembly Language, we can divide a double word by only
a word.
I do not know how to divide any long integer by any long integer or
divide a number with number of same size.
e.g. I want to divide say 144000 / 72000 or 567890 / 12345000 using
assembly language ?
How to perform division without getting divide overflow error ?
Can anybody help me ?
-Mahesh Shrikrishna Chavan
mahesh.s.chavan@gmail.com

```
 0

```"mahesh.s.chavan" <spamtrap@crayne.org> wrote in message
> Dear Sir,
> As I understand assembly Language, we can divide a double word by only
> a word.
> I do not know how to divide any long integer by any long integer or
> divide a number with number of same size.
> e.g. I want to divide say 144000 / 72000 or 567890 / 12345000 using
> assembly language ?
> How to perform division without getting divide overflow error ?
> Can anybody help me ?
> -Mahesh Shrikrishna Chavan
> mahesh.s.chavan@gmail.com
>

You need to get hep on multiword arithmetic.  See page 229 of
and search for other links.  Ratch

```
 0

```mahesh.s.chavan asked:
> Dear Sir,

:) call me Oncle, I'm not a Sir :)

> As I understand assembly Language, we can divide a double word by only
> a word.

Almost right, but we got more:

div r/m (b/w/d/q)
idiv r/m(b/w/d/q)

also possible with FPU:
fdiv st(n)          ;assumes IEE-figures

or look at the various SSE-DIV (depends on CPU-features)

the first two above means a divide by either a memory or register operand.
The x86 CPU knows for DIV or IDIV:

word/byte:      ;ax,byte
dword/word      ;dx:ax/word
qword/dword     ;edx:eax/dword
oword/qword     ;rdx:rax/qword  (64-bit mode only)

> I do not know how to divide any long integer by any long integer or
> divide a number with number of same size.
> e.g. I want to divide say 144000 / 72000 or 567890 / 12345000 using
> assembly language ?

the 'long' definition may vary with the tools ...
I assume you mean 32/32 bit yet.

Your first example can be just done with integer divide,
for the second I'd use the FPU or a 2^n factor biased integer DIV
because the result may be less than 1 and a plain integer divide
will just produce a Zero here.

> How to perform division without getting divide overflow error ?
> Can anybody help me ?

check ahead if the result can fit the destination:
quotient results after DIV/IDIV are in al(byte) ax(word) eax(dword)
while the remainder(MOD) is found in ah(byte) dx(word) edx(dword).

Or if you want to make sure no overflow occure expand the operands
ie:
MOV eax,dividend      ;32 bit
MOV ebx,divisor       ;32 bit
XOR edx,edx           ;clr edx
DIV ebx               ;div edx:eax/ebx    cannot overflow (if ebx =! 0)
;quotient is in eax, remainder (MOD) in edx

And you can repeat the DIV with the remainder until you got your desired
precision [even 2^(-n) valued fractions then].

btw: avoid (the slow) divisions when ever you can and replace it with
shift (2^n only) or MUL with 1/x constants, or even combine the two.

__
wolfgang

```
 0

```On Mon, 7 Jan 2008 12:25:54 -0800 (PST), mahesh.s.chavan
<spamtrap@crayne.org> wrote:

>Dear Sir,
>As I understand assembly Language, we can divide a double word by only
>a word.
>I do not know how to divide any long integer by any long integer or
>divide a number with number of same size.
>e.g. I want to divide say 144000 / 72000 or 567890 / 12345000 using
>assembly language ?
>How to perform division without getting divide overflow error ?
>Can anybody help me ?
>-Mahesh Shrikrishna Chavan
>mahesh.s.chavan@gmail.com
>

xor edx,edx	;High dword of numerator
mov eax,144000   ;Low dword of numerator
mov ebx,72000     ;Denominator
cmp edx,ebx	;Will it overflow?
jae Ovfl_Handler
div ebx		;No ovfl possible if EDX < EBX

The above is for unsigned values.
Checking for overflow before signed division (IDIV) is probably
not worth the trouble.  Instead, you can take the absolute
values of the numbers, keeping track of their original
signs, then do an unsigned DIV as above, then reapply
the signs.  This is more attractive if the sign of either the
numerator or denominator is constant.

;To take abs value of EAX:
cdq		;Sign to EDX
xor eax,edx

push edx

;(do DIV here, unsigned result in EAX)

pop edx		;Original sign to reapply
xor eax,edx
sub eax,edx	;EAX now has original sign

Best regards,

Bob Masta

DAQARTA  v3.50
Data AcQuisition And Real-Time Analysis
www.daqarta.com
Scope, Spectrum, Spectrogram, FREE Signal Generator

```
 0

```On Jan 8, 3:38 am, "Ratch"  <spamt...@crayne.org> wrote:
> "mahesh.s.chavan" <spamt...@crayne.org> wrote in message
>
>
> > Dear Sir,
> > As I understand assembly Language, we can divide a double word by only
> > a word.
> > I do not know how to divide any long integer by any long integer or
> > divide a number with number of same size.
> > e.g. I want to divide say 144000 / 72000 or 567890 / 12345000 using
> > assembly language ?
> > How to performdivisionwithout getting divide overflow error ?
> > Can anybody help me ?
> > -MaheshShrikrishna Chavan
> >mahesh.s.cha...@gmail.com
>
>      You need to get hep on multiword arithmetic.  See page 229 ofhttp://books.google.com/books?id=LzyOaaO4MN0C&pg=PA224&lpg=PA224&dq=m...
> and search for other links.  Ratch

Many Thanks to Wolfgang, Ratch and Masta who gave the answer to my
question. I am now able to do it.
However, when I try to download the book pages from the above site, I
was not able to view those pages, offline.
I have read some division algorithm without using any maths co-
processor, and 386 registers. It was based on Z80 assembly language
and I wish to perform division which should work on 8086 machine which
I purchased in 1990.
Thanks to all those who helped.
-Mahesh

```
 0

4 Replies
258 Views

Similiar Articles:

7/24/2012 4:31:58 PM