Download presentation
Presentation is loading. Please wait.
Published byReynard Hunter Modified over 8 years ago
1
Arithmetic Instructions and Programs
2
Outlines Range of numbers in 8051 unsigned data Range of numbers in 8051 unsigned data Addition & subtraction instructions for unsigned data Addition & subtraction instructions for unsigned data BCD system of data representation BCD system of data representation Packed and unpacked BCD data Packed and unpacked BCD data Addition & subtraction on BCD data Addition & subtraction on BCD data Range of numbers in 8051 signed data Range of numbers in 8051 signed data Signed data arithmetic instructions Signed data arithmetic instructions Carry & overflow problems & corrections Carry & overflow problems & corrections
3
Addition of Unsigned Numbers ADDA, source; A = A + source ADDA, source; A = A + source
5
Addition of Individual Bytes
6
ADDC & Addition of 16-bit Numbers 1 3CE7 3B8D 7874 +
7
BCD Number System Unpacked BCD: 1 byte Unpacked BCD: 1 byte Packed BCD: 4 bits Packed BCD: 4 bits
8
Adding BCD Numbers & DA Instruction MOVA,#17H ADDA,#28H MOVA,#47H;A=47H first BCD operandMOVB,#25H;B=25 second BCD operand ADDA,B;hex (binary) addition (A=6CH) DAA;adjust for BCD addition (A=72H) HEXBCD 290010 1001 +18 +0001 1000 +18 +0001 1000 410100 0001AC=1 + 6 + 0110 + 6 + 0110 470100 0111
9
Example
10
Subtraction of Unsigned Numbers SUBBA, source; A = A – source – CY SUBBA, source; A = A – source – CY SUBB when CY = 0 SUBB when CY = 0 Take 2 ’ s complement of subtraend (source)Take 2 ’ s complement of subtraend (source) Add it to minuendAdd it to minuend Invert carryInvert carry
11
Example (Positive Result)
12
Example (Negative Result)
13
SUBB When CY = 1 For multibyte numbers For multibyte numbers
14
Multiplication of Unsigned Numbers MULAB; A B, place 16-bit result in B and A MULAB; A B, place 16-bit result in B and A MOVA,#25H;load 25H to reg. A MOVB,#65H;load 65H in reg. B MULAB;25H * 65H = E99 where ;B = 0EH and A = 99H Table 6-1:Unsigned Multiplication Summary (MUL AB) Multiplication Operand 1 Operand 2 Result byte byte AB A=low byte, B=high byte
15
Division of Unsigned Numbers DIVAB; divide A by B DIVAB; divide A by B MOVA,#95H;load 95 into A MOVB,#10H;load 10 into B DIVAB;now A = 09 (quotient) and ;B = 05 (remainder) Table 6-2:Unsigned Division Summary (DIV AB) DivisionNumerator Denominat or Quotient Remainde r byte / byte ABAB
16
Example ( 1 of 2 )
17
Example ( 2 of 2 )
18
Signed 8-bit Operands Covert to 2 ’ s complement Covert to 2 ’ s complement Write magnitude of number in 8-bit binary (no sign)Write magnitude of number in 8-bit binary (no sign) Invert each bitInvert each bit Add 1 to itAdd 1 to it
19
Example
20
Example
21
Example
22
Byte-sized Signed Numbers Ranges DecimalBinaryHex -1281000 000080 -1271000 000181 -1261000 001082 …. ………….. -21111 1110FE -11111 1111FF 00000 000000 00000 000000 +10000 000101 +20000 001002 ……………... +1270111 11117F
23
Overflow in Signed Number Operations
24
When Is the OV Flag Set? Either: there is a carry from D6 to D7 but no carry out of D7 (CY = 0) Either: there is a carry from D6 to D7 but no carry out of D7 (CY = 0) Or: there is a carry from D7 out (CY = 1) but no carry from D6 to D7 Or: there is a carry from D7 out (CY = 1) but no carry from D6 to D7
25
Example
26
Example
27
Example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.