---
title: "8085 ALP: exchange bits D6 and D2"
subject: "Microprocessor"
chapter: 2
key: "semester_4/microprocessor:alp-8085-exchange-d6-d2"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-exchange-d6-d2
site: BCT Engineering
---

# 8085 ALP: exchange bits D6 and D2

Microprocessor · Chapter 2: Programming with 8085 Microprocessor

- Asked in: 2070 Bhadra, 8 marks
- Video: [Swap d6 and d3](https://www.youtube.com/watch?v=fLubHjXoyWY) — AAkrit Subedi
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-exchange-d6-d2
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-2

## Question

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.

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

## Answer

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


```asm
        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).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What idea does the program use to exchange bits D6 and D2?
   A. Always invert both D6 and D2
   B. Clear D6 and D2, then set them both to 1
   C. Rotate the byte 4 times and store the rotated byte
   D. Toggle both bits if they differ; otherwise leave the byte unchanged
   Answer: D. When the two bits differ, toggling both swaps them. When they are equal, the swap changes nothing. So the mask is 44H or 00H, and the program XORs the byte with that mask. (Key point: Exchanging two bits = toggling both only when they differ)

2. Why is the byte rotated left four times (RLC x4) before XRA B?
   A. To bring D2 to the D6 position, so XRA B gives D2 xor D6 at D6
   B. To move D6 into the carry flag so that JC can test it
   C. To swap the nibbles so the two bits are exchanged directly
   D. To multiply the byte by 16 before masking with 40H
   Answer: A. After four RLCs, the original D2 is at D6. XRA B then puts D2 xor D6 at bit D6, and ANI 40H keeps only that bit. (Key point: Four RLCs bring D2 to the D6 position for comparison)

3. If D6 and D2 of a byte are different, what toggle mask is built before the final XRA B?
   A. 40H
   B. 04H
   C. 44H
   D. 00H
   Answer: C. ANI 40H gives 40H. Four RRCs copy it to 04H, and ORA C combines them into 44H, which toggles both D6 and D2. For example, 40H xor 44H = 04H. (Key point: The toggle mask is 44H when the bits differ)

4. How does the program process all 200 bytes from 8090H?
   A. D starts at 200H and the results go to a new table
   B. D starts at 0C8H; each byte is written back in place, then INX H, DCR D
   C. D starts at 0C8H and HL is decremented after each byte
   D. It loops until it finds a 00H byte in the table
   Answer: B. 200 decimal is C8H. Each processed byte is stored back at the same address, HL moves to the next byte, and DCR D/JNZ repeat the loop until the count reaches zero. (Key point: The loop counts 200 bytes (C8H) and writes each byte back in place)

## Related in Microprocessor

- [8085 ALP: interchange two bits of bytes](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-interchange-bits) — Microprocessor, chapter 2
- [8085 ALP: divide two bytes](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-divide-bytes) — Microprocessor, chapter 2
- [8085 ALP: square of ten numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-square-numbers) — Microprocessor, chapter 2
- [8085 ALP: odd parity transfer with bit change](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-odd-parity-transfer) — Microprocessor, chapter 2
- [8085 ALP: transfer data with more than 3 ones](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-ones-greater-than-3) — Microprocessor, chapter 2
- [8085 ALP: multiplication table of a number](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-multiplication-table) — Microprocessor, chapter 2

## Related in other subjects

- [Interfacing: 512-byte RAM with 8085](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Arag-ram-512-interface) — Instrumentation II, chapter 2
