Working With Main Memory
Why Main Memory Register space limited Used for communication
Sections of Code.data section Things to place into memory at start.text section Code Any order, can have multiple.text/.data segments
Defining Memory Memory described as words/bytes/asciiz, etc… Hex: Ascii
Endianess Endianess : bytes order of a word in main memory
Little vs Big Endian Big is "Normal": Little weird – Words in order – Bytes in a word backwards
Integers Normal… Both endians store words (integers) same way – Disagree about how bytes are numbered
Single Byte Structures Agree on address of single byte structures – Just look different
Endian MARS is little endian: – Bytes/strings look goofy – Words (integers) unaffected
Why? Little Endian – Read different sized value at same address Big Endian – Easier to read hex – Reading start of word gives sign/magnitude
Base Register Data starts in defined location – 0x for MARS – 0x for reading simulator
Finding Memory Memory reference consist of offset(base) – base address : start here – offset : Go forward this many bytes 6(0x )
Arrays C++ Array – Base address – Offset : number of elements int quiz[3]; quiz[1] = 5;
Load Word / Store Word Memory access – lw : get word from memory – put in register – sw : put register value into memory lw $targetRegister, offset($baseAddressRegister) lw $9, 12($8) – Start from address in register $8 Should have 0x – Move over 16 bytes from there – Read a word into $9
LUI & Base Register lw $9, 12($8) Need address of.text section (0x ) in register Could do: ori $8, $0, 0x1001 sll $8, $8, 16
LUI & Base Register lui : load upper immediate – Place pattern in upper half of register lui $8, 0x1001
Offsets Locations in order declared: x : 0(0x ) y: 4(0x )
Accessing Memory x = x + y where x and y are in main memory:
Load Delay Load delay: Can't use register in instruction after it is loaded – Real MIPS hardware – Tutorial's SPIM simulator – Don't worry about for MARS
Labels Labels usually used to specify memory locations – Not allowed for now
Labels Behind the scenes… – lw/sw with label = 2 instructions
Weirdness sizeof(2 chars and an int) != sizeof(2 chars and an int)
Memory Alignment Memory alignment : restrictions on where read/write can start – Want to read 4 bytes: Start on multiple of 4 – Want to read 2 bytes: Start on multiple of 2 – Want to read 1 byte: Start on any byte
Offsets Locations in order declared – Must be aligned
Offsets Locations in order declared – Must be aligned aa
Offsets Locations in order declared – Must be aligned aaff
Offsets Locations in order declared – Must be aligned aaff bb
Offsets Locations in order declared – Must be aligned aaff cc bb
Offsets Alternative order
Offsets Alternative order aa
Offsets Alternative order bbaa
Offsets Alternative order cc bbaa
Offsets Alternative order cc bbaaff
Byte Packing Byte packing : ordering class/struct members for proper alignment
Loading Bytes lb $dest, offset(base) : load byte sb $source, offset(base) : store byte lh $dest, offset(based) : load half word sh $ source, offset(base) : store half word
LB Sign Extension Code: Memory Result f f f f f f b b
Loading Bytes Loads sign extend value – Perfect for numeric data lbu/lhu load unsigned – no sign extension – use for chars, etc…
LB Sign Extension Code: Memory Result b b
Horner's Method Reduce multiplications to evaluate polynomial Ex: 6x 3 – 3x 2 + 7x + 2 First, put the coefficient of the first term into the accumulator: 6 Next, multiply that value by x: 6x Add the coefficient of the next term: 6x - 3 Next, multiply that sum by x: 6x 2 - 3x Add the coefficient of the next term: 6x 2 - 3x + 7 Next, multiply that sum by x: 6x 3 - 3x 2 + 7x Finally, add the coefficient of the last term: 6x 3 - 3x 2 + 7x + 2