I'm writing a ZX Spectrum emulator in C# "for fun" (I think I regret starting already).
I know the Z80 is little endian, so in RAM the low-order byte is stored first.
What I can't seem to find a definitive answer to how it behaves with a 16 bit LD
instruction.
For example:
LD BC, 0x1234
After this, does B
contain 0x12
and C
contain 0x34
or vice-versa?
So, after the above, if I execute:
LD A, 0x56
LD (BC), A
Will memory 0x1234
or 0x3412
contain 0x56
?
If you are able to answer this, please provide a link to a reliable source... I have Googled the crap out of this and can't find an absolute definitive answer.
Thanks in advance,
Steve.
CodePudding user response:
Endian-ness only comes into play when we break a 16-bit value into two 8-bit pieces, and there is a choice of approaches, which happens when such a 16-bit value is stored in memory so, for example:
With the encoding of immediates in instructions, such as
ld bc, 0x1234
: looking at the individual bytes of the machine code for this instruction, we will see 0x01 0x34 0x12, storing the 0x1234 in little endian byte order.With a 16-bit value as data in memory, and looking at the individual bytes in memory, needs an endian-ness, so use little endian.
However, if the BC
register pair is loaded with a value 0x1234, then that's the memory address that indirect use of BC
will work with. There's no issue of endian-ness with this, since we're not looking at the individual bytes in this scenario.
Now, if you want to know how the BC
register composes from B
and C
then we have to know which is the high order and which is the low order.
From the manual:
PUSH BC is PUSH B then C
So, that means that the stack memory will have the value from C
then the value from B
at the address 1 higher than where C
was stored. Since 16-bit data in memory is stored using little endian byte order, we can conclude that C is the low order and B is the high order of the BC
16-bit register pair.
Thus, BC
is B
* 256 C
. Let's note though, that this is simply by definition, but doesn't really go to endian-ness — since there's no "addressing" of the individual bytes, there's also no other ordering as there is with memory (due to memory's having of both addresses and values).
However, the choice of which is high and which is low doesn't change that together they hold the value 0x1234, as per our examples.
CodePudding user response:
Is a manufacturer datasheet reliable source? Z80 user manual And of course in memory after opcode is firstly 0x34 then 0x12