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 653267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:21:47+00:00 2026-05-13T22:21:47+00:00

In Delphi, the declaration of the DivMod function is procedure DivMod(Dividend: Cardinal; Divisor: Word;

  • 0

In Delphi, the declaration of the DivMod function is

procedure DivMod(Dividend: Cardinal; Divisor: Word;
  var Result, Remainder: Word);

Thus, the divisor, result, and remainder cannot be grater than 65535, a rather severe limitation. Why is this? Why couldn’t the delcaration be

procedure DivMod(Dividend: Cardinal; Divisor: Cardinal;
  var Result, Remainder: Cardinal);

The procedure is implemented using assembly, and is therefore probably extremely fast. Would it not be possible for the code

    PUSH    EBX
    MOV     EBX,EDX
    MOV     EDX,EAX
    SHR     EDX,16
    DIV     BX
    MOV     EBX,Remainder
    MOV     [ECX],AX
    MOV     [EBX],DX
    POP     EBX

to be adapted to cardinals? How much slower is the naïve attempt

procedure DivModInt(const Dividend: integer; const Divisor: integer; out result: integer; out remainder: integer);
begin
  result := Dividend div Divisor;
  remainder := Dividend mod Divisor;
end;

that is not (?) limited to 16-bit integers?

  • 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-05-13T22:21:47+00:00Added an answer on May 13, 2026 at 10:21 pm

    Such a procedure is possible. I have not tested the code enough, but I think it’s OK:

    procedure DivMod32(Dividend, Divisor: Cardinal; var Quotient, Remainder: Cardinal);
    asm
            PUSH EBX
            MOV  EBX,EDX
            XOR  EDX,EDX
            DIV  EBX
            MOV  [ECX],EAX
            MOV  EBX,Remainder
            MOV  [EBX],EDX
            POP  EBX
    end;
    

    Updated:

    even more efficient:

    function DivMod32(Dividend, Divisor: Cardinal; var Remainder: Cardinal): Cardinal;
    asm
            PUSH EBX
            MOV  EBX,EDX
            XOR  EDX,EDX
            DIV  EBX
            MOV  [ECX],EDX
            POP  EBX
    end;
    

    Updated 2:

    You can see the assembly code generated by Delphi compiler in the Disassembly (or CPU) window. Eg, the procedure

    procedure DivMod32(const Dividend: Cardinal; const Divisor: Cardinal;
                        out result: Cardinal; out remainder: Cardinal);
    begin
      result := Dividend div Divisor;
      remainder := Dividend mod Divisor;
    end;
    

    generates code

    Unit1.pas.28: begin
    0046CC94 55               push ebp
    0046CC95 8BEC             mov ebp,esp
    0046CC97 53               push ebx
    0046CC98 56               push esi
    0046CC99 8BF2             mov esi,edx
    0046CC9B 8BD8             mov ebx,eax
    Unit1.pas.29: result := Dividend div Divisor;
    0046CC9D 8BC3             mov eax,ebx
    0046CC9F 33D2             xor edx,edx
    0046CCA1 F7F6             div esi
    0046CCA3 8901             mov [ecx],eax
    Unit1.pas.30: remainder := Dividend mod Divisor;
    0046CCA5 8BC3             mov eax,ebx
    0046CCA7 33D2             xor edx,edx
    0046CCA9 F7F6             div esi
    0046CCAB 8B4508           mov eax,[ebp+$08]
    0046CCAE 8910             mov [eax],edx
    Unit1.pas.31: end;
    0046CCB0 5E               pop esi
    0046CCB1 5B               pop ebx
    0046CCB2 5D               pop ebp
    0046CCB3 C20400           ret $0004
    

    This code is linear (contains no jumps) and modern processors (with long instruction pipeline) are very efficient in executing linear code. So though my DivMode32 implementation is about 3 times shorter, 60% is a reasonable estimate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 345k
  • Answers 345k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That is the intent. The idea here is that you… May 14, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer Now that MS has started devoting develoment budget to improving… May 14, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer Either you create two repositories and use the external link… May 14, 2026 at 5:50 am

Related Questions

I'm on Delphi 2009, and my application contains a data module, which has a
I have been learning Delphi for the last 3 years, on a hobby/occupational level.
How can I compare the value of a variable that contains a pointer to
I'm tired of the .NET BeginRead,EndRead stuff.I'd love to use WSAAsyncSelect the way I
Normally, in Delphi one would declare a function with a variable number of arguments

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.