Originally published in 2015-8-14, this article was migrated from other places.
(This article is about unsigned operations)
The div instruction is a division operation instruction in the 8086 assembly, and its result is not a floating-point number, but two integers: quotient and remainder.
Let's see what the Wang Shuang teacher said:
Now we think about why in assembly language, the divisor length is less than dividend?
Analysis:
Since the length of the dividend is equal to the length of the divisor (assuming 8-bit), define a multiplicative formula: X*y=z What happens if x=ff,y=2? The result is Z=fe (the original result is 1FE, but only 8-bit results are retained after the overflow), and the result is obviously unacceptable. If you divide this equation into division, the right thing is 1fe/ff=2. Therefore, the length of the z is extended, the problem can be solved.
Compressing the above content is:
dividend Number of digits = number of digits of divisor
Divisor is 8 . bit: (AX)/num= (AL) ... (AH)
Divisor is bit: (DX (H) ax (L))/num= (AX) ... (DX)
Usage: DIV Divisor
Through the book, we have come to the following conclusion:
A number that is greater than 2^8-1 (2 of 8 minus 1) is dividend, the divisor must be 16 bits, and the divisor is 8 bits, and the quotient and remainder are also 8 bits. A number that is greater than 2^16-1 is dividend, the divisor must be 32 bits, the divisor is 16 bits, and the quotient and remainder are 16 bits. Because sometimes the CPU registers may not be able to be used, so it is necessary to use memory space to do division.
First, the X PTR operator, which indicates the length of the data to be processed, is simple to use:
byte ptr processing data length bytes
Word PTR handles data length as font
mov ds: [0; assign an immediate value of 1H to the memory unit pointed to by Ds:[0] mov ds: [0; assign an immediate value of 1H to the memory Word unit pointed to by Ds:[0]
If the two example is executed before the corresponding memory unit is 0.
The first example executes after the memory unit is this: ds:0000 01
The second example executes after the memory unit is this: ds:0000 01 00
Now let's look at a piece of code:
AssumeCS:Codeds:datadata segment; data segment db100d;set a divisor memory unit, the data is 100, the address is ds:[0]Data Endscode Segment;Code SnippetStart: movAx,datamovDs,ax;set the data segment register to point to the data segment movax,200D;Set Dividend Divbyte ptrds:[0];to divide movbyte ptrds:[0],al;overwrite the result to the memory unit where the divisor is located movax,4c00h;Program returns int21Hcode endsend Start
Code Execution:
Memory Unit and register condition after instruction before running all division instructions:
After running the division instruction and executing the memory unit and register before the program returns:
The result is saved to the data segment and the program is completed.
Assembly Language Division instruction is easy to forget its usage and the location of the results saved, need more practice, more practice.
General usage of DIV directives