Assembly arithmetic operation commands

Source: Internet
Author: User
Tags sub command

Assembly arithmetic operation commands
8086 of arithmetic operation commands can perform addition, subtraction, multiplication, and Division operations on binary or decimal (BCD code) numbers, the data format of an operand can be 8-bit or 16-bit unsigned or signed. An immediate number is not allowed for single-operand commands. for dual-operand commands, only source operations can use immediate numbers. One of the two operands must be in a register.

1. Addition command

Command Format: Add DST, Src; (DST) values (SRC) + (DST)
Add adc dst, Src; (DST) percentile (SRC) + (DST) + CF
Add 1 Inc OPR; (OPR) Converged (OPR) + 1
Note:

These three command operations will affect the status flag, but the INC command does not affect the flag cf. The following describes the 8-digit addition operation.

[Example: 4.20]

 

Because the calculation result does not exceed the unsigned number range of a single byte, cf = 0; the calculation result exceeds the unsigned number range of a single byte, So of = 1; other signs ZF = 0, sf = 1. The preceding operations can be implemented using two Commands: mov ah, 01 add ah, 7fh
The ADC command is mainly used to calculate the addition of Multi-byte or multi-precision data.

For example, when adding two groups of four-byte (dual-precision) Numbers of 1122 3344h and 5566 7788h, the single-byte addition Command requires four addition operations. During the operation, the ADC command is used to add the carry produced by the low-byte operation to the high-byte operation. The word addition command is used to perform only two addition operations, of course, you must also use the ADC command to process the carry produced by low-word operations. To use the ADC command, you must set the CF flag to 0 first.

2. subtraction commands


Command Format:
Subtract sub DST, Src; (DST) least (DST)-(SRC)
Bitwise subtraction sbb dst, Src; (DST) least (DST)-(SRC)-cf
Minus 1 Dec OPR; (OPR) minus (OPR)-1
Calculate the neg OPR complement; (OPR) returns 0ffffh-(0pr) + 1
Compare CMP opr1, opr2; (opr1)-(opr2)

The SBB command is mainly used to calculate the subtraction of Multi-byte or multi-precision data. The neg command is used to obtain and Add 1 to the operand. The CMP command is similar to the sub command to perform the subtraction operation, however, no calculation result is generated. The effect on the flag is shown in table 4.3. The operation results of these commands affect the status flag, but the DEC command does not affect the mark cf.
[Example: 4.22]


Result of direct subtraction formula: (4006ah) = 520fh, Sf = 0, ZF = 0, cf = 0, of = 0
Result of the complement addition formula: (4006ah) = 520fh, Sf = 0, ZF = 0, cf = 1, of = 0
In formula, fecah is the complement of (-0136 H.
It can be seen that the calculation results are the same in the two formula, but the effect on Mark CF is different. Because it is a subtraction operation, the correct result should be cf = 0. Obtain the computation result cf = 1 through the complement addition. The result is reversed and sent to CF.

Table 4-3 Impact of CMP commands on status flag


3. Multiplication command

Multiplication is divided into two types: unsigned and signed. Each has its own commands and uses dual operands. The product is a 16-bit binary number. The product is a 32-bit binary number.

Command Format:
Unsigned number multiplication Mul SRC; (ax) percentile (Al) × (SRC) 8-digit multiplication
(Dx, ax) percentile (ax) × (SRC) 16-digit multiplication
The operation is the same as above, but the operand is the number of symbols.
Note:
During the byte operation, the destination operand must be the Al operator, and the product must be in the register ax. During the word operation, the destination operand must be the ax operator, and the product must be in the Dx and ax registers. The source operand cannot use the immediate number addressing method.
The Operation Result of the multiplication command only affects the status signs CF and of, and does not affect other status signs (the status is not fixed ).
For Mul commands, if the product of byte data multiplication (AH) = 0 or the product of word data multiplication (dx) = 0, cf = of = 0, otherwise, cf = of = 1; for imul commands, if the product Ah of byte data multiplication or the product DX of word data multiplication is a symbol extension of half, then cf = of = 0; otherwise, cf = of = 1.

4. Division commands

Division operations are divided into two types: unsigned number operations and signed number operations. Each operation has corresponding commands and uses dual operands. When the divisor is an 8-bit or 16-bit binary number, the divisor must be a 16-bit or 32-bit binary number.

Command Format:
division Div SRC by unsigned number; (Al) operator of division by 8-bit binary number by ax/src
(Ah) divide (ax)/(SRC) the remainder of the division of the 8-bit binary number
or (ax) divide (dx, ax)/(SRC) operator of the 16-bit binary number Division
(dx) Division (dx, ax)/(SRC) Division of the 16-bit binary number remainder
division by the symbol number idiv SRC; the operation is the same as above, but the operand is a signed number

Note:
When the divisor is a byte data, the divisor must be placed in ax. When the divisor is a word data, the divisor must be placed in Dx and ax.
The operation result of the division command is not defined for the status mark (the status is not fixed ). However, if the divisor is 0 or has a signed number, when the Division operation result exceeds the specified range, the Division 0 is interrupted, regardless of the overflow sign.
8086/8088 the symbol of the remainder of the Operation Result of the idiv command is the same as that of the dividend.
In the number division operation, when the number of divisor digits is not enough, the number of divisor must be extended to the required number of digits. 8086/8088 provides the symbol number extension command.
Command Format:
Byte extended to the word CBW; Extend the symbol bit in register Al to register ah
The characters are extended to the dual-character CWD; the symbols in the register ax are extended to the Register DX
These two commands do not affect the flag bit.

5. Decimal Adjustment Command

BCD is a binary-encoded decimal number, also known as binary-decimal number. In 8086/8088, BCD code is divided into two forms: one is to use four bits to represent a decimal number, called a compressed BCD code; the second is to use an eight-bit binary number to represent a decimal number. It is called a non-compressed BCD code. Its four-bit lower BCD code is meaningless. Because BCD code is a four-bit binary code, the four-bit binary code has a total of 16 encodings, and BCD code only uses 10 of them. The other useless encodings are called invalid codes. Errors may occur when the BCD code operation result enters or skips the invalid code area. To get the correct results, you must make adjustments. 8086/8088 for compressed BCD code and non-compressed BCD code, there are two sets of decimal adjustment commands, the adjustment method is slightly different.
(1) Compressed BCD Code Decimal Adjustment Command

Command Format:
Add to decimal to adjust DAA; (Al) encode to compress the sum in Al to the BCD format
Subtraction decimal to adjust the DAS; (Al) minus to adjust the difference in the Al to the compressed BCD format

The adjustment method is as follows:
If the Al lower 4 bits are greater than 9 or the secondary carry flag is Af = 1, the Al Of The accumulators is adjusted by 06h. If the Al Of The accumulators is greater than 9 or the carry mark is Cy = 1, the Al Of The accumulators is added with 60 h correction. The Al Of The accumulators is greater than or equal to 9. If the 4-bit lower is greater than 9, the Al Of The accumulators is added with 66h correction.
[Example 4.26] addition of BCD code 59 + 68 = 127

 

In this example, the BCD code addition results in the lower four bits make AF = 1, and the higher four bits are greater than 9, so 66h is added for correction.
Note:

The addition or subtraction of compressed BCD Code Decimal Adjustment Command must be used after the add (ADC) or sub (SBB) command. The adjustment result has no effect on the flag of other Status flags.
The subtraction decimal adjustment method is similar to the addition decimal adjustment method, except that the addition of 6 is changed to the subtraction of 6.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.