flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Main > Integer Division

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 263
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop

Integer Division

how to check whether the quotient will fit in AL before integer division (TheWord>0xFF)?

Code:
mov ax,[TheWord]
idiv cl


_________________
smaller is better
Post 10 Mar 2018, 17:14
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 231
Location: Belarus

Re: Integer Division


CandyMan wrote:
how to check whether the quotient will fit in AL before integer division (TheWord>0xFF)?

Code:
mov ax,[TheWord]
idiv cl



You’d better perform division for double-sized values.


Code:
mov ax, [TheWord]
cwd
movsx cxcl
idiv cx



You’re not in control of #DE anyway and wouldn’t like to be. So just prevent it.
Post 10 Mar 2018, 19:06
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 263
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop

Thanks. This works with byte integer division. How to prevent exception in this case:

Code:
mov edx,[Hi]
mov eax,[Lo]
idiv ecx


_________________
smaller is better
Post 10 Mar 2018, 20:39
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6812
Location: Kraków, Poland

You may already be aware that for unsigned division this is very simple:

Code:
mov edx,[Hi]
mov eax,[Lo]
cmp edx,ecx
jae overflow
div ecx

This is because the range of EDX:EAX that does not cause an overflow is from 0 to (1 shl 32)*ECX (not inclusive), and you can test if EDX:EAX fits in this range simply by comparing EDX with ECX.

The signed case is much more complex. You need EDX:EAX to be larger than -(1 shl 31 + 1)*ECX and smaller than (1 shl 31)*ECX, this could be tested with some shifts, but would not be as nice and simple.
Post 10 Mar 2018, 21:38
View user's profile Send private message Visit poster's website Reply with quote
CandyMan



Joined: 04 Sep 2009
Posts: 263
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop

I am thanking everyone for help.
Post 11 Mar 2018, 12:33
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


Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2018, Tomasz Grysztar.
Powered by rwasa.