Re: A89: division in asm
[Prev][Next][Index][Thread]
Re: A89: division in asm
BLoWh0Le@aol.com wrote:
>
> so it has to be a long and a long? i tried dividing a word and a word (7
> divided by 8), and it gave me an illegal operation. thanks, i will try longs.
>
> In a message dated 5/8/99 10:36:20 PM Pacific Daylight Time,
> oh@hem.passagen.se writes:
>
> << What do you have against divu and divs?
> They discard the remainder if you divide a long by a long. >>
The 68000 can *only* divide a LONG by a WORD. The result must fit in a
word,
otherwise you'll get an overflow. Division by zero should be avoided :)
Example:
DIVU.W D1,D0 ; D0 = D0/D1
This calculates D0.L/D1.W and puts the result in D0.W. The remainder is
put
in the high word of D0, you can use SWAP to get it.
If D0.L contains $1234 and D1.W contains $10, D0.L would be equal to
$00040123
after this division. ($0004 + $0123*$10 = $1234.)
If you want to divide a word by a word, say D3.W divided by D2.W, you
must
clear the upper word of D3 (or sign-extend using EXT.L if you are using
negative
numbers) before performing the division:
ANDI.L #$0000FFFF,D3 ; clear upper word of D3.L
DIVU.W D2,D3 ; unsigned division, result in D3.W
-or-
EXT.L D3 ; sign extend D3.W into D3.L
DIVS.W D2,D3 ; signed division, result in D3.W
If speed is more important than accuracy, you should consider shifting
and
adding instead. DIV?.W is a *very* slow operation.
//Johan
Follow-Ups:
References: