DIV: Number of Operands = 1? or 2?

  • Follow


Hello All,

If I'm reading things correctly, and not missing something simple,
both my tutorial and the pentium manuals indicate that DIV has one
operand only - the divisor.

But when I compile a simple C++ assignment using unsigned integers
(dword) division, both MS VS2005 C++ compiler and the Intel C++
compiler create assembly which uses a destination and a source operand
for the DIV instruction. 

Here's the C++ code, followed by MASM from MS compiler and MASM from
Intel Compiler.

---------C++-------------------
int main(void)
{
unsigned int var1 = 4000U;
unsigned int var2 = 5000U;
unsigned int var3 = 6000U;
unsigned int var4 = 0U;

var4 = (var1 * var2) / (var1 * var3); //assembly below
cout << var4 << endl;
return 0;
}
---------------------------------

following are both assembly outputs for 
"var4 = (var1 * var2) / (var1 * var3);"

------MASM MS COMPILER---------
mov eax,dword ptr [var1]
imul eax,dword ptr [var2]
mov ecx,dword ptr [var1]
imul ecx,dword ptr [var3]
xor edx,edx
div eax,ecx     ;<====* taking dest & source here for div
mov dword ptr [var4],eax
--------------------------------------

-----MASM Intel Compiler------------
  mov         eax,dword ptr [var2] 
  imul         eax,dword ptr [var1] 
  mov         edx,dword ptr [var3] 
  imul         edx,dword ptr [var1] 
  mov         dword ptr [ebp-4],edx 
  xor          edx,edx 
  mov         ecx,dword ptr [ebp-4] 
  div           eax,ecx 
  mov         dword ptr [var4],eax 
----------------------------------------

-- 
thanks,
Brian
To the best of my knowledge, I have:
1) asked a question specifically related to this newsgroup
2) not used my email to request answers be sent there
3) not top-posted
4) not used bad grammar that would make me appear more stupider

0
Reply Brian 4/2/2007 2:05:51 PM

On Apr 2, 7:05 am, Brian  <spamt...@crayne.org> wrote:
>
> Here's the C++ code, followed by MASM from MS compiler and MASM from
> Intel Compiler.

First, you are confusing "MASM" with "Intel-like Assembly Syntax".
MASM is a specific language. Unless MASM has changed dramatically in
the most recent version, I can assure you that MASM will not assembled
the output below. Therefore, claiming it is "MASM" is technically
incorrect.

> div eax,ecx     ;<====* taking dest & source here for div

Have you actually tried to *assemble* this code with any assembler?
As I said, MASM has never allowed this syntax in any version that I've
ever used. That's not to imply that some assemblers won't allow you to
specify the implied destination operand (HLA allows this, for
example). Just that I've not seen MASM allow this.

I would suggest that the output syntax these compilers produce is
really for "human eyes only" and not for use by a particular
assembler. I could be wrong, but that would be my first guess.


> --
> thanks,
> Brian
> To the best of my knowledge, I have:
> 1) asked a question specifically related to this newsgroup
> 2) not used my email to request answers be sent there
> 3) not top-posted
> 4) not used bad grammar that would make me appear more stupider

But you *have* posted extraneous information that wastes bandwidth :-)
And (4), of course, is incredibly poorly formulated, making it almost
self-contradictory :-)


Cheers,
Randy Hyde


0
Reply rhyde 4/2/2007 8:02:28 PM


There IS only one operand specified!

There are three unsigned divide operations (DIV) and three signed
divide operations (IDIV).
A set of three are register al=ax/op        ah=remainder
                   register ax=dx:ax/op     dx=remainder
                   register eax=edx:eax/op  edx=remainder

Only ONE operand is specified: the location of the divisor which can
be another register or a memory location (eacjh of the required width)

0
Reply Terence 4/2/2007 11:10:59 PM

Brian wrote:
> Hello All,
> 
> If I'm reading things correctly, and not missing something simple,
> both my tutorial and the pentium manuals indicate that DIV has one
> operand only - the divisor.

That's correct.

> But when I compile a simple C++ assignment using unsigned integers
> (dword) division, both MS VS2005 C++ compiler and the Intel C++
> compiler create assembly which uses a destination and a source operand
> for the DIV instruction. 

....
> div eax,ecx     ;<====* taking dest & source here for div

....
>   div           eax,ecx 

That's interesting. Does it assemble? (not with Nasm!!!) If so, does it 
assemble if you change it to "div ebx, ecx"? I'd say your compilers - 
two different ones!!! - were misleading you. Can't say it's "wrong", 
'cause it's a syntax issue - you could write an assembler that took 
"framistan GP0;GP2<-GP0:GP2,GP1" and did the same thing. One of those 
occasions we may need to speak machine language. F7 F1. What's your 
compiler say?

Best,
Frank

0
Reply Frank 4/3/2007 12:06:30 AM

I suspect Brian's compiler takes the
   DIV eax,ecx
And disregards the first operand and produces code for
   DIV ecx
since the dividend is always ax, ax:dx or eax:edx and the divisor is a
nother register (as ecx)
or a memory address.
It may be a symbolic notation, but certainly not the version of MASM
nor TASM that I use.
Even Intel's data on cpu instructions doesn't mention the AX part
Maybe a much later version of one of the assemblers?

0
Reply Terence 4/3/2007 9:59:58 AM

4 Replies
218 Views

(page loaded in 0.356 seconds)

Similiar Articles:













7/25/2012 4:23:28 PM


Reply: