You are here: Articles > Programming > Programming

 See more articles about "Programming "

Assembly: division

 

How to divide large numbers with assembly language, and where the results end up.

numbers are most commonly divided by smaller numbers (eg a 16 bit number divided by an 8 bit number)



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



This example takes a possible 32 bit number and shows how to move it's 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





that's all there is to it. 16 bit numbers are of course easier to deal with as there is no bit shifting involved as DH, and DL are available directly.

Thus is this result was 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: william_a_wilson@hotmail.com

 

Also see ...

Assembly: Bit Shifting
H3Shift bits left or right by a desired numebr of places/H3P br / br / br /Bit shifting is an easy task. br /Shift left (in this case by 8): br /div class="code" br /shl EAX,8 br /P br /or to Shift right (in this case by 8): br /div class="code" br /

Assembly: Bit Rotating
H3Shift bits left or right by a desired numebr of places, with a wrap around to the other side/H3PThis method saves all bits. br /Say you shifted AX by 8, in either direction the new AX value would hold AH as the old AL and AL would become the old AH. This is done with a temporary copy of t

Assembly: Bit Rotating (carry flag)
H3Shift bits left or right by a desired numebr of places, with a wrap around to the other side through the carry flag/H3PThis method saves all bits. br /Say you shifted AX by 8, in either direction the new AX value would hold AH as the old AL and AL would become the old AH. br /This method

Using Ascii Text to Help Section Code
H3When coding in a plain text environment, it is difficult to set off sections and comments. The use of ascii generated text can make your life easier./H3PCoding in a plain text environment can be difficult because it is often hard to identify and set off areas of code. The generation of lar

Windows Batch File (.bat) to get current date in MMDDYYYY format.
H3This is a recipe I discovered while working on a few old skool dos batch files. This is a quick .bat file to create a folder based on the current date however, this sets it up in the MMDDYYYY format. Useful for those .bat scripters who need to use the current date variable in such a format./H3