assembly8-bitz80bit-representation

What is the representation of values in the LD instruction in the 8-bit Z80 processor?


I am having difficulties with understanding what's going on in this code:

LD A, -1;
LD B, 130;
ADD A, B;

And what flags are set to 1 after the ADD instruction.

Basically, I don't know, what value is stored inside the register B.

I cannot find any information whether 130 in LD B, 130 means "1000 0010" or "0 1000 0010" (so subsequently we've got to get rid of the MSB/LSB - I don't know which one).

As a result, I am not sure what is the final value stored in A.


Solution

  • The Z80 is an 8-bit processor, therefore your 9-bit number 0 1000 0010 has no relevance. The code you posted

    LD A, -1;
    LD B, 130;
    ADD A, B;
    

    is equivalent to

    LD A, 0hFF
    LD B, 0h82
    ADD A, B
    

    and after the addition register A will contain 0h81

    The addition will cause the Carry flag to be set, since it generates a "borrow". The Carry is a result of unsigned addition.

    It will cause the Overflow flag to be clear, since there is no "internal carry" from bit 6 to bit 7. Both values were negative to start, and A remains negative. The Overflow Flag is set when the signed sum cannot be represented in the register correctly.

    The Zero Flag will be clear, since A is non zero.

    The Sign Flag will be set, since A is negative according to bit 7.