Data arrangement - Big-endian Little-endian
Data arrangement based on design of CPU and bus.
A stored value may span several bytes of storage and may be larger
that the size of the data bus.
Little endian - The lower portion of the value being stored is stored
in the lower or starting address of memory.
Common in machines that (historically) use an 8 bit data bus.
Even if current designs have larger data buses.
Big endian - The higher portion of the value being stored is stored
in the starting address of memory.
Common in machines initially designed with 16/32 bit data buses.
Storing the 32 bit hex value 1A140042 and the string "howdy" starting at
memory address sequence start at @10
Memory locations
@ 10 11 12 13 14 15 16 17 18
Big endian
1A 14 00 42 o h d w y
Little endian
6f 68 64 77 0a 79 h o w d y
Unicode includes a flag to indicate endian-ness.
Working with numbers.
MOS Technology 6502 8-bit architecture
; assume 4 bytes of 1st 32-bit number store at $2000-$2003 and
; 2nd number at $2010-$2013 in low endian form.
; numbers in parentheses are clock cycles for instruction.
CLC ; (1) Clear carry flag C = 0;
LDA $2000 ; (4) Load low byte of data found at memory $2000 into accumulator
ADC $2010 ; (4) Add low byte of data found at $2010, also add carry flag (0)
; If resulting value is larger than 8-bits, carry set to 1.
STA $2020 ; (4) Store low byte at $2020;
LDA $2001 ; (4) Load 2nd byte 1st number
ADC $2011 ; (4) Add 2nd byte of 2nd number, add carry. If result is larger
; than 8-bit, carry set to 1 else carry set to 0.
STA $2021 ; (4) Store 2nd byte.
LDA $2002 ; (4) Load 3rd byte of 1st number.
ADC $2012 ; (4) Add 3rd byte of 2nd number, add carry. Adjust carry.
33 clock cycles to add two 24-bit numbers. A 32-bit number would require
an additional 12 clock cycles.
Store and repeat until all bytes added.
* Big-endian / little endian also affect how data is transferred between
systems.
Some CPUs (RISC) provide for the ability to handle both formats when
transferring data in to/out of systems.
But more often handled with software.
Unicode also has features that help warn systems which order multi-byte
Unicode characters are stored in.