Search code examples
assembly8085

Why XRA A also clears other registers too?


Let us suppose accumulator contains 0AH and following instructons are given. MOV D,A; XRA A; I found out that this instruction clears accumulator as well as the D register. I confirmed this by using 'Virtual 8085' simulator. Why the secon instruction clears the D register as well?


Solution

  • Ok, I looked at Virtual 8085's source code, and it does indeed look buggy as I suggested in my comment.

    It represents registers as BitArray instances. Or rather, each register is represented as an object with a bits member which is a reference to a BitArray.

    Now, what the author does to simulate MOV is to simply point one register's bits to another register's bits. Let's say that you do MOV D,A; after that MOV, D.bits is now referring to the same BitArray as A.bits.
    If you then perform a bitwise logic operation you're operating directly on A.bits, which will also affect D.bits since they refer to the same BitArray. The bug doesn't happen if you do e.g. an ADD or SUB, because they are implemented in a different way and create a brand new BitArray instead of modifying an existing one.

    It seems to me that MOV is implemented in a broken way and should use Clone, or possibly CopyTo instead of just =. File a bug about this on github if you want to see it fixed.


    TL;DR: XRA A does not clear D. Virtual 8085 is buggy. Try to contact its author to have him fix the bug, or search for a different simulator.