Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.

Similar presentations


Presentation on theme: "Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6."— Presentation transcript:

1 Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

2 Motivation u Real programs use abstractions like lists, trees, stacks, and queues. u The data associated with these abstractions must be ordered within memory. u Assembly language does not provide convenient ways to access data in memory like high-level languages do. u It therefore necessary to calculate explicitly the locations of data within the structure.

3 Memory u A memory cell is a unit of memory with a unique address. u The entire memory is a collection of memory cells. u Each memory cell holds eight bits, or 1 byte. u A word designates the amount of memory used to store an integer, e.g., 1 word = 4 bytes

4 Storing an integer u Assume a 32-bit word. The number of bits used to store an integer is 32 or 4 bytes. It can also be thought of as an array of 4 bytes. u By convention, the smallest of the four byte addresses is used to indicate the address of the word. u The smallest of the byte addresses in a word must be a multiple of four  words are aligned

5 RISC: load/store architecture u Most RISC designs don't allow arithmetic and logic instructions to access memory  Source, target of operation must be registers u A few special load/store instructions are used to move data from memory to registers  This means RISC architecture require more registers than CISC architectures

6 Specifying an address u In a load/store architecture, only two types of instructions specify addresses:  store and load instructions  control instructions u Control instructions specify the target address of the next instruction if the control of the program execution is to be transferred there  branch  jump

7 PC-relative addressing u A machine language instruction can specify the target of a branch as an offset to the address of the instruction. u The offset is added to the value of the PC to find the effective address of the next instruction. An addressing mode that implies the use of a PC is PC-relative. offset beqvar1var2 program counter (PC) address + effective address

8 Load and store instructions lw R, address u R is the target register. In its most general form the address can include both a constant (which can be a label) and a base register. For example, lw $22, 12($25)  The effective address is computed by adding 12 to the contents of the base register $25. Note that ($25) means the contents of register 25. The word at the effective address is loaded into register 22. Make sure that the effective address is a multiple of 4.

9 Store to memory  Data can be copied from the register R to the memory location specified by address sw R, address Similar to the load instruction the address can include both a constant and a base register specification. sw, $13, 4($9) The effective address is computed by adding 4 to the contents of register 9.

10  The sb is equivalent to the store word instruction, sw, only that it stores 1 byte instead of 4 bytes to the effective address. Only the least significant byte of the register is used.  The lb and lbu instructions load the byte into the least significant byte of the register.  lbu loads the byte as unsigned, i.e. the other three bytes in the register are set to 0  lb sign-extends the byte -- the other three bytes are set to whatever the sign of the byte is. Byte access

11 Valid memory reference formats u lw $s0, label u sw $s0, label($s1) u sb $s2, 16($t0) u lw $s0, ($s3)

12 Array u The array is the most important and most general data structure. u All other data structures can be implemented using an array. u The computer memory itself is organized as a large, one- dimensional array. u All uses of the memory are simply allocations of part of this gigantic array. 0x00000000 0xffffffff

13 Allocating Space for Arrays u To allocate space for an array of ASCII characters {label}:.ascii “abcdef” {label}:.asciiz“abcdef” #null terminated u An array of integers can be allocated space and initialized using the word directive {label}:.word2, 4, 8, 0x10, 32 u An array of un-initialized bytes can be allocated using the space directive {label}:.space 80 #space for 20 integers #or 80 characters

14 Accessing Array Elements  To access element i of the array, we must add an offset to the array’s starting address, also called the base address. This is the address associated with the label of the array.  We must make sure that the value of i is the correct displacement between the i th element and the base address. u For ease of calculation, we assume the array is indexed starting with 0, as in C or C++

15 Accessing Array Elements ar:.space 20 #20-element array of char... la $s0, ar #get the base address lb $s1,5($s0) #access the target element target element base address address of target element

16 Finding the Element Address The formula for finding the address of an element in an array can be generalized to b = base address s = size of each element i = number of positions between the target and the base address char = 1 byte integer = 4 bytes can be viewed as an offset from the base address a = b + s*i

17 Two-dimensional Arrays u The number of bytes needed for the array is found by multiplying the size of each element times the number of elements 2 x 7 array

18 Example 15.4 To declare a 2 x 7 array of integers, we can use the following: ar:.space 56 7 x 2 x (4 bytes)

19 Storage Order Row-major order: the array is stored as a sequence of arrays consisting of rows 0123 54 6 0 1 (0,0) (0,1) (0,2)

20 Storage Order Column-major order: The array is stored as a sequence of arrays consisting of columns instead of rows 0123 54 6 0 1 (0,0) (1,0) (0,1)

21 Accessing elements in 2D arrays We know that the formula for finding the address of an element (x, y) in an array of size L x W is a = b + s*I I = e*j + k e = range of the minor dimension (L or W) j = index of the major dimension (y or x) k = index of the minor dimension (x or y)

22 Example: 2-D array Row-major order: e = 4, j = 1, k = 2 a = b + s*6 Column-major order: e = 3, j = 2, k = 1 a = b + s*7 0123 0 1 2 x


Download ppt "Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6."

Similar presentations


Ads by Google