39
Working With Main Memory

Working With Main Memory. Why Main Memory Register space limited Used for communication

Embed Size (px)

Citation preview

Page 1: Working With Main Memory. Why Main Memory Register space limited Used for communication

Working With Main Memory

Page 2: Working With Main Memory. Why Main Memory Register space limited Used for communication

Why Main Memory

• Register space limited• Used for communication

Page 3: 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

Page 4: Working With Main Memory. Why Main Memory Register space limited Used for communication

Defining Memory

• Memory described as words/bytes/asciiz, etc…

Hex:

Ascii

Page 5: Working With Main Memory. Why Main Memory Register space limited Used for communication

Endianess

• Endianess : bytes order of a word in main memory

Page 6: Working With Main Memory. Why Main Memory Register space limited Used for communication

Little vs Big Endian

• Big is "Normal":

• Little weird– Words in order– Bytes in a word backwards

Page 7: Working With Main Memory. Why Main Memory Register space limited Used for communication

Integers Normal…

• Both endians store words (integers) same way– Disagree about how bytes are numbered

+0 +1 +2 +3 +3 +2 +1 +0

Page 8: Working With Main Memory. Why Main Memory Register space limited Used for communication

Single Byte Structures

• Agree on address of single byte structures– Just look different

+0 +1 +2 +3 +3 +2 +1 +0

Page 9: Working With Main Memory. Why Main Memory Register space limited Used for communication

Endian

• MARS is little endian:– Bytes/strings look goofy– Words (integers) unaffected

Page 10: Working With Main Memory. Why Main Memory Register space limited Used for communication

Why?

• Little Endian– Read different sized value at same address

• Big Endian– Easier to read hex– Reading start of word gives sign/magnitude

Page 11: Working With Main Memory. Why Main Memory Register space limited Used for communication

Base Register

• Data starts in defined location– 0x1001000 for MARS– 0x1000000 for reading

simulator

Page 12: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 13: Working With Main Memory. Why Main Memory Register space limited Used for communication

Arrays

• C++ Array– Base address– Offset : number of elements

int quiz[3];

quiz[1] = 5;

Page 14: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 15: Working With Main Memory. Why Main Memory Register space limited Used for communication

LUI & Base Register

lw $9, 12($8)

• Need address of .text section (0x10010000)in register

• Could do:

ori $8, $0, 0x1001sll $8, $8, 16

Page 16: Working With Main Memory. Why Main Memory Register space limited Used for communication

LUI & Base Register

• lui : load upper immediate– Place pattern in upper half of register

lui $8, 0x1001 10010000

Page 17: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

Locations in order declared:

x : 0(0x10010000)y: 4(0x10010000)

Page 18: Working With Main Memory. Why Main Memory Register space limited Used for communication

Accessing Memory

• x = x + y where x and y are in main memory:

Page 19: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 20: Working With Main Memory. Why Main Memory Register space limited Used for communication

Labels

• Labels usually used to specify memory locations– Not allowed for now

Page 21: Working With Main Memory. Why Main Memory Register space limited Used for communication

Labels

• Behind the scenes…– lw/sw with label = 2 instructions

Page 22: Working With Main Memory. Why Main Memory Register space limited Used for communication

Weirdness

• sizeof(2 chars and an int) != sizeof(2 chars and an int)

Page 23: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 24: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Locations in order declared– Must be aligned

Page 25: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Locations in order declared– Must be aligned

3 2 1 0 7 6 5 4 11 10 9 8

aa

Page 26: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 27: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 28: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 29: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Alternative order

Page 30: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Alternative order

3 2 1 0 7 6 5 4 11 10 9 8

aa

Page 31: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Alternative order

3 2 1 0 7 6 5 4 11 10 9 8

bb aa

Page 32: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Alternative order

3 2 1 0 7 6 5 4 11 10 9 8

cc cc bb aa

Page 33: Working With Main Memory. Why Main Memory Register space limited Used for communication

Offsets

• Alternative order

3 2 1 0 7 6 5 4 11 10 9 8

cc cc bb aa ff ff ff ff

Page 34: Working With Main Memory. Why Main Memory Register space limited Used for communication

Byte Packing

• Byte packing : ordering class/struct membersfor proper alignment

Page 35: Working With Main Memory. Why Main Memory Register space limited Used for communication

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

Page 36: Working With Main Memory. Why Main Memory Register space limited Used for communication

LB Sign Extension

• Code:

• Memory

• Resultf f f f f f b b

1111 1111 1111 1111 1111 1111 1011 1011

Page 37: Working With Main Memory. Why Main Memory Register space limited Used for communication

Loading Bytes

• Loads sign extend value– Perfect for numeric data

• lbu/lhu load unsigned– no sign extension– use for chars, etc…

Page 38: Working With Main Memory. Why Main Memory Register space limited Used for communication

LB Sign Extension

• Code:

• Memory

• Result0 0 0 0 0 0 b b

0000 0000 0000 0000 0000 0000 1011 1011

Page 39: Working With Main Memory. Why Main Memory Register space limited Used for communication

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