Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | roger-hill |
View: | 222 times |
Download: | 2 times |
Working With Main Memory
Why Main Memory
• Register space limited• Used for communication
Sections of Code
• .data sectionThings to place into memory at start
• .text sectionCode
Any order, canhave multiple.text/.datasegments
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
+0 +1 +2 +3 +3 +2 +1 +0
Single Byte Structures
• Agree on address of single byte structures– Just look different
+0 +1 +2 +3 +3 +2 +1 +0
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– 0x1001000 for MARS– 0x1000000 for reading
simulator
Finding Memory
• Memory reference consist of
offset(base)– base address : start here– offset : Go forward this many bytes
6(0x1001000)
3 2 1 0 7 6 5 4
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 0x1001000
– Move over 16 bytes from there– Read a word into $9
LUI & Base Register
lw $9, 12($8)
• Need address of .text section (0x10010000)in register
• Could do:
ori $8, $0, 0x1001sll $8, $8, 16
LUI & Base Register
• lui : load upper immediate– Place pattern in upper half of register
lui $8, 0x1001 10010000
Offsets
Locations in order declared:
x : 0(0x10010000)y: 4(0x10010000)
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
3 2 1 0 7 6 5 4 11 10 9 8
aa
Offsets
• Locations in order declared– Must be aligned
3 2 1 0 7 6 5 4 11 10 9 8
aa ff ff ff ff
Offsets
• Locations in order declared– Must be aligned
3 2 1 0 7 6 5 4 11 10 9 8
aa ff ff ff ff bb
Offsets
• Locations in order declared– Must be aligned
3 2 1 0 7 6 5 4 11 10 9 8
aa ff ff ff ff cc cc bb
Offsets
• Alternative order
Offsets
• Alternative order
3 2 1 0 7 6 5 4 11 10 9 8
aa
Offsets
• Alternative order
3 2 1 0 7 6 5 4 11 10 9 8
bb aa
Offsets
• Alternative order
3 2 1 0 7 6 5 4 11 10 9 8
cc cc bb aa
Offsets
• Alternative order
3 2 1 0 7 6 5 4 11 10 9 8
cc cc bb aa ff ff ff ff
Byte Packing
• Byte packing : ordering class/struct membersfor 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
• Resultf f f f f f b b
1111 1111 1111 1111 1111 1111 1011 1011
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
• Result0 0 0 0 0 0 b b
0000 0000 0000 0000 0000 0000 1011 1011
Horner's Method
• Reduce multiplications to evaluate polynomial• Ex:
6x3 – 3x2 + 7x + 2First, 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:6x2 - 3x Add the coefficient of the next term:6x2 - 3x + 7 Next, multiply that sum by x:6x3 - 3x2 + 7x Finally, add the coefficient of the last term:6x3 - 3x2 + 7x + 2