flat assembler
Message board for the users of flat assembler.

Index > Main > Integer Division

Author
Thread Post new topic Reply to topic
CandyMan



Joined: 04 Sep 2009
Posts: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 10 Mar 2018, 17:14
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: 1228
Location: Belarus
DimonSoft 10 Mar 2018, 19:06
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 cx, cl
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: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 10 Mar 2018, 20:39
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



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 10 Mar 2018, 21:38
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: 414
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan 11 Mar 2018, 12:33
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


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

Website powered by rwasa.