www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - asm help with div/idiv

reply parabolis <parabolis softhome.net> writes:
I have poured over several documents and am at a loose end. I 
need help with making div and idiv work the way I expect them 
to. I keep getting a Win32 error when I try to use the following 
code:

================================================================
     typedef uint up4;

     up4 up4_div( up4 lhs, up4 rhs ) {
             up4 res;
             assert( rhs != 0 );
	    asm {
                 xor EAX, EAX;  // dividend bottom 32-bits to 0
                 mov EDX, lhs;  // dividend top 32-bits to lhs
                 div rhs;       // divide by rhs
                 mov res, EAX;  // return quotient
         }
         return res;
     }

     void main(char[][] args ) {
	up4 a = cast(up4)0xA0000001;
	up4 b = cast(up4)0xC0000000;
         up4_div( b, a );
     }

Output:
Error: Win32 Error
================================================================

I am expecting a result of ((lhs / rhs) * 2^32). Am I missing 
something simple?
Jul 31 2004
next sibling parent h3r3tic <h3r3tic dev.null> writes:
I don't get it either. Even that doesn't work for the same reason:

up4 up4_div( up4 lhs, up4 rhs )
{
	asm {
		pusha;
		mov AX, 1;
		div AX;
		popa;
	}
	
	return 0;
}
Jul 31 2004
prev sibling next sibling parent "Walter" <newshound digitalmars.com> writes:
Try running obj2asm over it and look at the result.

"parabolis" <parabolis softhome.net> wrote in message
news:ceghrm$15de$1 digitaldaemon.com...
 I have poured over several documents and am at a loose end. I
 need help with making div and idiv work the way I expect them
 to. I keep getting a Win32 error when I try to use the following
 code:

 ================================================================
      typedef uint up4;

      up4 up4_div( up4 lhs, up4 rhs ) {
              up4 res;
              assert( rhs != 0 );
     asm {
                  xor EAX, EAX;  // dividend bottom 32-bits to 0
                  mov EDX, lhs;  // dividend top 32-bits to lhs
                  div rhs;       // divide by rhs
                  mov res, EAX;  // return quotient
          }
          return res;
      }

      void main(char[][] args ) {
 up4 a = cast(up4)0xA0000001;
 up4 b = cast(up4)0xC0000000;
          up4_div( b, a );
      }

 Output:
 Error: Win32 Error
 ================================================================

 I am expecting a result of ((lhs / rhs) * 2^32). Am I missing
 something simple?
Jul 31 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
The div instruction is generating the exception. Perhaps it's overflowing.

"parabolis" <parabolis softhome.net> wrote in message
news:ceghrm$15de$1 digitaldaemon.com...
 I have poured over several documents and am at a loose end. I
 need help with making div and idiv work the way I expect them
 to. I keep getting a Win32 error when I try to use the following
 code:

 ================================================================
      typedef uint up4;

      up4 up4_div( up4 lhs, up4 rhs ) {
              up4 res;
              assert( rhs != 0 );
     asm {
                  xor EAX, EAX;  // dividend bottom 32-bits to 0
                  mov EDX, lhs;  // dividend top 32-bits to lhs
                  div rhs;       // divide by rhs
                  mov res, EAX;  // return quotient
          }
          return res;
      }

      void main(char[][] args ) {
 up4 a = cast(up4)0xA0000001;
 up4 b = cast(up4)0xC0000000;
          up4_div( b, a );
      }

 Output:
 Error: Win32 Error
 ================================================================

 I am expecting a result of ((lhs / rhs) * 2^32). Am I missing
 something simple?
Jul 31 2004
next sibling parent parabolis <parabolis softhome.net> writes:
Walter wrote:
 The div instruction is generating the exception. Perhaps it's overflowing.
Yes you are correct. I am certain it is the div instruction. I had not idea an overflow would cause an exception however...
Jul 31 2004
prev sibling parent reply parabolis <parabolis softhome.net> writes:
Walter wrote:
 The div instruction is generating the exception. Perhaps it's overflowing.
 
Thanks again. I did a search on idiv and found the following. http://www.jorgon.freeserve.co.uk/TestbugHelp/UseofIDIV.htm If the result of the divide is larger than the data size of the destination again the processor will halt (in Windows an exception code 0C0000095h occurs). If you have any links to well done ducmentation for x86 assembly then I could use them. The only substantial documentation I have is the "Intel 80386 Programmer's Reference 1986."
Jul 31 2004
parent "Walter" <newshound digitalmars.com> writes:
"parabolis" <parabolis softhome.net> wrote in message
news:cegnpm$17rd$1 digitaldaemon.com...
 If you have any links to well done ducmentation for x86 assembly
 then I could use them. The only substantial documentation I have
 is the "Intel 80386 Programmer's Reference 1986."
You can download the Intel instruction set manuals in PDF from Intel's web site. That's what I use.
Jul 31 2004