0% found this document useful (0 votes)
152 views44 pages

Arithmetic & Logic Instructions and Programs: The 8051 Microcontroller and Embedded Systems: Using Assembly and C

The document discusses arithmetic instructions and programs for the 8051 microcontroller. It covers addition, subtraction, and operations with different data types like binary coded decimal. Key points include: 1) The ADD instruction is used to add operands, with the destination always being register A. Memory-to-memory addition is not allowed. 2) When adding 16-bit numbers, the ADDC instruction propagates carries between bytes to get the correct sum. 3) The DA instruction is used after addition to adjust results for binary coded decimal format, ensuring numbers sum to a valid BCD value. 4) Subtraction uses the SUBB instruction, which subtracts the source and the carry flag. To perform
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)
152 views44 pages

Arithmetic & Logic Instructions and Programs: The 8051 Microcontroller and Embedded Systems: Using Assembly and C

The document discusses arithmetic instructions and programs for the 8051 microcontroller. It covers addition, subtraction, and operations with different data types like binary coded decimal. Key points include: 1) The ADD instruction is used to add operands, with the destination always being register A. Memory-to-memory addition is not allowed. 2) When adding 16-bit numbers, the ADDC instruction propagates carries between bytes to get the correct sum. 3) The DA instruction is used after addition to adjust results for binary coded decimal format, ensuring numbers sum to a valid BCD value. 4) Subtraction uses the SUBB instruction, which subtracts the source and the carry flag. To perform
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/ 44

ARITHMETIC & LOGIC

INSTRUCTIONS AND
PROGRAMS

The 8051 Microcontroller and Embedded


Systems: Using Assembly and C
Mazidi, Mazidi and McKinlay
ADD A,source ;A = A + source
ARITHMETIC  The instruction ADD is used to add two
INSTRUCTIONS 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
PF =1, because the number
Solution:
of 1s is zero (an even
F5H 1111 0101
number), PF is set to 1.
+ 0BH + 0000 1011 AC =1, since there is a
100H 0000 0000 carry from D3 to D4

2
Assume that RAM locations 40 – 44H have the following values.
ARITHMETIC Write a program to find the sum of the values. At the end of the
program, register A should contain the low byte and R7 the high byte.
INSTRUCTIONS
40 = (7D)
41 = (EB)
Additionof 42 = (C5)
Individual 43 = (5B)
44 = (30)
Bytes
Solution:
MOV R0,#40H ;load pointer
MOV R2,#5 ;load counter
CLR A ;A=0
MOV R7,A ;clear R7
AGAIN: ADD A,@R0 ;add the byte ptr to by R0
JNC NEXT ;if CY=0 don’t add carry
INC R7 ;keep track of carry
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is zero

3
 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).
Additionof16- + 3B 8D The carry is propagated to the
78 74 higher byte, which result in 3C
BitNumbers + 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
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

4
 The binary representation of the digits
ARITHMETIC 0 to 9 is called BCD (Binary Coded
INSTRUCTIONS Decimal) Digit BCD
0 0000
 Unpacked BCD 1 0001
BCDNumber  In unpacked BCD, the 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
9 1001
 PackedBCD
 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

5
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.

6
DA A ;decimal adjust for addition
ARITHMETIC  The DA instruction is provided to
INSTRUCTIONS correct the aforementioned 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.

7
 Summary of DA instruction
ARITHMETIC  After an ADD or ADDC instruction
INSTRUCTIONS 1. If the lower nibble (4 bits) is greater than 9, or
ifAC=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.

8
Assume that 5 BCD data items are stored in RAM locations starting
at 40H, as shown below. Write a program to find the sum of all the
ARITHMETIC numbers. The result must be in BCD.
INSTRUCTIONS 40=(71)
41=(11)
42=(65)
DA Instruction 43=(59)
(cont’) 44=(37)
Solution:
MOV R0,#40H ;Load pointer
MOV R2,#5 ;Load counter
CLR A ;A=0
MOV R7,A ;Clear R7
AGAIN: ADD A,@R0 ;add the byte pointer
;to by R0
DA A ;adjust for BCD
JNC NEXT ;if CY=0 don’t
;accumulate carry
INC R7 ;keep track of carries
NEXT: INC R0 ;increment pointer
DJNZ R2,AGAIN ;repeat until R2 is 0

9
 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

10
 SUBB when CY=0
ARITHMETIC 1. Take the 2’s complement of the
INSTRUCTIONS subtraction (source operand)
2. Add it to the minuend(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
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

11
 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 CY = 1
Unsigned MOV A,#62H ;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.

12
 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

13
 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
MUL 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

14
(a) Write a program to get hex data in the range of 00 – FFH from
port 1 and convert it to decimal. Save it in R7, R6 and R5.
ARITHMETIC (b) Assuming that P1 has a value of FDH for data, analyze program.
INSTRUCTIONS Solution:
(a)
MOV A,#0FFH
Application for MOV P1,A ;make P1 an input port
MOV A,P1 ;read data from P1
DIV MOV B,#10 ;B=0A hex
DIV AB ;divide by 10
MOV R7,B ;save lower digit
MOV B,#10
DIV AB ;divide by 10 once more
MOV R6,B ;save the next digit
MOV R5,A ;save the last digit
(b) To convert a binary (hex) value to decimal, we divide it by 10
repeatedly until the quotient is less than 10. After each division the
remainder is saves.
Q R
FD/0A = 19 3 (low digit)
19/0A = 2 5 (middle digit)
2 (high digit)
Therefore, we have FDH=253.

15
 D7 (MSB) is the sign and D0 to D6 are
SIGNED the magnitude of the number
ARITHMETIC
 If D7=0, the operand is positive, and if
INSTRUCTIONS D7=1, it is negative
D7 D6 D5 D4 D3 D2 D1 D0
Signed8-bit
Operands
Sign Magnitude
 Positive numbers are 0 to +127
 Negative number representation (2’s
complement)
1. Write the magnitude of the number in 8-bit
binary(nosign)
2. Invert each bit
3. Add 1 to it

16
Show how the 8051 would represent -34H
Solution:
SIGNED
1. 0011 0100 34H given in binary
ARITHMETIC 2. 1100 1011 invert each bit
INSTRUCTIONS 3. 1100 1100 add 1 (which is CC in hex)
Signed number representation of -34 in 2’s complement is CCH
Signed 8-bit
Decimal Binary Hex
Operands
(cont’) -128 1000 0000 80
-127 1000 0001 81
-126 1000 0010 82
... ...... ...
-2 1111 1110 FE
-1 1111 1111 FF
0 0000 0000 00
+1 0000 0001 01
+2 0000 0010 02
... ...... ...
+127 0111 1111 7F

17
 If the result of an operation on signed
SIGNED numbers is too large for the register
ARITHMETIC
INSTRUCTIONS  An overflow has occurred and the
programmer must be noticed
Overflow Examine the following code and analyze the result.
Problem MOV A,#+96 ;A=0110 0000 (A=60H)
MOV R1,#+70 ;R1=0100 0110(R1=46H)
ADD A,R1 ;A=1010 0110
;A=A6H=-90,INVALID
Solution:
+96 0110 0000
+ +70 0100 0110
+ 166 1010 0110 and OV =1
According to the CPU, the result is -90, which is wrong. The CPU
sets OV=1 to indicate the overflow

18
 In 8-bit signed number operations,
SIGNED
ARITHMETIC
OV is set to 1 if either occurs:
INSTRUCTIONS 1. There is a carry from D6 to D7, but no
carry out of D7(CY=0)
OVFlag 2. There is a carry from D7 out (CY=1), but
no carry from D6 to D7
MOV A,#-128 ;A=1000 0000(A=80H)
MOV R4,#-2 ;R4=1111 1110(R4=FEH)
ADD A,R4 ;A=0111 1110(A=7EH=+126,INVALID)
-128 1000 0000
+ -2 1111 1110
-130 0111 1110 and OV=1

OV = 1
The result +126 is wrong

19
MOV A,#-2 ;A=1111 1110(A=FEH)
MOV R1,#-5 ;R1=1111 1011(R1=FBH)
SIGNED ADD A,R1 ;A=1111 1001(A=F9H=-7,
ARITHMETIC ;Correct, OV=0)
INSTRUCTIONS -2 1111 1110
+ -5 1111 1011
-7 1111 1001 and OV=0
OVFlag
(cont’) OV = 0
The result -7 is correct
MOV A,#+7 ;A=0000 0111(A=07H)
MOV R1,#+18 ;R1=0001 0010(R1=12H)
ADD A,R1 ;A=0001 1001(A=19H=+25,
;Correct,OV=0)
7 0000 0111
+ 18 0001 0010
25 0001 1001 and OV=0

OV = 0
The result +25 is correct

20
 In unsigned number addition, we must
SIGNED
ARITHMETIC
monitor the status of CY(carry)
INSTRUCTIONS  Use JNC or JC instructions
 In signed number addition, the OV
OVFlag (overflow) flag must be monitored by
(cont’)
the programmer
 JB PSW.2 orJNB PSW.2

21
 To make the 2’s complement of a
SIGNED
ARITHMETIC
number
INSTRUCTIONS
CPL A ;1’s complement (invert)
ADD A,#1 ;add 1 to make 2’s comp.
2's
Complement

22
ANL destination,source
LOGIC AND ;dest = dest AND source
COMPARE  This instruction will perform a logic
INSTRUCTIONS
AND on the two operands and place
the result in the destination
AND
 The destination is normally the
accumulator
 The source operand can be a register, in
memory, or immediate

Show the results of the following.


X Y X AND Y
MOV A,#35H ;A = 35H
0 0 0
ANL A,#0FH ;A = A AND 0FH
0 1 0
35H 0 0 1 1 0 1 0 1 ANL is often used to
1 0 0 mask (set to 0) certain
0FH 0 0 0 0 1 1 1 1
1 1 1 05H 0 0 0 0 0 1 0 1 bits of an operand

23
ORL destination,source
LOGIC AND ;dest = dest OR source
COMPARE  The destination and source operands
INSTRUCTIONS are ORed and the result is placed in
the destination
OR  The destination is normally the
accumulator
 The source operand can be a register, in
memory, or immediate

X Y X OR Y
Show the results of the following.
0 0 0 MOV A,#04H ;A = 04
ORL A,#68H ;A = 6C
0 1 1
ORL instruction can be
1 0 1 04H 0 0 0 0 0 1 0 0 used to set certain bits
68H 0 1 1 0 1 0 0 0 of an operand to 1
1 1 1
6CH 0 1 1 0 1 1 0 0

24
XRL destination,source
LOGIC AND ;dest = dest XOR source
COMPARE
INSTRUCTIONS
 This instruction will perform XOR
operation on the two operands and
XOR place the result in the destination
 The destination is normally the
accumulator
 The source operand can be a register, in
memory, or immediate
X Y X XOR Y Show the results of the following.
00 0 MOV A,#54H
01 1 XRL A,#78H
XRL instruction can be
10 1 54H 0 1 0 1 0 1 0 0 used to toggle certain
11 0 78H 0 1 1 1 1 0 0 0 bits of an operand
2CH 0 0 1 0 1 1 0 0

25
The XRL instruction can be used to clear the contents of a register by
LOGIC AND XORing it with itself. Show how XRL A,A clears A, assuming that
AH = 45H.
COMPARE
45H 0 1 0 0 0 1 0 1
INSTRUCTIONS
45H 0 1 0 0 0 1 0 1
00H 0 0 0 0 0 0 0 0
XOR
(cont’) Read and test P1 to see whether it has the value 45H. If it does, send
99H to P2; otherwise, it stays cleared.
XRL can be used to
Solution: see if two registers
MOV P2,#00 ;clear P2 have the same value
MOV P1,#0FFH ;make P1 an input port
MOV R3,#45H ;R3=45H
MOV A,P1 ;read P1
XRL A,R3
JNZ EXIT ;jump if A is not 0
MOV P2,#99H
EXIT: ... If both registers have the same
value, 00 is placed in A. JNZ
and JZ test the contents of the
nce and Information Engineering
accumulator.
HANEL 26
CPL A ;complements the register A
LOGIC AND
COMPARE  This is called 1’s complement
INSTRUCTIONS MOV A, #55H
CPL A ;now A=AAH
;0101 0101(55H)
Complement ;becomes 1010 1010(AAH)
Accumulator
 To get the 2’s complement, all we
have to do is to add 1 to the 1’s
complement

27
CJNE destination,source,rel. addr.
LOGIC AND
COMPARE  The actions of comparing and jumping
INSTRUCTIONS are combined into a single instruction
called CJNE (compare and jump if not
Compare equal)
Instruction  The CJNE instruction compares two
operands, and jumps if they are not equal
 The destination operand can be in the
accumulator or in one of the Rn registers
 The source operand can be in a register, in
memory, or immediate
 The operands themselves remain unchanged
 It changes the CY flag to indicate if the
destination operand is larger or smaller
28
CJNE R5,#80,NOT_EQUAL ;check R5 for 80
LOGIC AND ... ;R5 = 80
COMPARE NOT_EQUAL:
INSTRUCTIONS JNC NEXT ;jump if R5 > 80
... ;R5 < 80
NEXT: ...
Compare
Instruction Compare Carry Flag
(cont’) destination source CY=0
destination< source CY=1
CY flag is always
checked for cases
of greater or less  Notice in the CJNE instruction that any
than, but only after Rn register can be compared with an
it is determined that immediate value
they are not equal
 There is no need for register A to be
involved

29
 The compare instruction is really a
LOGIC AND subtraction, except that the operands
COMPARE remain unchanged
INSTRUCTIONS  Flags are changed according to the
execution of the SUBB instruction
Compare Write a program to read the temperature and test it for the value 75.
According to the test results, place the temperature value into the
Instruction registers indicated by the following.
(cont’) If T = 75 then A = 75
If T < 75 then R1 = T
If T > 75 then R2 = T
Solution:
MOV P1,#0FFH ;make P1 an input port
MOV A,P1 ;read P1 port
CJNE A,#75,OVER ;jump if A is not 75
SJMP EXIT ;A=75, exit
OVER: JNC NEXT ;if CY=0 then A>75
MOV R1,A ;CY=1, A<75, save in R1
SJMP EXIT ; and exit
NEXT: MOV R2,A ;A>75, save it in R2
EXIT: ...

30
RR A ;rotate right A
ROTATE
INSTRUCTION  In rotate right
AND DATA  The 8 bits of the accumulator are rotated
SERIALIZATION right one bit,and
 Bit D0 exits from the LSB and enters into
Rotating Right MSB,D7
and Left

MSB LSB

MOV A,#36H ;A = 0011 0110


RR A ;A = 0001 1011
RR A ;A = 1000 1101
RR A ;A = 1100 0110
RR A ;A = 0110 0011

31
RL A ;rotate left A
ROTATE
INSTRUCTION  In rotate left
AND DATA  The 8 bits of the accumulator are rotated
SERIALIZATION left one bit, and
 Bit D7 exits from the MSB and enters into
RotatingRight LSB,D0
and Left
(cont’)
MSB LSB

MOV A,#72H ;A = 0111 0010


RL A ;A = 1110 0100
RL A ;A = 1100 1001

32
RRC A ;rotate right through carry
ROTATE
INSTRUCTION  In RRCA
AND DATA  Bits are rotated from left to right
SERIALIZATION  They exit the LSB to the carry flag, and
the carry flag enters the MSB
Rotating
through Carry
MSB LSB CY

CLR C ;make CY = 0
MOV A,#26H ;A = 0010 0110
RRC A ;A = 0001 0011 CY = 0
RRC A ;A = 0000 1001 CY = 1
RRC A ;A = 1000 0100 CY = 1

33
RLC A ;rotate left through carry
ROTATE
INSTRUCTION  In RLCA
AND DATA  Bits are shifted from right to left
SERIALIZATION  They exit the MSB and enter the carry flag,
and the carry flag enters the LSB
Rotating
through Carry
(cont’) CY MSB LSB

Write a program that finds the number of 1s in a given byte.


MOV R1,#0
MOV R7,#8 ;count=08
MOV A,#97H
AGAIN: RLC A
JNC NEXT ;check for CY
INC R1 ;if CY=1 add to count
NEXT: DJNZ R7,AGAIN

34
 Serializing data is a way of sending a
ROTATE
INSTRUCTION byte of data one bit at a time through
AND DATA a single pin of microcontroller
SERIALIZATION  Using the serial port, discussed in Chapter
10
Serializing Data  To transfer data one bit at a time and
control the sequence of data and spaces
in between them

35
 Transfer a byte of data serially by
ROTATE
INSTRUCTION  Moving CY to any pin of ports P0– P3
AND DATA  Using rotate instruction
SERIALIZATION Write a program to transfer value 41H serially (one bit at a time)
via pin P2.1. Put two highs at the start and end of the data. Send the
byte LSB first.
Serializing Data Solution:
(cont’) MOV A,#41H
SETB P2.1 ;high
SETB P2.1 ;high
MOV R5,#8
AGAIN: RRC A
MOV P2.1,C ;send CY to P2.1
DJNZ R5,HERE
SETB P2.1 ;high
SETB P2.1 ;high
Pin
Register A CY P2.1
D7 D0

36
Write a program to bring in a byte of data serially one bit at a time
ROTATE via pin P2.7 and save it in register R2. The byte comes in with the
INSTRUCTION LSB first.
AND DATA Solution:
SERIALIZATION MOV R5,#8
AGAIN: MOV C,P2.7 ;bring in bit
RRC A
SerializingData DJNZ R5,HERE
(cont’) MOV R2,A ;save it
Pin
P2.7 CY Register A
D7 D0

37
 There are several instructions by which
ROTATE
INSTRUCTION the CY flag can be manipulated directly
AND DATA Instruction Function
SERIALIZATION SETB C MakeCY=1
CLR C Clearcarrybit(CY=0)
Single-bit CPL C Complementcarrybit
MOV b,C Copycarrystatustobitlocation(CY=b)
Operationswith
MOV C,b Copybitlocationstatustocarry(b=CY)
CY JNC target JumptotargetifCY=0
JC target JumptotargetifCY=1
ANL C,bit ANDCYwithbitandsaveitonCY
ANL C,/bit ANDCYwithinvertedbitandsaveitonCY
ORL C,bit ORCYwithbitandsaveitonCY
ORL C,/bit ORCYwithinvertedbitandsaveitonCY

38
Assume that bit P2.2 is used to control an outdoor light and bit P2.5
ROTATE a light inside a building. Show how to turn on the outside light and
INSTRUCTION turn off the inside one.
AND DATA Solution:
SERIALIZATION SETB C ;CY = 1
ORL C,P2.2 ;CY = P2.2 ORed w/ CY
MOV P2.2,C ;turn it on if not on
Single-bit CLR C ;CY = 0
ANL C,P2.5 ;CY = P2.5 ANDed w/ CY
Operationswith MOV P2.5,C ;turn it off if not off
CY
(cont’) Write a program that finds the number of 1s in a given byte.
Solution:
MOV R1,#0 ;R1 keeps number of 1s
MOV R7,#8 ;counter, rotate 8 times
MOV A,#97H ;find number of 1s in 97H
AGAIN: RLC A ;rotate it thru CY
JNC NEXT ;check CY
INC R1 ;if CY=1, inc count
NEXT: DJNZ R7,AGAIN ;go thru 8 times

39
SWAP A
ROTATE
INSTRUCTION  It swaps the lower nibble and the
AND DATA higher nibble
SERIALIZATION  In other words, the lower 4 bits are put
into the higher 4 bits and the higher 4 bits
SWAP are put into the lower 4 bits
 SWAP works only on the accumulator
(A)
before : D7-D4 D3-D0

after : D3-D0 D7-D4

40
(a) Find the contents of register A in the following code.
ROTATE (b) In the absence of a SWAP instruction, how would you
INSTRUCTION exchange the nibbles? Write a simple program to show the
AND DATA process.
SERIALIZATION Solution:
(a)
SWAP MOV A,#72H ;A = 72H
(cont’) SWAP A ;A = 27H
(b)
MOV A,#72H ;A = 0111 0010
RL A ;A = 0111 0010
RL A ;A = 0111 0010
RL A ;A = 0111 0010
RL A ;A = 0111 0010

41
BCDANDASCII ASCII code and BCD for digits 0 - 9
APPLICATION Key ASCII (hex) Binary BCD (unpacked)

PROGRAMS 0 30 0110000 00000000


1 31 0110001 00000001
2 32 0110010 00000010
3 33 0110011 00000011
4 34 0110100 00000100
5 35 0110101 00000101
6 36 0110110 00000110
7 37 0110111 00000111
8 38 0111000 00001000
9 39 0111001 00001001

42
 The DS5000T microcontrollers have a
BCDANDASCII real-time clock(RTC)
APPLICATION
PROGRAMS  The RTC provides the time of day (hour,
minute, second) and the date (year,
month, day) continuously, regardless of
PackedBCDto whether the power is on or off
ACSII
Conversion  However this data is provided in
packed BCD
 To be displayed on an LCD or printed by
the printer, it must be in ACSII format
Packed BCD Unpacked BCD ASCII

29H 02H & 09H 32H & 39H


0010 1001 0000 0010 & 0011 0010 &
0000 1001 0011 1001

43
 ToconvertASCIItopackedBCD
BCDANDASCII
APPLICATION  It is first converted to unpacked BCD (to
PROGRAMS get rid of the 3)
 Combined to make packed BCD
ASCIIto
key ASCII Unpacked BCD Packed BCD
PackedBCD
Conversion 4 34 0000 0100
7 37 0000 0111 0100 0111 or 47H

MOV A, #’4’ ;A=34H, hex for ‘4’


MOV R1,#’7’ ;R1=37H,hex for ‘7’
ANL A, #0FH ;mask upper nibble (A=04)
ANL R1,#0FH ;mask upper nibble (R1=07)
SWAP A ;A=40H
ORL A, R1 ;A=47H, packed BCD

Department of Computer Science and Information Engineering


HANEL National Cheng Kung University, TAIWAN 44

You might also like