Lab 7 - ARRAY
Lab 7 - ARRAY
Lab 7 - ARRAY
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
Truong Dinh Tu 1
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization
Truong Dinh Tu 2
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization
3. Array Access
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
#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