In assembly language, if the divisor is 32 bits, the divisor is 16 bits, the divisor is put in Dx and ax, the quotient is put in ax, and the remainder is put in dx. If the divisor is 16 bits, the divisor is 8 bits, the divisor is in ax, the quotient is in AH, and the remainder is in Al.
But there is a problem, that is, when the Division operator is greater than ax or ah, Division overflow will occur. For example, in 1000/1, the divisor can be placed in a 16-bit ax, and 1 can be placed in an 8-bit register. The quotient is 1000, which exceeds the maximum value that ah can hold, therefore, Division overflow may occur.
Wang Shuang's book provides a formula to avoid division overflow.
ChildProgramRequirements
Name: divdw
Function: Performs Division operations without overflow. The divisor type is DWORD, the divisor type is word, and the result is DWORD.
Parameter: (ax) = low 16-bit DWORD data
(Dx) = high 16-bit DWORD data
(CX) = Divisor
Return Value: (ax) = low 16 bits of the operator
(Dx) = high 16 bits
(CX) = Remainder
Formula
X: dividend, range: [0, ffffffff]
N: divisor, range: [0, FFFF]
H: x 16-bit high, range: [0, FFFF]
L: x 16-bit low, range: [0, FFFF]
INT (): Descriptive operator, operator, for example, INT (38/10) = 3
REM (): A descriptive operator that obtains the remainder, for example, REM (38/10) = 8.
Formula: X/N = int (H/n) * 65536 + [REM (H/n) * 65536 + L]/n
Analysis
First, let's take a look at the formula 65536. Many new users may not understand what this 65536 means. In fact, 65536 is 10000 h, which is equivalent to 2 ^ 16, that is, multiply by 2 ^ 16, this means to move 16 bits to the left.
In the formula, INT (H/N) and REM (H/n) represent the 16-bit operator and remainder of X.
INT (H/n) * 65536 on the right of the equation indicates that the quotient of H/N is used as the high-level word of the final result quotient.
REM (H/n) * 65536 + L indicates that the remainder of H/N is shifted to 16 bits to form a New DWORD data with a height of 16 bits, and L is used as a low 16 bits.
The final result is the result of storing int (H/n) in dx, and storing (REM (H/n) * 65536 + l)/n in ax, the remainder of (REM (H/n) * 65536 + l)/n stored in CX.
Subroutine
; Subroutine description
; Name: divdw
Function: conducts Division operations without overflow. The divisor type is DWORD, the divisor type is word, and the result is DWORD.
; Parameter: (dx) = high 16-bit (ax) of DWORD data = low 16-bit (CX) of DWORD data = Divisor
; Return: (dx) = high 16-bit (ax) of the result = low 16-bit (CX) of the result = Remainder
Example: Calculate 1000000/10 (f4240h/0ah)
Divdw:
Push ax; low 16-bit divisor into Stack
MoV ax, DX
MoV dx, 0
MoV CX, 10
Div CX; the result provider is placed in ax, and the remainder is placed in dx.
MoV CX, ax; temporary storage provider
Pop ax; Retrieve the low 16-bit value
Push CX; save vendor into Stack
MoV CX, 10
Div CX
MoV CX, DX; add remainder to CX
Pop DX; the high 16 bits of the merchant are stored in dx, and the result in ax is the low 16 bits of the merchant.
RET