arrays in mips assembly computer organization and assembly language: module 6

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

Upload: adele-simmons

Post on 18-Jan-2018

233 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

Arrays in MIPS Assembly

Computer Organization and Assembly Language: Module 6

Page 2: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Motivation

Real programs use abstractions like lists, trees, stacks, and queues.

The data associated with these abstractions must be ordered within memory.

Assembly language does not provide convenient ways to access data in memory like high-level languages do.

It therefore necessary to calculate explicitly the locations of data within the structure.

Page 3: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Memory

A memory cell is a unit of memory with a unique address.

The entire memory is a collection of memory cells. Each memory cell holds eight bits, or 1 byte. A word designates the amount of memory used to

store an integer, e.g., 1 word = 4 bytes

Page 4: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Storing an integer

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.

By convention, the smallest of the four byte addresses is used to indicate the address of the word.

The smallest of the byte addresses in a word must be a multiple of four words are aligned

Page 5: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

RISC: load/store architecture Most RISC designs don't allow arithmetic and logic

instructions to access memory Source, target of operation must be registers

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

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

Specifying an address In a load/store architecture, only two types of

instructions specify addresses: store and load instructions control instructions

Control instructions specify the target address of the next instruction if the control of the program execution is to be transferred there branch jump

Page 7: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

PC-relative addressing A machine language instruction can specify the target of a

branch as an offset to the address of the instruction. 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.

offsetbeq var1 var2

programcounter (PC)

address +

effective address

Page 8: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Load and store instructionslw R, address

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.

Page 9: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Store to memory Data can be copied from the register R to the memory

location specified by addresssw R, addressSimilar 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.

Page 10: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

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

Page 11: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

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

Page 12: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Array The array is the most important

and most general data structure. All other data structures can be

implemented using an array. The computer memory itself is

organized as a large, one-dimensional array.

All uses of the memory are simply allocations of part of this gigantic array.

0x00000000

0xffffffff

Page 13: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Allocating Space for Arrays To allocate space for an array of ASCII characters{label}: .ascii “abcdef”{label}: .asciiz “abcdef” #null terminated

An array of integers can be allocated space and initialized using the word directive{label}: .word 2, 4, 8, 0x10, 32

An array of un-initialized bytes can be allocated using the space directive{label}: .space 80 #space for 20 integers #or 80 characters

Page 14: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

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 ith element and the base address.

For ease of calculation, we assume the array is indexed starting with 0, as in C or C++

Page 15: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Accessing Array Elementsar: .space 20 #20-element array of char

. . .la $s0, ar #get the base addresslb $s1,5($s0) #access the target element

target elementbase address address of target element

Page 16: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Finding the Element Address

The formula for finding the address of an element in an array can be generalized to

b = base addresss = size of each elementi = number of positions between the target and the base address

char = 1 byteinteger = 4 bytes

can be viewed as an offset from the base address

a = b + s*i

Page 17: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Two-dimensional Arrays 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

Page 18: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

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)

Page 19: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Storage Order

Row-major order: the array is stored as a sequence of arrays consisting of rows

0 1 2 3 54 60

1

(0,0)(0,1)(0,2)

Page 20: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Storage Order

Column-major order: The array is stored as a sequence of arrays consisting of columns instead of rows

0 1 2 3 54 60

1

(0,0)(1,0)(0,1)

Page 21: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

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)

Page 22: Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6

Example: 2-D array

Row-major order: e = 4, j = 1, k = 2a = b + s*6

Column-major order: e = 3, j = 2, k = 1a = b + s*7

0 1 2 30

1

2

x