31
Memory and Addresses CS/COE 0447 Luís Oliveira Original slides by: Jarrett Billingsley Modified with bits from: Bruce Childers, David Wilkinson

Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Memory and AddressesCS/COE 0447 Luís Oliveira

Original slides by: Jarrett BillingsleyModified with bits from: Bruce Childers, David Wilkinson

Page 2: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Class announcements● Add/Drop period ends friday!

● Everyone joined slack? Links will/have? Expired

● Repeat after me:o Store copies from the CPU to memoryo Load copies from memory to CPU

2

Page 3: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Memory

3

Page 4: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

What is the memory?● The system memory is a piece of temporary storage hardwareo it's smaller and faster (more expensive!) than the persistent storage.§ maybe in the future it won't be temporary§ the line between system memory and persistent storage will fade

away…● It's where the programs and data that the computer is currently executing

and using resideo all the variables, all the functions, all the open files etc.o the CPU can only run programs from system memory!

4

Page 5: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Bytes, bytes, bytes● the memory is a big one-dimensional array of bytes● what do these bytes mean?o ¯\_(ツ)_/¯

● every byte value has an addresso this is its "array index"o addresses start at 0, like arrays in C/Java§ gee wonder where they got the idea

● when each byte has its own address, we call it a byte-addressable machineo not many non-byte-addressable machines these days

5

Addr Val

0 00

1 30

2 04

3 00

4 DE

5 C0

6 EF

7 BE

8 6C

9 34

A 00

B 01

C 02

Page 6: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

How much memory?● each address refers to one byte. if your addresses are n bits long… how

many bytes can your memory have?o 2n B

● machines with 32-bit addresses can access 232 B = 4GiB of memoryo with 64-bit addresses… 16EiB

6

WAIT GiB???EiB???

Page 7: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

kibi Mebi Gibi???

● kibi, Mebi, Gibi, Tebi, Pebi, Exbi are powers of 2o kiB = 210, MiB = 220, GiB = 230 etc.

● kilo, mega, giga, tera, peta, exa are ostensibly powers of 10o kB = 103, MB = 106, GB = 109 etc.

● but most people still say "kilo, mega" to mean the powers of 2o Hard drive manufacturers use the "power of 10"§ 1TB hard drive is 1012B… 1012 ÷ 240 = 909 GiB– now you know why it's like that

o Network devices also use “power of 10”§ So a 30Mbps is actually (30/8)*106B = 3.58MiBps

7

Page 8: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Words, words, words● for most things, we want to use wordso the "comfortable" integer size for the CPUo on this version of MIPS, it's 32b (4B)

● but our memory only holds bytes…● combine multiple bytes into larger valueso the CPU can handle this for uso but importantly, the data is still just bytes

● when we talk about values bigger than a byte…o the address is the address of their first byte§ the byte at the smallest address

o so what are the addresses of the three words here?

8

Addr Val

0 00

1 30

2 04

3 00

4 DE

5 C0

6 EF

7 BE

8 6C

9 34

A 00

B 01

C 02

Page 9: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Endianness

9

Page 10: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

A matter of perspective● let's say there's a word at address 4… made of 4 bytes● wh…what word do those 4 bytes represent?

10

Addr Val

... ...

7 DE

6 C0

5 EF

4 BE

... ...

…is it 0xBEEFC0DE?

…is it 0xDEC0EFBE?

Page 11: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Endianness● when interpreting a sequence of bytes as larger values, endianness is the

rule used to decide what order to put the bytes in

11

0xDEC0EFBE0xBEEFC0DE

big-endian means the “BIG address"

contains the END-byte

little-endian means the “LITTLE address“

contains the END-byteDE C0 EF BE0 1 2 3

nothing to do with value of bytes, only order

Page 12: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Which is better: little or big?● it doesn't matter.* as long as you're consistent, it's fine● for political (x86) reasons, most computers today are little-endian● but endianness pops up whenever you have sequences of bytes:o like in fileso or networkso or hardware buseso or… memory!

● which one is MIPS?o it's bi-endian, meaning it can be configured to work either wayo but MARS uses the endianness of the computer it's running on§ so little-endian for virtually everyone– cause x86

12*big endian is better

Page 13: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

What DOESN'T endianness affect?× the arrangement of the bits within a byteo it just changes meaning of order of the bytes § note the bytes are still DE, C0 etc.

× 1-byte values, arrays of bytes, ASCII strings…o single bytes don’t care about endianness at all

× the ordering of bytes inside the CPUo there's no need for e.g. "big-endian" arithmetico the CPU works with whole words

● endianness only affects moving/splitting data:o larger than single byteso between the CPU and memoryo or between multiple computers

13

0xBEEFC0DE0xED0CFEEB0xDEC0EFBEl l e H oH e l l o

Page 14: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Variables, Loads, Stores

14

Page 15: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Memory addresses● everything in memory has an addresso the position in memory where it begins§ where its first byte is

o this applies to variables, functions, objects, arrays etc.● a super important concept:

every variable really has two parts:an address and a value

● if you want to put a variable in memory…o first you need to figure out what address to put it ino this extremely tedious task is handled by assemblers§ whew

15

Page 16: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Putting a variable in memory● we can declare a global variable like this:

.datax: .word 4

● the Java/C equivalent would be static int x = 4;● .data says "I'm gonna declare variables"o you can declare as many as you want!o to go back to writing code, use .text

● if we assemble this little program and make sure Tools > Show Labels Window is checked, what do you see?o the assembler gave the variable that addresso it'll do that for every variable

16

name initial valuetype

Page 17: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Load-store architectures● in some architectures, many instructions can access memoryo x86-64: add [rsp-8], rcx§ adds the contents of rcx to the value at address rsp-8

● in a load-store architecture, all memory accesses are done with two kinds of instructions: loads and stores (like in MIPS)

17

Registers Memory

lw loads copy data from memory intoCPU registers

sw stores copy data from CPU registers into memory

Page 18: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Operating on variables in memory (animated)● we want to increment a variable that is in memoryo where do values have to be for the CPU to operate on them?o what do we want the overall outcome to be?

● so, what three steps are needed to increment that variable?1. load the value from memory into a register2. add 1 to the value in the register3. store the value back into memory

● every variable access works like this!!!o HLLs just hide this from you

18

445 5 x

Page 19: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Accessing memory in MIPS

19

Page 20: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

MIPS ISA: load and store instructions for words● you can load and store entire 32-bit words with lw and sw● the instructions look like this (variable names not important):lw t1, x # loads from variable x into t1sw t1, x # stores from t1 into variable x

● in MIPS, stores are written with the destination on the right. !?o well, you can remember it with this diagram…o the memory is "on the right" for both

loads and stores

20

Registers Memory

lw

sw

Page 21: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

MIPS ISA: load and store instructions for words● you can also load the 32-bit address of a variable with la

la t1, x # loads the ADDRESS of x into t1

21

Addr Val

0 04

1 00

2 00

3 00

4 DE

5 C0

6 EF

7 BE

8 6C

9 34

x

Page 22: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Read, modify, write● you now know enough to increment x!● But first, lets look at some assembly

● first we load x into a register● then…● and then…

● let's see what values are in t0 and memory after this program runs

22

lw t0, xadd t0, t0, 1sw t0, x

Page 23: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

It really is that simple● variables in asm aren't THAT scary● please don't be afraid of them● you just gotta remember to store if you wanna change em

23

Page 24: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Questions?

● Just in case I prepared some for you:

o Does load word (lw) put or get data from memory?

o I already know the word is the most “comfortable” size for the CPU, but are they the only size it can work with?

24

Page 25: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Smaller values

25

Page 26: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

Smaller numeric● MIPS also understands smaller and tiny datatypes

.datax: .word 4 => 0x00000004y: .half 4 => 0x0004z: .byte 4 => 0x04

26

Page 27: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

MIPS ISA: loading and storing 8/16-bit values● to load/store bytes, we use lb/sb● to load/store 16-bit (half-word) values, we use lh/sh● these look and work just like lw/sw, like:lb t0, tiny # loads a byte into t0sb t0, tiny # stores a byte into tinyo …or DO THEY?!?!?!?

● how big are registers?o what should go in those extra 16/24 bits then?§ ???

27

Page 28: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

can I get an extension● sometimes you need to widen a number with fewer bits to more● zero extension is easy: put 0s at the beginning.

10012 è to 8 bits è 0000 10012● but there are also signed numbers which we didn't talk about yeto the top bit (MSB) of signed numbers is the sign (+/-)

● sign extension puts copies of the sign bit at the beginning10012 è to 8 bits è 1111 1001200102 è to 8 bits è 0000 00102

o like spreading peanut butter§ we'll learn about why this is important later in the course

28

Page 29: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

E X P A N D V A L U E● if you load a byte…

29

31 000000000 00000000 00000000 00000000

10010000

31 011111111 11111111 11111111 10010000

If the byte is signed… what should it become?

31 000000000 00000000 00000000 10010000

If the byte is unsigned… what should it become?

lbdoessign extension.

lbu does zero extension.

Page 30: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

How does the CPU know whether it's signed or unsigned● do YOU think the CPU knows this?o no§ it doesn't– you have to use the right instruction.

● it's easy to mess this up● lbu in particular is usually what you want for byte variables but lb is one

character shorter and just looks so nice and consistent…o But don’t!

30

Page 31: Memory and Addresses - GitHub Pages · kilo, mega, giga, tera, peta, exaare ostensibly powers of 10 o kB = 10 3 , MB = 10 6 , GB = 10 9 etc. but most people still say "kilo, mega"

31 010100010 00001110 11111111 00000100

Truncation● if we go the other way, the upper part of the value is cut off.

● the sign issue doesn't exist when storing, cause we're going from a largernumber of bits to a smaller numbero therefore, there are no sbu/shu instructions

31

31 010100010 00001110 11111111 00000100

11111111 00000100

sh