The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: theifyppl on July 21, 2009, 03:03:01 AM

Title: Help with division in Easy Code MASM
Post by: theifyppl on July 21, 2009, 03:03:01 AM
Okay, so I need a bit of help with one of my projects.  I'm just trying to make a basic calculator with Easy Code MASM.  The point of it is just to mess around with Easy Code and see what I can do with it.  I've run into a bit of a problem.  When I click button4, I get the text of two textboxes, place the result in two variables (input1 and input2), use Invoke Value to change Input1 and Input2 to decimal values and place them in the variables Number1 and Number2.  At this point I Mov number1 into edx, and the I run DIV.  Ok well I looked up 32-bit division and it said I needed to load the denominator with a 16-bit number, meaning Number2 has to be 16-bit.  Here's my code:

.ElseIf Ax == IDC_WINDOW1_BUTTON4
HiWord wParam
.If Ax == BN_CLICKED
Invoke GetText, edit1, Addr Input1
Invoke DoEvents
Invoke GetText, edit2, Addr Input2
Invoke DoEvents

Invoke Value, Addr Input1
Mov Number1, Eax
Invoke DoEvents

Invoke Value, Addr Input2
Mov Number2, Eax
Invoke DoEvents

Mov Edx, Number1
Mov Ax, Dx
Shr Edx, 16
Div Number2

Invoke String, Ax, Addr FinalAnswer, ecDecimal
Invoke SetText, edit3, Addr FinalAnswer

.EndIf
.EndIf


The problem is, I need Number2 to be 16-bit, but the Invoke Value command returns the decimal result as 32-bit in eax.  I then mov eax into Number2, meaning number2 is 32-bit.  What I need is someone to help explain if it's possible to make number2 16-bit.  How would I go about doing it, etc.

Thanks
Title: Re: Help with division in Easy Code MASM
Post by: NightWare on July 21, 2009, 03:14:43 AM

mov edx, Number1
mov ecx, Number2
cdq
idiv ecx
and eax,0000FFFFh ; <- here needed only if sign, otherwise the result is in ax


Edit :
hmm, if Number2 IS a 16 bit value, use :
  movsx ecx,Number2
Title: Re: Help with division in Easy Code MASM
Post by: theifyppl on July 21, 2009, 04:31:40 AM
Thanks a bunch man.  I put the code in that you suggested and it's still crashing, so the crashing doesn't have anything to do with the dividing.  Thanks for helping me sort out where the problem is :)
Title: Re: Help with division in Easy Code MASM
Post by: MichaelW on July 21, 2009, 05:38:37 AM
The crash could be caused by DIV triggering a EXCEPTION_INT_OVERFLOW.

quotient = dividend / divisor

For DIV and IDIV the dividend is twice the size of the divisor and quotient, so for a DIV mem32 the dividend is in EDX:EAX and the quotient goes into EAX. To avoid an overflow the value of EDX must be less than the value of the divisor. For example:

Assuming EDX=1, EAX=0,ECX=1,
DIV ECX is effectively 100000000h / 1 = 100000000h (which will not fit in 32 bits)

Assuming EDX=1, EAX=0,ECX=2,
DIV ECX is effectively 100000000h / 2 = 80000000h (which will fit in 32 bits)

Title: Re: Help with division in Easy Code MASM
Post by: NightWare on July 21, 2009, 08:55:20 PM
 :red oops, it's
  mov eax,Number1
(too fast when answered, and if edx is not 0 or FFFFFFFFh then yes it crash...  ::) i generally don't divide much...)
Title: Re: Help with division in Easy Code MASM
Post by: theifyppl on July 25, 2009, 08:38:06 AM
Thanks for all your help guys.  One more problem tho :(

Ok so this is my code:

Mov Eax, Number1
Mov Ecx, Number2
Cdq
IDiv Ecx
Movsx Sum, Ax

Invoke String, Sum, Addr FinalAnswer, ecDecimal
Invoke SetText, edit3, Addr FinalAnswer


the problem is the Invoke String requires Sum to be a dword, and when I try to use movsx or movzx I get an error saying memory operand is not allowed in the context.  Any ideas?
Title: Re: Help with division in Easy Code MASM
Post by: MichaelW on July 25, 2009, 09:03:25 AM
An IDIV ECX will divide EDX:EAX by ECX and leave the 32-bit quotient in EAX.
Title: Re: Help with division in Easy Code MASM
Post by: sinsi on July 25, 2009, 09:35:12 AM
Is there any reason you need 16-bit? You can still do 32-bit division, but using 16-bit values - this gets around the divide overflow.
Title: Re: Help with division in Easy Code MASM
Post by: theifyppl on July 25, 2009, 11:22:06 PM
@ MichaelW

Thanks a bunch man! My problem was I was trying to move ax into the sum, when the answer was an eax like you said.

I got everything working now everyone :D.  Thanks ALOT