Hardware manual

The 8051 Instruction Set
1-8 Atmel 8051 Microcontrollers Hardware Manual
4316A–8051–01/04
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 0012345678XX
MOV R0, # 2DH 0012345678XX
loop for R1 = 2EH:
LOOP: MOV A, @R1 001234567878
XCHD A, @R0 001234587876
SWAP A 001234587867
MOV @R1, A 001234586767
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 080123456700
XCH A,2AH 000123456708