- 2070 Bhadra · 8 marks
8085 ALP: exchange bits D6 and D2
Short answer
For each byte, D6 and D2 are swapped in place. Rotate left 4 times so D2 moves to D6; XOR with original isolates D6 xor D2 at 40H, mask it, shift to D2 (04H), OR to get mask 44H if bits differ, else 00H. XOR with original toggles both bits, exchanging them. Processed 200 bytes.
Write an assembly language program for 8085 to exchange the bits D6 and D2 of every byte of a program. Suppose there are 200 bytes in the program starting from memory location 8090H.
Assumptions: the 200 bytes (C8H) start at 8090H and are exchanged in place.
Method: D2 and D6 are 4 positions apart. If the two bits are different, toggling both exchanges them; if equal, nothing changes. The byte is rotated left 4 times so D2 is in the D6 position, XOR with the original gives D2 xor D6 at D6, which is masked with 40H; copying it 4 places right gives the other bit position (04H), and OR gives the toggle mask 44H or 00H.
LXI H,8090H ; HL -> first byte of the program area
MVI D,0C8H ; counter = 200 decimal
LOOP: MOV A,M ; A = byte
MOV B,A ; save original byte
RLC ; rotate left 4 times: D2 comes to D6
RLC
RLC
RLC
XRA B ; D6 of A = D2 xor D6 of original
ANI 40H ; 40H if D2 and D6 differ, else 00H
MOV C,A
RRC ; 40H -> 04H (position D2)
RRC
RRC
RRC
ORA C ; 44H if they differ, else 00H
XRA B ; toggling both bits = exchanging them
MOV M,A ; write back in the same place
INX H
DCR D
JNZ LOOP
HLT
Check: byte 40H (D6 = 1, D2 = 0) gives mask 44H, so 40H xor 44H = 04H (bits exchanged).
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- 8085 ALP: interchange two bits of bytes · Microprocessor, chapter 2
- 8085 ALP: divide two bytes · Microprocessor, chapter 2
- 8085 ALP: square of ten numbers · Microprocessor, chapter 2
- 8085 ALP: odd parity transfer with bit change · Microprocessor, chapter 2
- 8085 ALP: transfer data with more than 3 ones · Microprocessor, chapter 2
- 8085 ALP: multiplication table of a number · Microprocessor, chapter 2
- Interfacing: 512-byte RAM with 8085 · Instrumentation II, chapter 2