Arithmetic flags • The 8051 has four arithmetic flags: the carry (C), auxiliary carry (AC), overflow (OV), and parity (P). • Not all instructions affect arithmetic flags. Instructions Affecting Flags Incrementing and Decrementing No math flags (C, AC, OV) are affected Notes • No math flags are affected. • Increment and decrement instructions that operate on a port direct address alter the latch for that port. • DPTR is 16 bits; DPTR overflows from FFFFh to 0000h. • The 8-bit address contents underflow from 00h to FFh. • There is no DEC DPTR to match the INC DPTR. Examples Addition • C flag is set to 1 if there is a carry out of bit position 7. • AC flag is set to 1 if there is a carry out of bit position 3. • OV flag is set to 1 if there is a carry out of bit position 7, but not bit position 6 or if there is a carry out of bit position 6 but not bit position 7, which may be expressed as the logical operation OV = C7 XOR C6 • All addition is done with A reg as a destination. Unsigned and Signed Addition • Unsigned or signed numbers are considered by the programmer. • Unsigned byte 0-> FFh. • Signed byte MSB = 0 -> +ve number. 00 -> 127 00 -> 7Fh • Signed byte MSB =1 -> -ve number (2’s comp) -1 -> -128 FFh ->80h • Only C flag are used in unsigned addition and subtraction • Adding or subtracting unsigned numbers may generate a carry flag when the sum exceeds FFh or a borrow flag when the minuend is less than the subtrahend. • The OV flag is not used for unsigned addition and subtraction. • Adding or subtracting signed numbers can lead to carries and borrows in a similar manner, and to overflow conditions due to the actions of the sign bits. Unsigned Addition Example Signed Addition • Adding unlike signed numbers results to always correct answer. • If OV flag is set when adding liked signed numbers complement the sign bit and take into account that the sum exceeds the largest positive or negative number. Example of adding two liked sign numbers Multiple-Byte Arithmetic • ADDC is normally used to add a carry after the LSB addition in a multi-byte process. • ADD is normally used for the LSB addition. Subtraction • SUBB (subtract with borrow) is used for subtraction. • A is always the destination • SUBB A, source ; A = A - source – C subtraction • Remember to clear C flag before subtracting the 1st byte in multi-byte subtraction. Unsigned and Signed Subtraction • C flag is now thought of as a borrow flag to account for situations when a larger number is subtracted from a smaller number. • OV flag indicates results that must be adjusted whenever two numbers of unlike signs are subtracted and the result exceeds the planned signed magnitudes. Unsigned Subtraction • If C = 0 the result is true • If C = 1 the source is larger than A and the result is in 2’s complement form. Signed Subtraction • Subtraction of liked signed numbers results to always correct answer. • If OV flag is set when subtracting unlike signed numbers complement the sign bit and take into account that the sum exceeds the largest positive or negative number. Multiplication and Division • Multiplication and division treat the numbers in registers A and B as unsigned. • MUL AB ;Multiply A by B - put the low-order byte of the product in A, put the high-order byte in B. • OV flag will be set if A x B > FFh. • In the following, there is an error in lines 3&4 The correction • mov a,#7bh ; A =7Bh mov b,#2 ; B = 02h mul ab ; B = 00h and A = F6h; OV =0 mov b,#0feh ; B = FEh mul ab ; A = 14h and B = F4h; OV =1 Division • DIV AB ; Divide A by B, put the integer part of quotient in register A and the integer part of the remainder in B. • The OV flag is cleared to 0 unless B holds 00h before the DIV. Then the OV flag is set to 1 to show division by 0. Decimal Arithmetic • DA A ; Adjust the sum of two packed BCD numbers found in A register; leave the adjusted number in A. • C is set if the adjustment exceeded 99 BCD.