









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










After completing this lab you will:
As long as the compiler generates code, the programmer may be completely unaware of the layout of data and instructions in memory. The two questions we address in this lab are:
An object (data or instruction) with the size 2 n^ bytes is said to be aligned in memory if it is stored in at an address which is a multiple of 2 n. Since a multiple of 2 n^ number has the least significant n bits zero, we can also say that an object of size 2 n^ is memory aligned if it is stored at an address whose least significant n bits are zero.
The alignment requirement for data and instructions in MIPS (as well as in other RISC architectures) is directly related to performance. An unaligned object in memory may require multiple memory accesses and/ or special processing. As the next figure shows, an unaligned word in memory requires two memory accesses
Object Size (bytes) Store at address
byte 1 = 2^0 any address
half-word 2 = 2^1 multiple of 2
word 4 = 2^2 multiple of 4
double 8 = 2^3 multiple of 8
to be fetched and extra operations to be aligned in a register: the three bytes of the word read from address 0x400001 must be left shifted one byte, then the byte read from address 0x400004 merged on the least significant position. All this extra work would make the instruction execute slower.
When data larger than a byte is stored in memory there are two possible layouts for the individual bytes as shown in the following figure where an integer (size = word) with the value 0x56789abc is stored at address 0x400000.If the most significant byte of the data object is stored at a smaller address than the least
significant byte, then the layout is called Big Endian. If the most significant byte is stored at a higher address than the least significant byte, then the layout is called Little Endian.
MIPS CPUs can use either memory model. The specific model is selected at the reset time. The layout model can not change dynamically (while programs are running).
The SPIM simulator uses the memory model of the machine it is running on. If the simulator runs on a Little Endian machine then the memory model the user sees is Little Endian. If the simulator runs on a Big Endian machine then the memory model the user sees is Big Endian.
0x
0x
0x
Unaligned word stored at address 0x
0x
Aligned word stored at address 0x
0x
0x
0x56 0x78 0x9a 0xbc 0xbc 0x9a 0x78 0x 0x
Big Endian layout for the integer 0x56789abc
Little Endian layout for the integer 0x56789abc
Laboratory 5: Prelab Memory Issues
Load lab5.1.asm in the simulator and look in the data segment to see where data has been stored. Fill out the following table. In the ‘Multiple of’ column of the table indicate what power of two the address is a multiple
of. In the ‘Displacement’ column’ indicate what is the displacement of the data object from the beginning of the data segment.
Based on the way data has been stored in the data segment, do you think data is aligned or not in memory? Explain why.
How many bytes have been wasted due to alignment?
Modify lab5.1.asm as to minimize the number of bytes wasted due to alignment. Save your work as lab5.2.asm.
Hint : change the order of data declarations.
Name Data size (bytes) Address Multiple of Displacement (bytes)
char
double
char
half
char
word
char
wasted_bytes =
Laboratory 5: Prelab Memory Issues
How many bytes are wasted due to alignment in program lab5.2.asm?
Modify lab5.1.asm as follows:
Save this program as lab5.3.asm.
Load lab5.3.asm in the simulator and look in the data segment to see where data has been stored. Fill out the following table. In the ‘Multiple of’ column of the table indicate what power of two the address is a multiple
of. In the ‘Displacement’ column’ indicate what is the displacement of the data object from the beginning of the data segment.
Based on the way data has been stored in the data segment, do you think data is aligned or not in memory?
wasted_bytes =
Name Data size (bytes) Address Multiple of Displacement (bytes)
char
double
char
half
char
word
char
Laboratory 5: Prelab Memory Issues
Load lab5.4.asm. Write down the first error message you obtain
Why does the simulator report an error?
Laboratory 5: Inlab Memory Issues
Date Section
Name
Most of the time the programmer does not see the details of memory layout for data. Most of the time it is irrelevant whether the data (or instructions for that matter) is stored in Big or Little Endian format. But you do not need to do assembly programming to become aware of these details. Playing with pointers makes the issue visible very quickly.
int w=0x4255664c; // an integer is of size word char *p; // pointer to byte
p = (char*)&w; // p now points to where w starts in memory cout << *p << endl; // prints the byte pointed to by p }
The last statement in this piece of code will print out the byte stored in memory at the address where the stor- age for w begins. If the memory model is Big Endian, then this byte is the most significant byte of w (0x42), and the program prints the letter B (0x42 is the ASCII representation for B). Otherwise, it is the least sig- nificant byte of w (0x4c) and the program prints the letter L.
The MIPS architecture provides instructions that can be used to load and store data of other sizes than the native size (word). The next table presents the loads. Corresponding store instructions exist for the signed ver-
sions of these loads.
Remember that sign-extending a datum means replicating the most significant bit of the datum until it reaches the required size. Sign-extending a byte to fit a register (32 bits) means replicating 24 times the most signif- icant bit of the byte.
Instruction Effect Comment
lb Rdest, disp(Rbase) Rdest ← M[Rbase + disp] 8 load byte and sign extend it
lbu Rdest, disp(Rbase) Rdest ← M[Rbase + disp]8 load byte
lh Rdest, disp(Rbase) Rdest ← M[Rbase + disp]16 load half-word and sign extend it
lhu Rdest, disp(Rbase) Rdest ← M[Rbase + disp]16 load half-word
Laboratory 5: Inlab Memory Issues
Why are $t2 and $t6 different even if they have been loaded with data from the same address?
Based on the content of registers can you decide whether the SPIM simulator uses a Big Endian or Little Endian memory model? Which one?
Use lab4.2.asm as a template to create lab5.6.asm using the following description.
Run the program using the next test plan. Enter the last four digits of your SSN in the bolded row of the table.
Test plan for lab5.6.asm
Integer Output
0 0
-1 -
1 16777216
16777216
Laboratory 5: Postlab Memory Issues
Date Section
Name
The MIPS architecture provides instructions that can be used to handle unaligned data. Loading an unaligned word from memory will require at least two instructions, one to get the upper part of the data and another one for the lower part of the same data.
Using the special instructions in the instruction set will result in programs that work without generating align- ment related errors. These programs will be slower than their counterparts in which data is aligned in memory at the proper boundaries.
Instruction alignment is a must. The programmer may choose whether data is aligned or not but there is no choice for instructions.
Can you see any advantage in working with unaligned data?
Aside from the fact that these instructions must generate no error, they must also place the data of interest in the proper place in the destination register.
In the figure below the most significant three bytes of a word are stored beginning with address 0x10000001, while the least significant byte of the same word is stored at 0x10000004. When loaded in a register, the most significant three bytes of the word must end up on the most significant three bytes in the register, while the least significant byte of the word (now stored on the most significant position of the word beginning at 0x10000004) should be merged with the other bytes on the least significant byte of the register.
There are two instructions used to do the job, lwl and lwr. Similar instructions exist for dealing with half- words.
lwl will read from a memory address (possibly unaligned) and will place the bytes starting with the current address up to the next aligned word address in the upper bytes of the destination register. The next example
Laboratory 5: Postlab Memory Issues
Create the program lab5.8.asm which does
Run the program. Based on the values stored in registers fill the missing spaces in the following figure. The bold vertical arrow indicates the address lwr reads from. Use slanted lines to indicates which bytes are trans- ferred in the register and on what position(s) in the register.
When you run lab5.3.asm you get an error message, due to the fact that you attempt to load an unaligned word using the lw instruction. Modify that program and create a new one, named lab5.9.asm , which fixes the prob- lem. Use only native instructions.
Hint : use a sequence of lwl and lwr instructions with the appropriate displacements.
It is now time to use some of the store instructions for unaligned data.
Create the program lab5.10.asm with the following description.
0x
0x
0x
0x
$t
$t
$t
$t
Memory Register
Laboratory 5: Postlab Memory Issues
Start the SPIM simulator using the -bare command line option. Load the program and run it. Look into the memory and make sure the program has changed the value of word2 from 0 to 0x89abcdef.
You want to evaluate the impact of unaligned data on the overall performance of some application.
If the data were aligned in the memory, then each memory access would be just one instruction. If data is not aligned, then several instructions are needed for each load or store.
Let’s also assume that the overall CPI for the application does not change and that the clock cycle is the same in both cases. The next table gives the frequency of loads and stores for various data sizes in the case data is aligned. For the sake of this problem we ignore any data of size other than byte and word.
By how much faster is the application when data is aligned than the same application with unaligned data? Show your work.
Extra instructions needed to load an unaligned word =
Extra instructions needed to store an unaligned word =
Instruction Frequency
lw 9%
sw 4%
lb 2%
sb 0.9%