The point
Do the address arithmetic by hand before making the compiler do it. Off-by-four is very cheap to make and very expensive to find in generated assembly.
Do this
- On paper.
int a[5];sits at offset 12 in the frame. Write the byte address ofa[0],a[3]anda[i]. Then write the MIPS to loada[i]into$t4, withiin$t2. - Two dimensions, on paper only. For
int m[3][4]in row-major order, write the address ofm[i][j]. Which of the two bounds does the formula need? Now explain why C requiresint m[][4]and notint m[][]in a parameter list. - Why
sll $t, $t, 2and notmul $t, $t, 4? Both are correct. Name the optimization this is an instance of, and check whether SPIM's instruction count agrees with your reasoning. - Implement array declaration, indexing, and the load/store cases in the back end.
- Test a local array first, then a global one. They take different paths to the base address; read the generated
.sand confirm you can see which path each took. - Break it deliberately. Read
a[99]from a five-element array. Nothing stops you — there is no bounds check. What memory did you just read? Would adding a bounds check be a phase-3 or a phase-6 change, and what would it cost?
.asciiz leaves the assembler's location counter on an odd byte. Without .align 2 before the next .space, every lw on that array raises an address-error exception in SPIM — and the message does not point at the cause. This is the alignment issue from §1 of the notes, made concrete.
Deliverable
Your hand-written address arithmetic and MIPS; the two-dimensional formula with the C parameter explanation; your answer on strength reduction; a working array test; and a paragraph on step 6. Feeds Lab Question 14.