flat assembler
Message board for the users of flat assembler.

 Index > Main > Integer Division
Author
 Thread
CandyMan

Joined: 04 Sep 2009
Posts: 319
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan
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: 821
Location: Belarus
DimonSoft
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: 319
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan
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

Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
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: 319
Location: film "CandyMan" directed through Bernard Rose OR Candy Shop
CandyMan
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----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals 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

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

Website powered by rwasa.