0% found this document useful (0 votes)
15 views5 pages

Lab 7 - ARRAY

The document discusses arrays in MIPS assembly language. It begins by explaining how to declare an array and reserve space in memory for it. It then demonstrates how to initialize an array with values and traverse through an array. Examples are given to access array elements to print or input values. Practice problems are provided to input arrays of integers from keyboard and perform operations like calculating sum, average, finding min/max, sorting, etc. The key points are declaring and accessing arrays in MIPS assembly along with examples and exercises to work with integer arrays.

Uploaded by

Kappa Boosted
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Lab 7 - ARRAY

The document discusses arrays in MIPS assembly language. It begins by explaining how to declare an array and reserve space in memory for it. It then demonstrates how to initialize an array with values and traverse through an array. Examples are given to access array elements to print or input values. Practice problems are provided to input arrays of integers from keyboard and perform operations like calculating sum, average, finding min/max, sorting, etc. The key points are declaring and accessing arrays in MIPS assembly along with examples and exercises to work with integer arrays.

Uploaded by

Kappa Boosted
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FACULTY OF INFORMATION TECHNOLOGY

DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION


Subject: Computer organization

LAB 7
ARRAY
1. Array Declaration
The first step is to reserve sufficient space for the array:
.data
list: .space 1000 # reserves a block of 1000 bytes

This yields a contiguous block of bytes of the specificed byte. The label is a symbolic name for the
address of the beginning of the array.

list == 1004000

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

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


.data
vowels: .byte 'a', 'e', 'i', 'o', 'u'
pow2: .word 1, 2, 4, 8, 16, 32, 64, 128

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


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

Address of vowels[k] == vowels + k

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


the given values; each value is stored in a word (4 bytes)

Truong Dinh Tu 1
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

2. Array Traversal and Initialization


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

Array Traversal Details:

Truong Dinh Tu 2
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

3. Array Access

Example 1: Get the elements from the list


.data
list: .word 11, 13, 15, 17, 19
length: .word 5
.text
.globl main
main:
la $s0, list # s0= array address
li $s1, 0 # i=0
lw $s2, length # array dimension

printLoop: # Loop to print elements


li $v0, 1 # code=1: print integer
lw $a0, ($s0) # Get list[i]
syscall

addi $s1, $s1, 1 # i=i+1


addi $s0, $s0, 4 # step to next array cell
blt $s1, $s2, printLoop # if i<N printLoop

# if i=N then Exit


li $v0, 10
syscall

Example 2: Input integers into the list


.data
list: .word 0
length: .word 5
.text
.globl main
main:
la $s0, list # s0= array address
li $s1, 0 # i=0
lw $s2, length # array dimension
readLoop:
li $v0, 5 # read integer, store to $v0
syscall
sw $v0,($s0) # list[i] = entered number
addi $s1,$s1,1 # i=i+1
addi $s0,$s0, 4 # step to next array cell
blt $s1, $s2, readLoop # if i<N readLoop

Truong Dinh Tu 3
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

Example 3: Write a program to input an array of 4 integers, output the array just
entered to the screen.
.data
list1: .word 0
N: .word 4
msg_in: .asciiz "input: "
msg_out: .asciiz "output: "
.text
.globl main
main:
# print a string input
li $v0, 4
la $a0, msg_in
syscall

# Array
la $s0, list1 #list Address
li $s1, 0 #i=0
lw $s2, N #N: array dimension

# Read an integer
readLoop:
li $v0, 5 # read integer, store to $v0
syscall
sw $v0, ($s0) # list[i]= entered number
addi $s1,$s1,1 # i=i+1
addi $s0,$s0, 4 # step to next array cell
bne $s1, $s2, readLoop

# Print a string output


li $v0, 4
la $a0, msg_out
syscall

# print elements from list


la $s0, list1 #list Address
li $s1, 0 #i=0
printLoop:
li $v0, 1
lw $a0, ($s0) # $a0 = Get list[i]
syscall
addi $s1,$s1,1 # i=i+1
addi $s0,$s0, 4 # step to next array cell
bne $s1, $s2, printLoop

#exit:
li $v0, 10
syscall

Truong Dinh Tu 4
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

Practice exercises:
1. Write a program to input an array of N integers (N input from the keyboard), output that
array of N integers to the screen.
2. Write a program to input an array of N integers (N input from the keyboard), calculate
the sum and average of the numbers in the array.
3. Write a program to input an array of N integers (N input from the keyboard), find the
number of min, max of the elements in the array.
4. Write a program to input an array of N integers (N input from the keyboard), calculate
the sum of even/odd numbers in the array.
5. Write a program to input an array of N integers (N input from the keyboard), sort and
output the array list in ascending order.

Truong Dinh Tu 5

You might also like