You are on page 1of 42

成功大學工程科學系

Chapter 6

Arithmetic Instructions and


Programs

Spring'17 TW 8051
Hou 1
成功大學工程科學系

• 6.1Arithmetic Instructions
• 6.2 Signed number concepts and arithmetic
operations
• 6.3 Logic and compare instructions
• 6.4 Rotate instruction and data serialization
• 6.5 BCD, ASCII, and other application programs

Spring'17 TW Hou 8051


2
成功大學工程科學系

6-1 Arithmetic instructions


• Addition of unsigned numbers
• Addition of individual bytes
• ADDC and addition of 16-bit numbers
• BCD number system
• Unpacked BCD
• Packed BCD
• DA instruction
• Subtraction of unsigned number

Spring'17 TW Hou 8051


3
成功大學工程科學系

6-1 Arithmetic instructions


• Unsigned multiplication and division
– Multiplication of unsigned numbers
• MUL AB ; A x B, place 16-bit result
– Division of unsigned numbers

Spring'17 TW Hou 8051


4
成功大學工程科學系

Addition of unsigned numbers


• Unsigned numbers- all bits are used to represent
data, no bits are for the positive or negative sign.
• Operand value: 00H~FFH (8-bit)
– ADD A, source; A= A+ source
– Example 6-1

Spring'17 TW Hou 8051


5
成功大學工程科學系

Spring'17 TW 8051
Hou 6
成功大學工程科學系

Addition of individual bytes


• Carry flag should be checked, if we are to
calculate the sum of any number of operands
• Example6-2

Spring'17 TW Hou 8051


7
成功大學工程科學系

Spring'17 TW 8051
Hou 8
成功大學工程科學系

ADDC and addition of 16-bit numbers


• Add 16-bit numbers – propagation of a carry from
the lower byte to the higher byte.
• Use ADDC (add with carry) instruction
• Example 6-3: 3CE7H + 3B8DH

Spring'17 TW Hou 8051


9
成功大學工程科學系

Spring'17 TW 8051
Hou 10
成功大學工程科學系

BCD (binary coded decimal) number system

Spring'17 TW Hou 8051


11
成功大學工程科學系

Unpacked BCD
• Lower 4 bits represent the BCD number, the rest
of the bits are 0.
• Ex: unpacked BCD of ‘0000 1001”  BCD 9
• unpacked BCD of ‘0000 0101’ BCD 5

Spring'17 TW Hou 8051


12
成功大學工程科學系

Packed BCD
• A single byte contains two BCD numbers.
• Ex: packed BCD ‘0101 1001’ is 59H
• Packed BCD is more efficient to store data than
unpacked BCD
• NOTE: There is a problem: after adding packed
BCD numbers, the result is no longer BCD
• EX: MOV A,#17H; 17H + 28H = 3FH
• ADD A,#28H; 3FH is not a BCD
• ; the result should be 45H
• ; in BCD
Spring'17 TW Hou 8051
13
成功大學工程科學系

Packed BCD
• MOV A,#17H; 17H + 28H = 3FH
• ADD A,#28H; 3FH is not a BCD
• ; the result should be 45H ( in BCD)
• ;  add 6 to the lower digit: 3FH + 6H = 45H
• ;another example of higher digit:
• ;52H + 87H = D9H (52D + 87D = 139D)
• ; add 6 the the higher digit: D9H + 60H = 139H
• ; decimal adjustment

Spring'17 TW Hou 8051


14
成功大學工程科學系

DA (decimal adjust for addition) instruction


• Add 6 to the lower nibble or higher nibble if needed;
otherwise it will leave the result alone.
• EX: MOV A,#47H ; A=47H first BCD number
• MOV B, #25H; B=25H second BCD number
• ADD A,B; hex(binary) addition (A=6CH)
• DA A ; adjust for BCD addition (A=72H)
• DA instruction works only on A.
• DA must be used after addition of BCD operands and BCD
operands can never have any digit greater than 9
• DA works only after an ADD (ADDC) instruction. It will
not work after the INC instruction.

Spring'17 TW Hou 8051


15
成功大學工程科學系

Spring'17 TW 8051
Hou 16
成功大學工程科學系

Spring'17 TW 8051
Hou 17
成功大學工程科學系

Subtraction of unsigned numbers


• SUBB A, source ;A = A – source –CY
• In many microprocessors there are two different
instructions for subtraction: SUB and SUBB
(subtract with borrow).
• In 8051, only SUBB.
• To make SUB out of SUBB, we must make CY=0
prior to the execution of SUBB.
– Two cases for SUBB instruction:
– (1) CY=0
– (2) CY=1

Spring'17 TW Hou 8051


18
成功大學工程科學系

Subtraction with unsigned numbers


• Assume 8051 executing SUBB with CY=0, the
hardware operations:
– 1.Take the 2’s complement of the subtrahend (source)
– 2.Add it to the minuend (A)
– 3. Invert the carry.
– Example 6-5
– If after the execution of SUBB the CY=0, the result is
positive;
– if CY=1, the result is negative and the destination has
the 2’s complement of the result.
•  use CPL (1’s complement) and INC to get the 2’s
complement. (Example 6-6)

Spring'17 TW Hou 8051


19
成功大學工程科學系

• 注意: 硬體自動invert CY.

Spring'17 TW Hou 8051


20
成功大學工程科學系

Spring'17 TW 8051
Hou 21
成功大學工程科學系

SUBB with CY=1


• If CY=1 prior to executing the SUBB instruction,
it also subtracts 1 from the result.
• Used for multibyte numbers and will take care of
the borrow of the lower operand.
• Example 6-7

Spring'17 TW Hou 8051


22
成功大學工程科學系

Spring'17 TW 8051
Hou 23
成功大學工程科學系

Unsigned multiplication and division


• Use register A and B only.
• Multiplication of unsigned numbers
– MUL AB ; A x B, place 16-bit result
• Division of unsigned numbers

Spring'17 TW Hou 8051


24
成功大學工程科學系

Multiplication of unsigned numbers

– MUL AB ; A x B, place 16-bit result in B and A


– ; lower byte in A, higher byte in B
– EX: MOV A, #25H ; 25H x 65 H = E99H
– MOV B,#65H ; where B= 0EH,
– MUL AB ; A=99H

Spring'17 TW Hou 8051


25
成功大學工程科學系

Division of unsigned numbers


• Byte over byte only
• DIV AB ; divide A by B
• ; quotient in A, Remainder in B
• EX: MOV A,#95 ; Dive 95 by 10
• MOV B, #10 ; the result-A=09 (quotient)
• DIV AB ; B=05 (remainder)
• The instruction always make CY=0 and OV=0 if
the numerator is not 0.
• If the numerator is 0 (B=0), OV=1 indicates an
error, and CY=0. Divide by 0  OV=1
• Example 6-8 and 6-9 (舊版是同一題,新版為兩題)
Spring'17 TW Hou 8051
26
成功大學工程科學系

Spring'17 TW 8051
Hou 27
成功大學工程科學系

6.2 Signed numbers and arithmetic operations


• Concept of signed numbers in computers
• Signed 8-bit operands
• Positive numbers
• Negative numbers
• Overflow problem in signed number operations
• When is the OV flag set?
• Instructions to create 2’s complent

Spring'17 TW Hou 8051


28
成功大學工程科學系

• Positive numbers:
• Negative numbers: 2’s complement of a positive
number.
– Example 6-9,6-10,6-11

Spring'17 TW Hou 8051


29
成功大學工程科學系

Exampl 6-10

Spring'17 TW Hou 8051


30
成功大學工程科學系

Example 6-11

Spring'17 TW Hou 8051


31
成功大學工程科學系

Example 6-12

Spring'17 TW Hou 8051


32
成功大學工程科學系

Spring'17 TW 8051
Hou 33
成功大學工程科學系

Overflow problem in signed number operations


• Overflow : If the result of an operation on signed
numbers is too large for the register, an overflow
has occurred and the programmer must be notified.
• Overflow  OV flag = 1
• Programmer takes care of the error.
• Example 6-13 (舊版6-12)

Spring'17 TW Hou 8051


34
成功大學工程科學系

Example 6-13

Spring'17 TW Hou 8051


35
成功大學工程科學系

When is the OV flag set?


• In 8-bit signed operations, OV is set to 1
– There is a carry from D6 to D7 but no carry out of D7
(CY=0) (正數)
– There is a carry from D7 out (CY=1) but no carry from
D6 to D7 (負數)
–  overflow flag is set to 1 if there is a carry from D6
to D7 or from D7 out, but not both.
– Example 6-14,6-15,6-16.

Spring'17 TW Hou 8051


36
成功大學工程科學系

Example 6-14

Spring'17 TW Hou 8051


37
成功大學工程科學系

Example 6-15

Spring'17 TW Hou 8051


38
成功大學工程科學系

Example 6-16

Spring'17 TW Hou 8051


39
成功大學工程科學系

• Overflow flag: in any signed number addition, OV


indicates whether the result is valid or not. If
OV=1, the result is errornous; if OV=0, the result
is valid.
• In unsigned number addition, we must monitor the
status of CY. (We can use JC, JNC to branch right
after the addition of unsigned numbers)
• In signed number addition, we must monitor the
OV flag. (no branch instruction over the OV flag.)
But we can use JB PSW.2 or JNB PSW.2

Spring'17 TW Hou 8051


40
成功大學工程科學系

Instructions to Create 2’s Complement


• CPL A ; 1’s complement
• ADD A, #1 ; add 1 to make 2’s complement

Spring'17 TW Hou 8051


41
成功大學工程科學系

6-2 ends

Spring'17 TW 8051
Hou 42

You might also like