Arrays in MIPS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Array Declaration and Storage Allocation

MIPS Arrays 1

The first step is to reserve sufficient space for the array:


list:

.data
.space

1000

# reserves a block of 1000 bytes

The label is a symbolic name for the address of the beginning


of the array.
list == 1004000

Memory
1004000
1004001
1004002
1004003
1004004
...

allocation for list

This yields a contiguous block of bytes of the specified


size.

1004999

The size of the array is specified in bytes could be used as:


array of 1000 char values (ASCII codes)
array of 250 int values
array of 125 double values
There is no sense in which the size of the array is "known" by
the array itself.

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Array Declaration with Initialization

MIPS Arrays 2

An array can also be declared with a list of initializers:


.data
vowels: .byte
pow2:
.word

'a', 'e', 'i', 'o', 'u'


1, 2, 4, 8, 16, 32, 64, 128

Address of vowels[k] == vowels + k

1004000

97

1004001

101

1004002

105

1004003

111

1004004

117

alloc for vowels

vowels names a contiguous block of 5 bytes, set to store the


given values; each value is stored in a single byte.

Memory

1004005
1004006
1004007
1004008
1004009
1004010

1004011
1004012

alloc for pow2

pow2 names a contiguous block of 32 bytes, set to store the


given values; each value is stored in a word (4 bytes)
Address of pow2[k] == pow2 + 4 * k

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Another View

MIPS Arrays 3

69

10040001

10040002

6F

75

01

00

00

00

02

00

00

00

10040012

65

10040008

61
10040000

Viewed as hex nybbles, the contents of memory would look like (in little-endian):

0110 0001

Note that endian-ness affects the ordering of bytes, not the ordering of the nybbles within
a byte.

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Array Traversal and Initialization

MIPS Arrays 4

Here's an array traversal to initialize a list of integer values:


.data
list:
.space
listsz: .word

main:

1000
250

.text
lw
$s0, listsz
la
$s1, list
li
$t0, 0

# using as array of integers

# $s0 = array dimension


# $s1 = array address
# $t0 = # elems init'd

initlp: beq
$t0, $s0, initdn
sw
$s1, ($s1)
# list[i] = addr of list[i]
addi $s1, $s1, 4
# step to next array cell
addi $t0, $t0, 1
# count elem just init'd
b
initlp
initdn:
li
$v0, 10
syscall
QTP: why 4?

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Array Traversal Details

MIPS Arrays 5
1004008
1004008

. . .
initlp: beq
$t0, $s0, initdn
sw

$s1, ($s1)

addi

$s1, $s1, 4

addi

$t0, $t0, 1

initlp

1004012
1004012

1004016
1004016

initdn:
1004020

A variable that stores an address is called a pointer.


Here, $s1 is a pointer to a cell of the array list.
We can re-target $s1 to a different cell by adding an appropriate
value to it.

CS@VT September 2010

Computer Organization I

1004020

1004024
1004024

2006-10 McQuain,

Alternate Traversal Logic

MIPS Arrays 6

This traversal uses pointer logic to terminate the loop:


.data
list:
.space
listsz: .word

1004008
1004008

1000
250
1004012

main:

.text
la
lw
addi
sll
add

$s1,
$s0,
$s0,
$s0,
$s0,

list
listsz
$s0, -1
$s0, 2
$s0, $s1

1004012

# index of last cell


# offset of last cell
# ptr to last cell

initlp: bgt
$s1, $s0, initdn
sw
$s1, ($s1)
addi $s1, $s1, 4
b
initlp
initdn:
li
$v0, 10
syscall

1004016
1004016

...
...

1004996
1004024

QTP: rewrite this using the do-while pattern shown in the previous lecture
CS@VT September 2010

Computer Organization I

1005000
2006-10 McQuain,

Array Bounds Issues

MIPS Arrays 7

An array can also be declared with a list of initializers:

Memory

'a', 'e', 'i', 'o', 'u'


1, 2, 4, 8, 16, 32, 64, 128

117

1004005
1004006
1004007
1004008

What happens if you access an array with a logically-invalid


array index?
??

contents of address 1004005

1004012
2

While vowels[5] does not exist logically as part of the array,


it does specify a physical location in memory.
...

What is actually stored there is, in general, unpredictable.


In any case, the value is not one that we want

CS@VT September 2010

Computer Organization I

alloc for pow2

vowels[5]

alloc for vowels

1004004

.data
vowels: .byte
pow2:
.word

1004036

2006-10 McQuain,

Special Case: Array of Characters

MIPS Arrays 8

As we've seen, the declaration:


.data
vowels: .byte

'a', 'e', 'i', 'o', 'u'

Leads to the allocation:

61

65

69

6F

75

65

69

6F

75

However, the declaration:


.data
vowels: .asciiz

Leads to the allocation:

"aeiou"
61

00

An extra byte is allocated and initialized to store 0x00, which acts as a marker for the end
of the character sequence (i.e., string).
This allows us to write loops to process character strings without knowing the length of
the string in advance.

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Searching a Character String


char:
vowels:

.data
.byte
.asciiz

MIPS Arrays 9

'u'
"aeiou"

.text
main:

srchlp:

lb
li
la
lb

$t0,
$t1,
$s0,
$s1,

char
0
vowels
($s0)

#
#
#
#

load character to look for


it's not found yet
set pointer to vowels[0]
get vowels[0]

beq
seq
bgt
addi
lb
b

$s1, $zero, srchdn


$t1, $s1, $t0
$t1, $zero, srchdn
$s0, $s0, 1
$s1, ($s0)
srchlp

#
#
#
#
#

check for terminator


compare characters
check if found
no, step to next vowel
load next vowel

srchdn:
li
$v0, 10
syscall

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Setup Details


. . .
lb
li
la
lb
. . .

$t0,
$t1,
$s0,
$s1,

char
0
vowels
($s0)

#
#
#
#

MIPS Arrays 10

load character to look for


it's not found yet
set pointer to vowels[0]
get vowels[0]

char vowels
$t0

00

00

00

75

$t1

00

00

00

00

00

00

00

61

75

61

65

69

6F

75

00

$s0

$s1

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Loop Details


srchlp:

MIPS Arrays 11

. . .
beq

$s1, $zero, srchdn

# string terminator is 0x00

seq

$t1, $s1, $t0

# $t1 = 1 iff $s1 == $t0

bgt

$t1, $zero, srchdn

# if match found, exit loop

addi

$s0, $s0, 1

# step to next elem of vowels

lb

$s1, ($s0)

# load next elem of vowels

srchlp

srchdn:
. . .

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Print Array Contents


list:
size:

MIPS Arrays 12

.data
.word
.word
. . .
lw
la
li

$t3, size
$t1, list
$t2, 0

# get array address


# set loop counter

beq

$t2, $t3, prndn

# check for array end

2, 3, 5, 7, 11, 13, 17, 19, 23, 29


10

prnlp:

lw
$a0, ($t1)
li
$v0, 1
syscall

# print list element

la
$a0, NL
li
$v0, 4
syscall

# print a newline

addi
addi
b

# advance loop counter


# advance array pointer
# repeat the loop

$t2, $t2, 1
$t1, $t1, 4
prnlp

prndn:

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: syscall Details


. . .
lw
$a0, ($t1)
li
$v0, 1
syscall
. . .

. . .
la
$a0, NL
li
$v0, 4
syscall
. . .

CS@VT September 2010

MIPS Arrays 13

# syscall #1 prints and integer to stdout


# takes value via register $a0
# takes syscall # via register $v0

# syscall #4 prints asciiz to stdout


# takes address of string via $a0
# takes syscall # via register $v0

Computer Organization I

2006-10 McQuain,

Example: Palindromes

MIPS Arrays 14

A palindrome is a sequence of characters that reads the same from left to right as from
right to left:
able was i ere i saw elba
anna
madam
It is generally permitted to adjust capitalization, spaces and punctuation:
A man, a plan, a canal, Panama!
Madam, I'm Adam.
For the purpose of an example, we will not allow such manipulations.

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Reading a String

MIPS Arrays 15

We must reserve space to store the characters:


buffer:

.space

1025

# 1024 maximum, plus a terminator

We'll want to issue a prompt to the user to enter the string to be tested:
user_prompt:
.asciiz

"Enter ... of no more than 1024 characters.\n"

We can use a couple of system calls to get the input:


main:
## Prompt the user to enter a string:
la
$a0, user_prompt
li
$v0, 4
syscall
## Read the string, plus a terminator, into the buffer
la
$a0, buffer
li
$a1, 1024
li
$v0, 8
syscall

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Finding the End of the String

MIPS Arrays 16

We must locate the end of the string that the user entered:
la
la
LengthLp:
lb
beqz
addi
b
LengthDn:
addi

$t1, buffer
$t2, buffer

# lower array pointer = array base


# start upper pointer at beginning

$t3, ($t2)
$t3, LengthDn
$t2, $t2, 1
LengthLp

#
#
#
#

$t2, $t2, -2

# move upper pointer back to last char

grab the character at upper ptr


if $t3 == 0, we're at the terminator
count the character
repeat the loop

QTP: why -2?

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Testing the String

MIPS Arrays 17

Now we'll walk the pointers toward the middle of the string, comparing characters as we
go:
TestLp:
bge

$t1, $t2, Yes

# if lower pointer >= upper pointer, yes

lb
lb

$t3, ($t1)
$t4, ($t2)

# grab the character at lower ptr


# grab the character at upper pointer

bne

$t3, $t4, No

# if different, it's not a palindrome

addi
subi

$t1, $t1, 1
$t2, $t2, 1

# increment lower ptr


# decrement upper ptr

TestLp

# restart the loop

CS@VT September 2010

Computer Organization I

2006-10 McQuain,

Example: Reporting Results


Yes:
la
$a0, is_palindrome_msg
li
$v0, 4
syscall
b
exit

MIPS Arrays 18

# print confirmation

No:
la
$a0, is_not_palindrome_msg
li
$v0, 4
syscall

CS@VT September 2010

Computer Organization I

# print denial

2006-10 McQuain,

You might also like