* better
[mascara-docs.git] / hw / i386.reference / s03_02.htm
blob6ffbf62400f9d358debc812b5e5541159485df81
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>80386 Programmer's Reference Manual -- Section 3.2</TITLE>
5 </HEAD>
6 <BODY>
7 <B>up:</B> <A HREF="c03.htm">
8 Chapter 3 -- Applications Instruction Set</A><BR>
9 <B>prev:</B> <A HREF="s03_01.htm">3.1 Data Movement Instructions</A><BR>
10 <B>next:</B> <A HREF="s03_03.htm">3.3 Decimal Arithmetic Instructions</A>
11 <P>
12 <HR>
13 <P>
14 <H1>3.2 Binary Arithmetic Instructions</H1>
15 The arithmetic instructions of the 80386 processor simplify the
16 manipulation of numeric data that is encoded in binary. Operations include
17 the standard add, subtract, multiply, and divide as well as increment,
18 decrement, compare, and change sign. Both signed and unsigned binary
19 integers are supported. The binary arithmetic instructions may also be used
20 as one step in the process of performing arithmetic on decimal integers.
21 <P>
22 Many of the arithmetic instructions operate on both signed and unsigned
23 integers. These instructions update the flags ZF, CF, SF, and OF in such a
24 manner that subsequent instructions can interpret the results of the
25 arithmetic as either signed or unsigned. CF contains information relevant to
26 unsigned integers; SF and OF contain information relevant to signed
27 integers. ZF is relevant to both signed and unsigned integers; ZF is set
28 when all bits of the result are zero.
29 <P>
30 If the integer is unsigned, CF may be tested after one of these arithmetic
31 operations to determine whether the operation required a carry or borrow of
32 a one-bit in the high-order position of the destination operand. CF is set
33 if a one-bit was carried out of the high-order position (addition
34 instructions <A HREF="ADD.htm">ADD</A>, <A HREF="ADC.htm">ADC</A>, <A HREF="AAA.htm">AAA</A>, and
35 <A HREF="DAA.htm">DAA</A>) or if a one-bit was carried (i.e.
36 borrowed) into the high-order bit (subtraction instructions
37 <A HREF="SUB.htm">SUB</A>,
38 <A HREF="SBB.htm">SBB</A>,
39 <A HREF="AAS.htm">AAS</A>,
40 <A HREF="DAS.htm">DAS</A>,
41 <A HREF="CMP.htm">CMP</A>, and
42 <A HREF="NEG.htm">NEG</A>).
43 <P>
44 If the integer is signed, both SF and OF should be tested. SF always has
45 the same value as the sign bit of the result. The most significant bit (MSB)
46 of a signed integer is the bit next to the -- 6 of a byte, bit 14 of
47 a word, or bit 30 of a doubleword. OF is set in either of these cases:
48 <UL>
49 <LI> A one-bit was carried out of the MSB into the sign bit but no one bit
50 was carried out of the sign bit (addition instructions
51 <A HREF="ADD.htm">ADD</A>,
52 <A HREF="ADC.htm">ADC</A>,
53 <A HREF="INC.htm">INC</A>,
54 <A HREF="AAA.htm">AAA</A>, and
55 <A HREF="DAA.htm">DAA</A>).
56 In other words, the result was greater than the greatest
57 positive number that could be contained in the destination operand.
58 <LI> A one-bit was carried from the sign bit into the MSB but no one bit
59 was carried into the sign bit (subtraction instructions
60 <A HREF="SUB.htm">SUB</A>,
61 <A HREF="SBB.htm">SBB</A>,
62 <A HREF="DEC.htm">DEC</A>,
63 <A HREF="AAS.htm">AAS</A>,
64 <A HREF="DAS.htm">DAS</A>,
65 <A HREF="CMP.htm">CMP</A>, and
66 <A HREF="NEG.htm">NEG</A>). In other words, the result was smaller that
67 the smallest negative number that could be contained in the destination
68 operand.
69 </UL>
70 These status flags are tested by executing one of the two families of
71 conditional instructions:
72 <A HREF="Jcc.htm">Jcc</A> (jump on condition cc) or
73 <A HREF="SETcc.htm">SETcc</A> (byte set on condition).
75 <H2>3.2.1 Addition and Subtraction Instructions</H2>
76 <A HREF="ADD.htm">ADD</A> (Add Integers)
77 replaces the destination operand with the sum of the
78 source and destination operands. Sets CF if overflow.
79 <P>
80 <A HREF="ADC.htm">ADC</A> (Add Integers with Carry)
81 sums the operands, adds one if CF is set, and
82 replaces the destination operand with the result. If CF is cleared,
83 <A HREF="ADC.htm">ADC</A>
84 performs the same operation as the
85 <A HREF="ADD.htm">ADD</A> instruction. An
86 <A HREF="ADD.htm">ADD</A> followed by
87 multiple <A HREF="ADC.htm">ADC</A> instructions can be used to add
88 numbers longer than 32 bits.
89 <P>
90 <A HREF="INC>htm">INC</A> (Increment)
91 adds one to the destination operand.
92 <A HREF="INC.htm">INC</A> does not affect CF. Use
93 <A HREF="ADD.htm">ADD</A> with an immediate value of 1 if
94 an increment that updates carry (CF) is needed.
95 <P>
96 <A HREF="SUB.htm">SUB</A> (Subtract Integers)
97 subtracts the source operand from the destination
98 operand and replaces the destination operand with the result. If a borrow is
99 required, the CF is set. The operands may be signed or unsigned bytes,
100 words, or doublewords.
102 <A HREF="SBB.htm">SBB</A> (Subtract Integers with Borrow)
103 subtracts the source operand from the
104 destination operand, subtracts 1 if CF is set, and returns the result to the
105 destination operand. If CF is cleared,
106 <A HREF="SBB.htm">SBB</A> performs the same operation as
107 <A HREF="SUB.htm">SUB</A>. <A HREF="SUB.htm">SUB</A> followed by multiple
108 <A HREF="SBB.htm">SBB</A> instructions may be used to subtract
109 numbers longer than 32 bits. If CF is cleared,
110 <A HREF="SBB.htm">SBB</A> performs the same operation as
111 <A HREF="SUB.htm">SUB</A>.
113 <A HREF="DEC.htm">DEC</A> (Decrement)
114 subtracts 1 from the destination operand.
115 <A HREF="DEC.htm">DEC</A> does not update CF. Use
116 <A HREF="SUB.htm">SUB</A> with an immediate value of 1 to perform a
117 decrement that affects carry.
119 <H2>3.2.2 Comparison and Sign Change Instruction</H2>
120 <A HREF="CMP.htm">CMP</A> (Compare)
121 subtracts the source operand from the destination operand. It
122 updates OF, SF, ZF, AF, PF, and CF but does not alter the source and
123 destination operands. A subsequent
124 <A HREF="Jcc.htm">Jcc</A> or
125 <A HREF="SETcc.htm">SETcc</A> instruction can test the appropriate flags.
127 <A HREF="NEG.htm">NEG</A> (Negate)
128 subtracts a signed integer operand from zero. The effect of
129 <A HREF="NEG.htm">NEG</A> is to reverse the sign of the operand
130 from positive to negative or from negative to positive.
132 <H2>3.2.3 Multiplication Instructions</H2>
133 The 80386 has separate multiply instructions for unsigned and signed
134 operands.
135 <A HREF="MUL.htm">MUL</A> operates on unsigned numbers, while
136 <A HREF="IMUL.htm">IMUL</A> operates on signed
137 integers as well as unsigned.
139 <A HREF="MUL.htm">MUL</A> (Unsigned Integer Multiply)
140 performs an unsigned multiplication of the
141 source operand and the accumulator. If the source is a byte, the processor
142 multiplies it by the contents of AL and returns the double-length result to
143 AH and AL. If the source operand is a word, the processor multiplies it by
144 the contents of AX and returns the double-length result to DX and AX. If the
145 source operand is a doubleword, the processor multiplies it by the contents
146 of EAX and returns the 64-bit result in EDX and EAX.
147 <A HREF="MUL.htm">MUL</A> sets CF and OF
148 when the upper half of the result is nonzero; otherwise, they are cleared.
150 <A HREF="IMUL.htm">IMUL</A> (Signed Integer Multiply)
151 performs a signed multiplication operation.
152 <A HREF="IMUL.htm">IMUL</A> has three variations:
153 <OL>
154 <LI>A one-operand form. The operand may be a byte, word, or doubleword
155 located in memory or in a general register. This instruction uses EAX
156 and EDX as implicit operands in the same way as the
157 <A HREF="MUL.htm">MUL</A> instruction.
158 <LI>A two-operand form. One of the source operands may be in any general
159 register while the other may be either in memory or in a general
160 register. The product replaces the general-register operand.
161 <LI>A three-operand form; two are source and one is the destination
162 operand. One of the source operands is an immediate value stored in
163 the instruction; the second may be in memory or in any general
164 register. The product may be stored in any general register. The
165 immediate operand is treated as signed. If the immediate operand is a
166 byte, the processor automatically sign-extends it to the size of the
167 second operand before performing the multiplication.
168 </OL>
169 The three forms are similar in most respects:
170 <UL>
171 <LI> The length of the product is calculated to twice the length of the
172 operands.
173 <LI> The CF and OF flags are set when significant bits are carried into the
174 high-order half of the result. CF and OF are cleared when the
175 high-order half of the result is the sign-extension of the low-order half.
176 </UL>
177 However, forms 2 and 3 differ in that the product is truncated to the
178 length of the operands before it is stored in the destination register.
179 Because of this truncation, OF should be tested to ensure that no
180 significant bits are lost. (For ways to test OF, refer to the
181 <A HREF="INT.htm">INTO</A> and <A HREF="PUSHF.htm">PUSHF</A> instructions.)
183 Forms 2 and 3 of
184 <A HREF="IMUL.htm">IMUL</A> may also be used with unsigned operands because,
185 whether the operands are signed or unsigned, the low-order half of the
186 product is the same.
188 <H2>3.2.4 Division Instructions</H2>
189 The 80386 has separate division instructions for unsigned and signed
190 operands.
191 <A HREF="DIV.htm">DIV</A> operates on unsigned numbers, while
192 <A HREF="IDIV.htm">IDIV</A> operates on signed
193 integers as well as unsigned. In either case, an exception (interrupt zero)
194 occurs if the divisor is zero or if the quotient is too large for AL, AX, or
195 EAX.
197 <A HREF="DIV.htm">DIV</A> (Unsigned Integer Divide)
198 performs an unsigned division of the
199 accumulator by the source operand. The dividend (the accumulator) is twice
200 the size of the divisor (the source operand); the quotient and remainder
201 have the same size as the divisor, as the following table shows.
202 <PRE>
203 Size of Source Operand
204 (divisor) Dividend Quotient Remainder
205 Byte AX AL AH
206 Word DX:AX AX DX
207 Doubleword EDX:EAX EAX EDX
208 </PRE>
209 Non-integral quotients are truncated to integers toward 0. The remainder is
210 always less than the divisor. For unsigned byte division, the largest
211 quotient is 255. For unsigned word division, the largest quotient is 65,535.
212 For unsigned doubleword division the largest quotient is 2^(32) -1.
214 <A HREF="IDIV.htm">IDIV</A> (Signed Integer Divide)
215 performs a signed division of the accumulator
216 by the source operand.
217 <A HREF="IDIV.htm">IDIV</A> uses the same registers as the
218 <A HREF="DIV.htm">DIV</A> instruction.
220 For signed byte division, the maximum positive quotient is +127, and the
221 minimum negative quotient is -128. For signed word division, the maximum
222 positive quotient is +32,767, and the minimum negative quotient is -32,768.
223 For signed doubleword division the maximum positive quotient is 2^(31) -1,
224 the minimum negative quotient is -2^(31). Non-integral results are truncated
225 towards 0. The remainder always has the same sign as the dividend and is
226 less than the divisor in magnitude.
228 <HR>
230 <B>up:</B> <A HREF="c03.htm">
231 Chapter 3 -- Applications Instruction Set</A><BR>
232 <B>prev:</B> <A HREF="s03_01.htm">3.1 Data Movement Instructions</A><BR>
233 <B>next:</B> <A HREF="s03_03.htm">3.3 Decimal Arithmetic Instructions</A>
234 </BODY>