Wednesday, December 18, 2024
HomeComputer programmingAssembly: Division

Assembly: Division

The following tutorial explains how to divide large numbers with assembly language and where the results end up.


Numbers are most commonly divided by smaller numbers. (e.g., A 16-bit number is divided by an 8-bit number.)

The numerator must be placed in specific registers, and the denominator is supplied as a single operand.

This example takes a possible 32-bit number and shows how to move its contents to the correct 16- bit registers.

XOR EDX,EDX ;clear EDX
;place number to be divided into EDX (32-bit)
XOR CX,CX ;clear CX
;place 16-bit denomenator into CX

do_division:
mov AX,DX ;move least significant bits to AX
shr EDX,16 ;shift most significant bits to DX
div CX ;divide DX:AX by CX
;quotient in AX
;remainder in DX

Sixteen-bit numbers are, of course, easier to manage as there is no bit shifting involved since DH and DL are available directly.
Thus, if this result were a fraction, the solution would be AX + DX / CX, where DX/CX is the remainder over the divisor as the fractional part of the solution.

Questions/Comments: [email protected]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!