News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

DIV instruction! Help

Started by annimbanerjee, December 02, 2012, 09:15:07 PM

Previous topic - Next topic

annimbanerjee

Currently learning Windows ask using mask 10v...GUI based app developing...

I am stuck on a point where i am trying to get some calculations with integers kinda data.

i am trying to do this...

now writing in C++:=>


GetClientRect(hWnd, &rect);


rect.right = rect.right/2; <= help




the line which i have made bold and highlighted... i am stuck over there...

means i want equivalent code for that line...

i tried DIV instruction but its terminating my application...
as i debug using olydebug.

the register's status goes wired over the DIV instructions...

anyways, please help me getting the same...

Thank You ::)

hutch--

Hi annimbanerjee, welcome on board.

With a divide by 2 you can do it with a shift rather than a DIV which is usually much slower. Have a look at the SHR ands SHL instructions to either multiply by powers of two or divide by powers of two.

Now with this line -> rect.right = rect.right/2; <= hel   "shr rect.right, 1" will do the job. Here you are using the shift instruction directly on the memory operand rect.right.


Gunther

Hi annimbanerjee,

welcome to the forum.

Gunther
You have to know the facts before you can distort them.

dedndave

you can do it directly
shr rect.right,1

if you are worried about the bit that gets shifted off, and you want the nearest...
mov eax,rect.right
shr eax,1
adc eax,0
mov rect.right,eax


the reason you are having trouble with DIV is probably the way you are using it
when you divide by a dword value, EDX:EAX make up the 64-bit dividend
mov eax,rect.right
xor edx,edx    ;shortcut version of mov edx,0
mov ecx,2
div ecx        ;in this case, ECX is the divisor
mov rect.right,eax

after the DIV, EAX holds the quotient and EDX holds the "modulus" or remainder

again, if you are worried about the remainder, and you want to round to the nearest...
mov eax,rect.right
xor edx,edx    ;shortcut version of mov edx,0
mov ecx,2
div ecx
shl edx,1      ;shr,1 the divisor instead if it is 80000000h or larger
cmp edx,ecx
sbb eax,-1
mov rect.right,eax

this round-off code also works for divisors other than 2   :P

annimbanerjee

@dedndave

mov eax,rect.right
xor edx,edx    ;shortcut version of mov edx,0
mov ecx,2
div ecx        ;in this case, ECX is the divisor
mov rect.right,eax


i was providing the EAX at DIV inst. line... sorry  :(

but anyways.. i am trying all the ways as directed by all the above...
Thanx for the assistance...

that SHR and SHL is more efficient i guess...

i ll try and get back here for results...

for now i have just fixed my window as non-resizable.
as i have wasted lot of time on that line.... so dobbu i am!!!  :biggrin:

dedndave

yes - for dividing by a power of 2, shift is much faster   :t
i showed the other examples in case you wanted to divide by 3 - lol