There are N bytes stored from m/m location 2500H. The value of N is stored in 2400H.How can I write an 8085 program to interchange (irrespective of the bit value) the bit Di with Dj for all bytes. The values of i=4 and j=0
1 Answers
Nobody wants to do your homework for you. That being said, here's how you can interchange i-th bit of an 1-byte data with the j-th bit.
First let's revisit the bitwise logical operators and their usages. Suppose we want to know whether the 4-th bit is set, we take a bit mask 0000 1000
(i.e. 08H
) and AND
it with the data. To clear the 2nd bit we take the bit mask 1111 1101
(i.e. FDH
) and AND
it with data. Whereas, to set the 6-th bit we take a bit mask 0000 0010
(i.e. 02H
) and OR
it with the data. To complement of flip the 4-th bit we take a bit mask 0000 1000
(i.e. 08H
) and XOR
it with the data.
Assuming the actual data is in register D, thus to exchange the 2nd bit with the 4-th one we may write:
MVI A, 08H ;i-th bit
ORI 02H ;j-th bit
ANI D ;only 2nd and 4bit of the data survives
JPE SKIP ;if both bits are same (both 0 or both 1) no exchange required
;if not we need a swap, which is this case can be done by flipping the both
MOV A,D ;bring back the data again
XRI 08H ;flip the i-th bit
XRI 02H ;flip the j-th bit
SKIP: MOV D,A ;put the data back to D
The bit mask canbe programmatically generated with appropritate number of shifts (or rotate without carry). And repeating this process on all of the n
bytes would complete the required task.

- 165
- 1
- 1
- 13