Arrays in MIPS
Arrays in MIPS
Arrays in MIPS
MIPS Arrays 1
.data
.space
1000
Memory
1004000
1004001
1004002
1004003
1004004
...
1004999
Computer Organization I
2006-10 McQuain,
MIPS Arrays 2
1004000
97
1004001
101
1004002
105
1004003
111
1004004
117
Memory
1004005
1004006
1004007
1004008
1004009
1004010
1004011
1004012
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.
Computer Organization I
2006-10 McQuain,
MIPS Arrays 4
main:
1000
250
.text
lw
$s0, listsz
la
$s1, list
li
$t0, 0
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?
Computer Organization I
2006-10 McQuain,
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
Computer Organization I
1004020
1004024
1004024
2006-10 McQuain,
MIPS Arrays 6
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
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,
MIPS Arrays 7
Memory
117
1004005
1004006
1004007
1004008
1004012
2
Computer Organization I
vowels[5]
1004004
.data
vowels: .byte
pow2:
.word
1004036
2006-10 McQuain,
MIPS Arrays 8
61
65
69
6F
75
65
69
6F
75
"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.
Computer Organization I
2006-10 McQuain,
.data
.byte
.asciiz
MIPS Arrays 9
'u'
"aeiou"
.text
main:
srchlp:
lb
li
la
lb
$t0,
$t1,
$s0,
$s1,
char
0
vowels
($s0)
#
#
#
#
beq
seq
bgt
addi
lb
b
#
#
#
#
#
srchdn:
li
$v0, 10
syscall
Computer Organization I
2006-10 McQuain,
$t0,
$t1,
$s0,
$s1,
char
0
vowels
($s0)
#
#
#
#
MIPS Arrays 10
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
Computer Organization I
2006-10 McQuain,
MIPS Arrays 11
. . .
beq
seq
bgt
addi
$s0, $s0, 1
lb
$s1, ($s0)
srchlp
srchdn:
. . .
Computer Organization I
2006-10 McQuain,
MIPS Arrays 12
.data
.word
.word
. . .
lw
la
li
$t3, size
$t1, list
$t2, 0
beq
prnlp:
lw
$a0, ($t1)
li
$v0, 1
syscall
la
$a0, NL
li
$v0, 4
syscall
# print a newline
addi
addi
b
$t2, $t2, 1
$t1, $t1, 4
prnlp
prndn:
Computer Organization I
2006-10 McQuain,
. . .
la
$a0, NL
li
$v0, 4
syscall
. . .
MIPS Arrays 13
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.
Computer Organization I
2006-10 McQuain,
MIPS Arrays 15
.space
1025
We'll want to issue a prompt to the user to enter the string to be tested:
user_prompt:
.asciiz
Computer Organization I
2006-10 McQuain,
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
$t3, ($t2)
$t3, LengthDn
$t2, $t2, 1
LengthLp
#
#
#
#
$t2, $t2, -2
Computer Organization I
2006-10 McQuain,
MIPS Arrays 17
Now we'll walk the pointers toward the middle of the string, comparing characters as we
go:
TestLp:
bge
lb
lb
$t3, ($t1)
$t4, ($t2)
bne
$t3, $t4, No
addi
subi
$t1, $t1, 1
$t2, $t2, 1
TestLp
Computer Organization I
2006-10 McQuain,
MIPS Arrays 18
# print confirmation
No:
la
$a0, is_not_palindrome_msg
li
$v0, 4
syscall
Computer Organization I
# print denial
2006-10 McQuain,