Arrays CS031 – TA Lecture October 17, 2011 Motivation We’d like to see how to represent various high-level language constructs in MIPS. One of the simpler constructs is an array. We’d like to program in MIPS with arrays and understand how our high level code maps to assembly language. Arrays Elements of an array will be stored in contiguous blocks of memory of an appropriate size. How do we create arrays in memory, and how do we access their elements? What do we do about 2D arrays? In what ways can we use arrays as arguments for our procedures? Declaring Arrays in .data – 1D Examples charArr: .byte 0:8 intArr: .word 1:10 What is the offset of charArr[5] from the label charArr? Ans: 5 * sizeof(byte) = 5 What is the offset of intArr[3] from the label intArr? Ans: 3 * sizeof(word) = 12 Accessing Arrays in .data – 1D How do we access charArr[5]? li $t0, 5 #offset of charArr[5] lb $t1, charArr($t0) #load this byte What about intArr[3]? li $t0, 12 lw $t1, intArr($t0) #offset of intArr[3] #load this word What happens if we try to access intArr[-2]? What about intArr[11]? Declaring Arrays in .data – 2D Suppose we want to create a 3 row by 5 column array of int’s. How many words do we want? Ans: 3x5 = 15 We can use this declaration: s: .word 0:15 By convention, we’ll use row major order: The offset of s[row][col] is 4 * (5 * row + col) 5 is the number of columns in s, and 4 is the size of each element. Declaring Arrays in .data – 2D Here are two different ways to view row major order: The offset of s[row][0] is always divisible by the number of columns! More Arrays in .data Accessing 2D arrays is the same as accessing 1D arrays once you have the offset. How do we use these arrays in procedures? Do we need to pass them as arguments? Arrays declared in .data are global variables. We don’t need to pass them…but global variables should be avoided (Read the Conventions Guide!) Arrays on the Stack Instead, we can use the stack for our arrays. Decide how much space the array needs. space = rows * cols * sizeOfElement Allocate space on the stack. Here, rows * cols is in $t0 and each element’s size is in $t1. The array address will go in $s0. mult $t2, $t0, $t1 sub $sp, $sp, $t2 add $s0, $sp, 4 Be sure to set each element to zero! Arrays on the Stack We can calculate offsets like before. When our arrays were declared in the .data section, we used relative indexing like intArr($t0). In this approach, our array’s address is in a register, so we’ll have to add the offset. If $s0 is the array’s address and $t0 is the offset, we access the element like this: add $t1, $s0, $t0 lw $s1, ($t1) #or lb Arrays on the Stack How do we pass these arrays as arguments to procedures? We can pass by reference. Pass the address of the array to the procedure. We can pass by value. Pass the size of the array and the contents of the array on the stack. In your assignments, you’ll be passing by reference. An Example Given a 2D array of integers, the number of rows, and number of columns, fill all elements where row = col with 1. An Example
© Copyright 2026 Paperzz