[go: up one dir, main page]

0% found this document useful (0 votes)
15 views17 pages

CH 6 - Arithmetic Logic Instruction and Programming Part - A

This document covers arithmetic and logic instructions in embedded system design, focusing on coding operations such as addition, subtraction, multiplication, and division, as well as logic operations like AND, OR, and EX-OR. It explains the BCD (binary coded decimal) system, including data representation, addition of BCD data, and the use of the DA instruction for BCD correction. Additionally, it discusses the handling of unsigned and signed numbers, and provides examples of programming techniques for these operations.

Uploaded by

jay061007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

CH 6 - Arithmetic Logic Instruction and Programming Part - A

This document covers arithmetic and logic instructions in embedded system design, focusing on coding operations such as addition, subtraction, multiplication, and division, as well as logic operations like AND, OR, and EX-OR. It explains the BCD (binary coded decimal) system, including data representation, addition of BCD data, and the use of the DA instruction for BCD correction. Additionally, it discusses the handling of unsigned and signed numbers, and provides examples of programming techniques for these operations.

Uploaded by

jay061007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

ARITHMETIC & LOGIC

INSTRUCTIONS AND
PROGRAMS
NMJ32404 Embedded System Design
Objectives
• Upon completion of this chapter, you will be able to:

• Code arithmetic instruction; addition, subtraction, multiplication, and division

• Code logic instructions; AND, OR, and EX-OR include bit manipulation.

• Use compare and jump instructions for program control, code rotate instruction and
data serialization.

• Explain the BCD (binary coded decimal) system data representation and Perform
addition of BCD data.

• Code ASCII and BCD data conversion


Objectives
• Sub-topic will be covered today:

✓Code arithmetic instruction; addition, subtraction, multiplication, and division


instructions

• Code logic instructions AND, OR, and EX-OR include bit manipulation.

• Use compare and jump instructions for program control, code rotate instruction and
data serialization.

✓Explain the BCD (binary coded decimal) system data representation and Perform
addition of BCD data.

• Code ASCII and BCD data conversion


What are you understand about
• Arithmetic Instruction
• Logic instruction
• Unsigned / singed number
• BCD number system
Discuss / explain of arithmetic instruction with the
following sequence:

“ADD” : 8-bit “ADDC” : 16- “DA”: BCD


BCD system
number bit number addition

“SUBB”: “MUL”: “DIV”:


Subtraction Multiplication Division
ADD A,source ;A = A + source
ARITHMETIC □ The instruction "ADD" is used to add
INSTRUCTIONS two operands
► Destination operand is always in register A
Addition of
► Source operand can be a register,
Unsigned immediate data, or in memory
Numbers
► Memory-to-memory arithmetic operations
are never allowed in 8051 Assembly
language
Show how the flag register is affected by the following instruction.
MOV A,#0F5H ;A=F5 hex CY =1, since there is a
ADD A,#0BH ;A=F5+0B=00 carry out from D7
P =0, because the number
Solution: of 1s is zero (an even
F5H 1111 0101 number)
+ 0BH + 0000 1011 AC =1, since there is a
100H 0000 0000 carry from D3 to D4
□ When adding two 16-bit data operands,
ARITHMETIC the propagation of a carry from lower
INSTRUCTIONS byte to higher byte is concerned
1 When the first byte is added
ADDC and 3C E7 (E7+8D=74, CY=1).
+ 3B 8D The carry is propagated to the
Addition of 16- 78 74 higher byte, which result in 3C
Bit Numbers + 3B + 1 =78 (all in hex)
Write a program to add two 16-bit numbers. Place the sum in R7 and R6;
R6 should have the lower byte.
Solution:
CLR C ;make CY=0
MOV A, #0E7H ;load the low byte now A=E7H
ADD A, #8DH ;add the low byte CY =1
MOV R6, A ;save the low byte sum in R6
MOV A, #3CH ;load the high byte
ADDC A, #3BH ;add with the carry
MOV R7, A ;save the high byte sum
□ The binary representation of the digits
ARITHMETIC 0 to 9 is called BCD (Binary Coded
INSTRUCTIONS
Decimal) Digit BCD

► Unpacked BCD 0 0000


1 0001
BCD Number ■In unpacked BCD, only lower 4 2 0010
System bits of the number represent the 3 0011
BCD number, and the rest of the 4 0100
bits are 0 5 0101
6 0110
■Ex. 00001001 and 00000101 are
7 0111
unpacked BCD for 9 and 5
8 1000
► Packed BCD 9 1001

■In packed BCD, a single byte has


two BCD number in it, one in the
lower 4 bits, and one in the
upper 4 bits
■Ex. 0101 1001 is packed BCD for
59H
ARITHMETIC □ Adding two BCD numbers must give a
INSTRUCTIONS BCD result Adding these two
numbers gives
Unpacked and MOV A, #17H 0011 1111B (3FH),
ADD A, #28H Which is not BCD!
Packed BCD
The result above should have been 17 + 28 = 45 (0100 0101).
To correct this problem, the programmer must add 6 (0110) to the
low digit: 3F + 06 = 45H.
DA A ;decimal adjust for addition
ARITHMETIC □ The "DA" instruction is provided to
INSTRUCTIONS correct the problem associated with
BCD addition
DA Instruction
► The DA instruction will add 6 to the lower
nibble or higher nibble if need
Example: 6CH
MOV A,#47H ;A=47H first BCD operand
MOV B,#25H ;B=25H second BCD operand
ADD A,B ;hex(binary) addition(A=6CH)
DA A ;adjust for BCD addition
(A=72H)
72H
DA works only
after an ADD, The “DA” instruction works only on A. In other word, while the source
but not after INC can be an operand of any addressing mode, the destination must be in
register A in order for DA to work.
□ Summary of DA instruction
ARITHMETIC ► After an ADD or ADDC instruction
INSTRUCTIONS 1. If the lower nibble (4 bits) is greater than 9, or
if AC=1, add 0110 to the lower 4 bits
DA Instruction 2. If the upper nibble is greater than 9, or if
(cont’) CY=1, add 0110 to the upper 4 bits

Example:
HEX BCD
29 0010 1001
+ 18 + 0001 1000
41 0100 0001 AC=1
+ 6 + 0110
47 0100 0111

Since AC=1 after the


addition, ”DA A” will add 6 to the
lower nibble.
The final result is in BCD format.
□ In many microprocessor there are two
ARITHMETIC different instructions for subtraction:
INSTRUCTIONS SUB and SUBB (subtract with borrow)
► In the 8051 we have only SUBB
Subtraction of
Unsigned ► The 8051 uses adder circuitry to perform
the subtraction
Numbers
SUBB A,source ;A = A – source – CY
□ To make SUB out of SUBB, we have to
make CY=0 prior to the execution of
the instruction
► Notice that we use the CY flag for the
borrow
□ SUBB when CY = 0
ARITHMETIC 1. Take the 2’s complement of the
INSTRUCTIONS source operand
2. addition result store in (A)
Subtraction of 3. Invert the carry
Unsigned CLR C
Numbers MOV A,#4C ;load A with value 4CH
(cont’) SUBB A,#6EH ;subtract 6E from A
JNC NEXT ;if CY=0 jump to NEXT
CPL A ;if CY=1, take 1’s complement
INC A ;and increment to get 2’s comp
NEXT: MOV R1,A ;save A in R1
G) 2’s
Solution: complement
4C 0100 1100 0100 1100
CY=0, the result is positive; - 6E 0110 1110 1001 0010 +
CY=1, the result is negative
-22 01101 1110
and the destination has the CY =1
2’s complement of the result ® Invert carry
□ SUBB when CY = 1
ARITHMETIC
► This instruction is used for multi-byte
INSTRUCTIONS numbers and will take care of the borrow
of the lower operand
Subtraction of A = 62H – 96H – 0 = CCH
CLR C
Unsigned MOV A,#62H
CY = 1
;A=62H
Numbers SUBB A,#96H ;62H-96H=CCH with CY=1
(cont’) MOV R7,A ;save the result
MOV A,#27H ;A=27H
SUBB A,#12H ;27H-12H-1=14H
MOV R6,A ;save the result
A = 27H - 12H - 1 = 14H
Solution: CY = 0
We have 2762H - 1296H = 14CCH.
□ The 8051 supports byte by byte
ARITHMETIC multiplication only
INSTRUCTIONS
The byte are assumed to be unsigned data

Unsigned MUL AB ;AxB, 16-bit result in B, A


Multiplication MOV A,#25H ;load 25H to reg. A
MOV B,#65H ;load 65H to reg. B
MUL AB ;25H * 65H = E99 where
;B = OEH and A = 99H

Unsigned Multiplication Summary (MUL AB)


Multiplication Operand1 Operand2 Result
Byte x byte A B B = high byte
A = low byte
□ The 8051 supports byte over byte
ARITHMETIC
INSTRUCTIONS
division only
The byte are assumed to be unsigned data

Unsigned DIV AB ;divide A by B, A/B
Division
MOV A,#95 ;load 95 to reg. A
MOV B,#10 ;load 10 to reg. B
DIV AB ;A = 09(quotient) and
;B = 05(remainder)

Unsigned Division Summary (DIV AB)


Division Numerator Denominator Quotient Remainder
Byte / byte A B A B

CY is always 0
If B  0, OV = 0
If B = 0, OV = 1 indicates error
Quizizz Time

You might also like