Arrays 1 - GitHub Pages

Arrays

MIPS Arrays 1

First step is to reserve sufficient space for the array.

Array elements are accessed via their addresses in memory, which is convenient if you¡¯ve

given the .space directive a suitable label.

list:

size:

. . .

.data

.word

.word

lw

la

li

print_loop:

beq

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

10

$t3, size

$t1, list

$t2, 0

$t2, $t3, print_loop_end

lw

$a0, ($t1)

li

$v0, 1

syscall

addi

$t2, $t2, 1

addi

$t1, $t1, 4

j

print_loop

print_loop_end:

CS@VT October 2009

# get array address

# set loop counter

# check for array end

# print value at the array pointer

# advance loop counter

# advance array pointer

# repeat the loop

Computer Organization I

?2006-09 McQuain, Feng & Ribbens

Array Example

MIPS Arrays 2

This is part of the palindrome example from the course website:

.data

string_space: .space 1024

...

# prior to the loop, $t1 is set to the address of the first

# char in string_space, and $t2 is set to the last one

test_loop:

bge

$t1, $t2, is_palin

# if lower pointer >= upper

# pointer, yes

lb

lb

bne

$t3, ($t1)

$t4, ($t2)

$t3, $t4, not_palin

# grab the char at lower ptr

# grab the char at upper ptr

# if different, it's not

addi

addi

j

$t1, $t1, 1

$t2, $t2, -1

test_loop

# advance lower ptr

# advance upper ptr

# repeat the loop

...

CS@VT October 2009

Computer Organization I

?2006-09 McQuain, Feng & Ribbens

Example 1: Array Traversal in C

MIPS Arrays 3

// PrintList.c

#include

int main() {

int Sz = 10;

int Array[10] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};

int Pos = 0;

while ( Pos < Sz ) {

printf("%3d:

++Pos;

%d\n", Pos, Array[Pos]);

}

}

CS@VT October 2009

Computer Organization I

?2006-09 McQuain, Feng & Ribbens

Example 1: Array Traversal in MIPS

MIPS Arrays 4

# PrintList.asm

.data

Sz:

.word

10

Array: .word

1, 1, 2, 3, 5, 8, 13, 21, 34, 55

NL:

.asciiz "\n"

.text

main:

lw

move

move

$s7, Sz

$s1, $zero

$s2, $zero

# get size of list

# set counter for # of elems printed

# set offset from Array

print_loop:

bge

$s1, $s7, print_loop_end

lw

li

syscall

la

li

syscall

$a0, Array($s2)

$v0, 1

# print next value from the list

$a0, NL

$v0, 4

# print a newline

addi

$s1, $s1, 1

addi

$s2, $s2, 4

j

print_loop

print_loop_end:

CS@VT October 2009

# stop after last elem is printed

# increment the loop counter

# step to the next array elem

# repeat the loop

Computer Organization I

?2006-09 McQuain, Feng & Ribbens

Example 2: C Bubblesort

MIPS Arrays 5

int main() {

int Sz = 10;

int List[10] = {17, 5, 92, 87, 41, 10, 23, 55, 72, 36};

int Stop,

Curr,

Next,

Temp;

//

//

//

//

$s3:

$s0:

$s1:

$s2:

upper limit for pass

index of current value in comparison

index of successor to current value

temp storage for swap

for (Stop = Sz - 1; Stop > 0; Stop--) {

for (Curr = 0; Curr < Stop; Curr++) {

Next = Curr + 1;

if ( List[Curr] > List[Next] ) {

Temp

= List[Curr];

List[Curr] = List[Next];

List[Next] = Temp;

}

}

}

}

CS@VT October 2009

Computer Organization I

?2006-09 McQuain, Feng & Ribbens

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download