Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:01:43+00:00 2026-06-13T03:01:43+00:00

I need some easy way to divide 64b unsigned integers in assembler for x86.

  • 0

I need some easy way to divide 64b unsigned integers in assembler for x86. My number is saved in two 32b registers EDX:EAX and I need to put result back to EDX:EAX. Factor is in 32b integer. Some code, please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:01:44+00:00Added an answer on June 13, 2026 at 3:01 am

    If I interpret your question correctly (particularly the part Factor is in 32b integer), you want to divide a 64-bit dividend by a 32-bit divisor and get a 64-bit quotient.

    If that interpretation is correct, then it’s actually easy to do in 32-bit code.

    The idea is that you divide both “halves” of the dividend by the divisor and reuse the remainder from the first division for the second division.

    C code illustrating how to do it:

    #include <stdio.h>
    #include <limits.h>
    
    #define C_ASSERT(expr) extern char CAssertExtern[(expr)?1:-1]
    
    #if UINT_MAX >= 0xFFFFFFFF
    typedef unsigned int uint32;
    #else
    typedef unsigned long uint32;
    #endif
    typedef unsigned long long uint64;
    
    typedef unsigned long ulong;
    
    // Make sure uint32=32 bits and uint64=64 bits
    C_ASSERT(sizeof(uint32) * CHAR_BIT == 32);
    C_ASSERT(sizeof(uint64) * CHAR_BIT == 64);
    
    int div64by32eq64(uint64* dividend, uint32 divisor)
    {
      uint32 dividendHi = (uint32)(*dividend >> 32);
      uint32 dividendLo = (uint32)*dividend;
      uint32 quotientHi;
      uint32 quotientLo;
    
      if (divisor == 0)
        return 0;
    
      // This can be done as one 32-bit DIV, e.g. "div ecx"
      quotientHi = dividendHi / divisor;
      dividendHi = dividendHi % divisor;
    
      // This can be done as another 32-bit DIV, e.g. "div ecx"
      quotientLo = (uint32)((((uint64)dividendHi << 32) + dividendLo) / divisor);
    
      *dividend = ((uint64)quotientHi << 32) + quotientLo;
    
      return 1;
    }
    
    int main(void)
    {
      static const struct
      {
        uint64 dividend;
        uint32 divisor;
      } testData[] =
      {
        { 1 , 0 },
        { 0xFFFFFFFFFFFFFFFFULL, 1 },
        { 0xFFFFFFFFFFFFFFFFULL, 2 },
        { 0xFFFFFFFF00000000ULL, 0xFFFFFFFFUL },
        { 0xFFFFFFFFFFFFFFFFULL, 0xFFFFFFFFUL },
      };
      int i;
    
      for (i = 0; i < sizeof(testData)/sizeof(testData[0]); i++)
      {
        uint64 dividend = testData[i].dividend;
        uint32 divisor = testData[i].divisor;
    
        printf("0x%016llX / 0x%08lX = ", dividend, (ulong)divisor);
    
        if (div64by32eq64(&dividend, divisor))
          printf("0x%016llX\n", dividend);
        else
          printf("division by 0 error\n");
      }
    
      return 0;
    }
    

    Output (ideone):

    0x0000000000000001 / 0x00000000 = division by 0 error
    0xFFFFFFFFFFFFFFFF / 0x00000001 = 0xFFFFFFFFFFFFFFFF
    0xFFFFFFFFFFFFFFFF / 0x00000002 = 0x7FFFFFFFFFFFFFFF
    0xFFFFFFFF00000000 / 0xFFFFFFFF = 0x0000000100000000
    0xFFFFFFFFFFFFFFFF / 0xFFFFFFFF = 0x0000000100000001
    

    And now the equivalent division code in assembly (NASM syntax) without checking for division by 0:

    ; 64-bit dividend
    mov edx, 0xFFFFFFFF
    mov eax, 0xFFFFFFFF
    
    ; 32-bit divisor
    mov ecx, 0xFFFFFFFF
    
    push eax
    mov eax, edx
    xor edx, edx
    div ecx ; get high 32 bits of quotient
    xchg eax, [esp] ; store them on stack, get low 32 bits of dividend
    div ecx ; get low 32 bits of quotient
    pop edx ; 64-bit quotient in edx:eax now
    ; edx:eax should now be equal 0x0000000100000001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some with help with PowerShell, please. It should be pretty easy: I
I have a console application that I didn't write. Is there some easy way
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some quick advice I am trying to access a object array but I
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
Need some help with this problem in implementing with XSLT, I had already implemented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.