flat assembler
Message board for the users of flat assembler.

 Index > Main > How to divide two integers?
Author
denial

Joined: 12 Sep 2004
Posts: 98
denial
I want to divide two integers by IDIV, so my operand is DWORD. The manual says that my operand must then be a 64 bit value. However I didn't really understand the system, so I'm confused about dividing two signed integer values.

For example, if ECX is 1000 and EDX is 50
How can I perform ECX / EDX?

Then, I have another question...
There are unsigned and signed integers, right?
Unsigned integers can't hold a - sign, so they are always positive, is that right? So I wonder how signed integers store their sign, and how the range of the value gets decreased.
I think that a signed integer can't hold such a big value as an unsigned integer?
Could you explain me?

20 Oct 2004, 18:24

Joined: 16 Jun 2003
Posts: 1138
Location: Russian Federation
Regarding dividing two integers using IDIV:
The source operand is, indeed, 64-bit, but that does not mean you have to use all of them. The upper half of the 64-bit QWORD divident is stored in EDX, and the lower in EAX. So simply put your integer in EAX, and use CDQ to extend it to EDX (if EAX is negative, EDX will be FFFFFFFF, if EAX is positive, EDX will be 0). IDIV will then divide EDX:EAX by whatever operand register you give it.
So if you want to divide 1000 / 50, you would do the following:
Code:
```mov eax, 1000
cdq
mov ecx, 50
idiv ecx
; quotient is in eax
; remainder is in edx    ```

Regarding signed/unsigned integers
An unsigned integer is normally 32-bits long, and all these bits are used to represent the value of the integer. A signed integer would take away one of these bits, and use it to describe the sign (positive or negative) of the value. That way, only 31-bits are left for the value, so the range of a signed integer is twice as small as of its unsigned counterpart. The sign bit (usually the 32nd bit, the most significant bit, the leftmost bit) is 0 if the integer is positive, and 1 if the integer is negative.
20 Oct 2004, 19:43
denial

Joined: 12 Sep 2004
Posts: 98
denial
thank you very much for your fast and clear response. Now I understood that.
20 Oct 2004, 20:06
gorshing

Joined: 27 Jul 2003
Posts: 72
Location: Okla, US
gorshing
You might also want to read Paul Carter's Tut ... www.drpaulcarter.com ... look at Chapter 2.1

_________________
gorshing
20 Oct 2004, 20:46
 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