flat assembler
Message board for the users of flat assembler.

Index > Main > How to divide two integers?

Author
Thread Post new topic Reply to topic
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?

I would be glad about answers, thank you Smile
Post 20 Oct 2004, 18:24
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
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.
Post 20 Oct 2004, 19:43
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
thank you very much for your fast and clear response. Now I understood that. Smile
Post 20 Oct 2004, 20:06
View user's profile Send private message Reply with quote
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
Post 20 Oct 2004, 20:46
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar.

Powered by rwasa.