
The 8051 Instruction Set
Atmel 8051 Microcontrollers Hardware Manual 1-8
4316E–8051–01/07
To right-shift by an odd number of digits, a one-digit shift must be executed. Table 1-6
shows a sample of code that will right-shift a BCD number one digit, using the XCHD
instruction. Again, the contents of the registers holding the number and of the Accumu-
lator are shown alongside each instruction.
First, pointers R1 and R0 are set up to point to the two bytes containing the last four
BCD digits. Then a loop is executed which leaves the last byte, location 2EH, holding
the last two digits of the shifted number. The pointers are decremented, and the loop is
Table 1-5. Shifting a BCD Number Two Digits to the Right
2A 2B 2C 2D 2E ACC
MOV A,2EH
MOV 2EH, 2DH
MOV 2DH, 2CH
MOV 2CH, 2BH
MOV 2BH, # 0
00
00
00
00
00
12
12
12
12
00
34
34
34
12
12
56
56
34
34
34
78
56
56
56
56
78
78
78
78
78
Note: Using direct MOVs: 14 bytes, 9 µs
2A 2B 2C 2E 2E ACC
CLR A
XCH A,2BH
XCH A,2CH
XCH A,2DH
XCH A,2EH
00
00
00
00
00
12
00
00
00
00
34
34
12
12
12
56
56
56
34
34
78
78
78
78
56
00
12
34
56
78
Note: Using XCHs: 9 bytes, 5 µs
Table 1-6. Shifting a BCD Number One Digit to the Right
2A 2B 2C 2D 2E ACC
MOV R1,# 2EH 00 12 34 56 78 XX
MOV R0, # 2DH 00 12 34 56 78 XX
loop for R1 = 2EH:
LOOP: MOV A, @R1 00 12 34 56 78 78
XCHD A, @R0 00 12 34 58 78 76
SWAP A 00 12 34 58 78 67
MOV @R1, A 00 12 34 58 67 67
DEC R1 00 12 34 58 67 67
DEC R0 00 12 34 58 67 67
CJNE R1, #2AH, LOOP
loop for R1 = 2DH: 00 12 38 45 67 45
loop for R1 = 2CH: 00 18 23 45 67 23
loop for R1 = 2BH: 08 01 23 45 67 01
CLR A 08 01 23 45 67 00
XCH A,2AH 00 01 23 45 67 08
Comentários a estes Manuais