Download presentation
Presentation is loading. Please wait.
1
CS/COE 0447 (term 2181) Jarrett Billingsley
Memory and Addresses CS/COE 0447 (term 2181) Jarrett Billingsley
2
Class announcements Add/drop ends tomorrow!
Recitations begin tomorrow! You can use your own computers, or the lab computers. If you use your own computers, download MARS and make sure it works before going to recitation. I've provided a modified version of MARS so be sure to use that one, and not the one on the Missouri State page. The lab will help you get started with the most basic parts of the project. 9/7/2017 CS/COE term 2181
3
Memory 9/7/2017 CS/COE term 2181
4
What is the memory? The system memory is a piece of temporary storage hardware that is smaller and faster than the persistent storage. Maybe in the future it won't be temporary, or the line between system memory and persistent storage will go away… It's where the programs and data that the computer is currently executing reside. All the open files, all the variables, all the functions etc. The CPU can only run programs from system memory! 9/7/2017 CS/COE term 2181
5
Bytes, bytes, bytes The memory is a big one-dimensional array of bytes. What do these bytes mean? ¯\_(ツ)_/¯ Every byte value has an address. This is its "array index." Addresses start at 0, like arrays in C/Java. When each byte has its own address, we call it a byte-addressable machine. Not many non-byte-addressable machines these days. Addr Val 00 1 30 2 04 3 4 DE 5 C0 6 EF 7 BE 8 6C 9 34 A B 01 C 02 D 03 9/7/2017 CS/COE term 2181
6
Words, words, words Well, bytes aren't very big. (What's their range?)
For most things, we want to use words, the "comfortable" integer size for the CPU. On this version of MIPS, it's 32 bits, or 4 bytes. But our memory only holds bytes. So what do we do? We combine multiple bytes into larger values. Addr Val 00 1 30 2 04 3 4 DE 5 C0 6 EF 7 BE 8 6C 9 34 A B 01 C 02 D 03 9/7/2017 CS/COE term 2181
7
A matter of perspective
Let's say the bytes with addresses 4 through 7 represent a word. What word do they represent? If we think of addresses increasing downward… Addr Val ... 4 DE 5 C0 6 EF 7 BE Addr Val ... 7 BE 6 EF 5 C0 4 DE If we think of addresses increasing upward… …is it 0xDEC0EFBE? …is it 0xBEEFC0DE? 9/7/2017 CS/COE term 2181
8
0xDEC0EFBE 0xBEEFC0DE 0 1 2 3 DE C0 EF BE Endianness
When interpreting a sequence of bytes as larger values, endianness is the rule used to decide what order to put the bytes in. Big-endian means the first byte is the "big end:" the most significant byte. Little-endian means the first byte is the "little end:" the least significant byte. DE C0 EF BE 0xDEC0EFBE 0xBEEFC0DE 9/7/2017 CS/COE term 2181
9
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: Like in files Or networks Or hardware buses Or… memory! Which one is MIPS? It's bi-endian, meaning it can be configured to work either way. But the MARS simulator uses the endianness of the computer it's running on, so little-endian for virtually everyone. 9/7/2017 CS/COE term 2181
10
What DOESN'T endianness affect?
It doesn't affect the arrangement of the bits within a byte. It just changes the meaning of the order of the bytes. Notice the bytes are still DE, C0 etc. regardless of endianness. It doesn't affect the ordering of bytes inside the CPU. There's no need for e.g. "big-endian" arithmetic. The CPU works with whole words. It's only when data is moved between the CPU and memory that this issue has to be resolved. 9/7/2017 CS/COE term 2181
11
Accessing memory 9/7/2017 CS/COE term 2181
12
Load-store architectures
In some architectures, many instructions can access memory. x86-64: add [rsp-8], rcx MIPS is a load-store architecture. All memory accesses are done with two kinds of instructions: loads and stores. lw Loads move data from memory into CPU registers. Registers Memory sw Stores move data from CPU registers into memory. 9/7/2017 CS/COE term 2181
13
Operating on variables in memory (animated)
Let's say we want to increment a 32-bit variable that is in memory. Where do values have to be for the CPU to operate on them? So, what three steps are needed to increment that variable? Load the value from memory into a register. Add 1 to the value in the register. Store the value from the register back into memory. Every variable access works like this! HLLs just hide this tedium from you. 5 4 5 4 x 9/7/2017 CS/COE term 2181
14
Every variable really has two parts: an address and a value.
Memory addresses Everything in memory has an address: the position in memory where it begins (i.e. where its first byte is). 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, you first need to figure out what address to give it. Thankfully, this extremely tedious task is handled by assemblers. 9/7/2017 CS/COE term 2181
15
Putting a variable in memory
We can declare a global variable like this: .data x: .word 4 The Java equivalent would be static int x = 4; The .data says "I'm gonna declare variables." You can declare as many as you want! 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? 9/7/2017 CS/COE term 2181
16
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 (register names not important): lw t1, (t0) # loads a word sw t1, (t0) # stores a word The (parentheses) mean "this register holds the address of the variable we want to access." So t0 above holds an address. In MIPS, stores are written with the destination on the right. !? Well, you can remember it with this diagram… The memory is "on the right" for both loads and stores. Registers Memory lw sw 9/7/2017 CS/COE term 2181
17
MIPS ISA: putting a variable's address in a register
That x variable is given an address automatically. So how do we put the address into a register to use with lw/sw? la t0, x la means load address. It doesn't load anything from memory. t0 now contains x's address. But x's value is still in memory. Remember the sequence of steps we need to do? 9/7/2017 CS/COE term 2181
18
la t0, x lw t1, (t0) addi t1, t1, 1 sw t1, (t0) Read, modify, write
You now know (almost) enough to increment x! First we put the address of x into a register. Then… Let's see what values are in t0, t1, and memory after this program runs. la t0, x lw t1, (t0) addi is like add, but adds an immediate number – it's there in the instruction. addi t1, t1, 1 sw t1, (t0) 9/7/2017 CS/COE term 2181
19
How does the CPU know that t0 holds an address?
IT DOESN'T Addresses are just numbers too! Which means we can do math on addresses. Which sounds silly, but it's the basis of how we implement arrays, objects, local variables in functions etc. More on that next time. You can also have a variable whose value is an address. You'll learn about these in C. They're called pointers. 9/7/2017 CS/COE term 2181
20
Smaller values 9/7/2017 CS/COE term 2181
21
MIPS ISA: loading and storing 8/16-bit values
Some values are tiny. 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 t1, (t0) # loads a byte sb t1, (t0) # stores a byte …or DO THEY?!?!?!? 9/7/2017 CS/COE term 2181
22
lb (lh) lbu (lhu) 10010000 Remember extension? 31 00000000 31 11111111
MIPS registers are 32 bits. So what happens when you load an 8-bit value into a 32-bit register? 31 If the byte is signed… what should it become? 31 lb (lh) If the byte is unsigned… what should it become? 31 lbu (lhu) 9/7/2017 CS/COE term 2181
23
Truncation If we go the other way, the upper part of the register is truncated (cut off). The sign issue doesn't exist when storing, since we're going from a larger number of bits to a smaller number. Therefore, there are no sbu/shu instructions. sb 31 31 9/7/2017 CS/COE term 2181
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.