Little-endian

From NES Hacker Wiki
Revision as of 14:13, 4 May 2013 by TheAlmightyGuru (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The 6502 that powers the Nintendo stores its memory in Little-Endian rather than big-endian. This affects the stored order of multiple-byte values in the processor. The difference between the two can be seen in the table below:

Hex Value 2-Byte Value
Little-Endian $1000 00 10
Big-Endian $1000 10 00

The 6502 has no native support for 2-byte integers, so its endianness doesn't apply to them, but memory addresses are often stored as 2-bytes. Thankfully, compilers of 6502 assembly don't require you to input memory addresses in little-endian. Instead, they swap the two bytes for you when a program is compiled.

However, if you're reading 6502 machine code from a debugger or from the ROM, the memory addresses will be stored in little-endian. For example, if you wanted to load memory from address $1000 using assembly, the code would look like this:

LDA $1000          ; Loads A with whatever value is in $1000.

However, if you open the compiled machine code in a hex editor, it will look like this:

AD 00 10      

Notice how the low and high bytes of the memory address are swapped by the compiler due to the 6502 using little-endian.