Pages

Wednesday, 25 September 2013

Little Endian vs Big Endian

Little Endian and Big Endian refers to the order in which multi-byte numbers are stored in memory. The  Juno CPU is Little Endian like Intel chips used in PCs and the 6502.

What is Endianess? 
It is easier to demonstrate Endianess using hexadecimal numbers, which is one of the reasons why system programmers like using hexadecimal so much as it makes things much simpler.

Take the two byte hexadecimal value 0xFF08 (65288 in decimal) in Big Endian notation the 2 byte value 0xFF08 is stored in memory most significant byte first as follows

Address 100 = FF
Address 101 = 08

In Little Endian notion the same two byte value is stored in memory least significant byte first

Address 100 = 08
Address 101 = FF

If you read the contents of memory as part of a memory dump then Big Endian notation would be written left to right as FF08 whereas Little Endian notation would be written 08FF. 

Note that byte order Endianess does not apply to the order in which the individual bits within a byte are stored only the bytes themselves.

So why would anybody want to use Little Endian notation as it appears backwards and awkward to read? 

There are many articles on the web discussing the advantages and disadvantages of Little vs big Endian and even many articles arguing there are no real advantages or disadvantages to either.

For the Juno PC there is one clear advantage of using Little Endian which is why I have chosen to use it. Little Endian makes some multi-byte operations easier to implement in micro-code because the program counter will be pointing to the least significant byte following the fetch of the instruction and the least significant bytes need to be added first in order to calculate the carry in multi-byte operations

For example, consider an instruction to jump relative by a two byte offset e.g. JMR #1000. In micro-code this instruction is implemented by adding the 2 byte value to the program counter in an intermediate register such as MAR and loading PC with the result. After fetching the JMR instruction the PC will point to the least significant byte of the offset. This can be fetched and added to MAR and the carry will be calculated ready for the addition of the second byte to MAR. If the value was stored in Big Endian order then the PC would need to be incremented to point to the LSB and then decremented to point to the MSB which would require more micro-code steps and hence clock cycles.




No comments:

Post a Comment