flat assembler
Message board for the users of flat assembler.
 flat assembler > Main > Integer Division
Author
CandyMan

Joined: 04 Sep 2009
Posts: 259
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
10 Mar 2018, 17:14
DimonSoft

Joined: 03 Mar 2010
Posts: 204
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 cx, cl idiv cx

You’re not in control of #DE anyway and wouldn’t like to be. So just prevent it.
10 Mar 2018, 19:06
CandyMan

Joined: 04 Sep 2009
Posts: 259
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
10 Mar 2018, 20:39
Tomasz Grysztar
Assembly Artist

Joined: 16 Jun 2003
Posts: 6770
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.
10 Mar 2018, 21:38
CandyMan

Joined: 04 Sep 2009
Posts: 259
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
I am thanking everyone for help.
11 Mar 2018, 12:33
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------Blog General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum