In: Computer Science
Write a MIPS assembly language program to find the addition of two arrays
1
#data segment
02
03
.data
04
05
numbers:.word 2,1,5,3,10
06
07
count: .word 5
08
09
10
11
ans1: .asciiz "sum = "
12
13
endl: .asciiz "\n"
14
15
16
17
#text segment
18
19
.text
20
21
22
23
main:
24
25
la $a0, numbers # load array address
26
27
lw $a1, count # load number of elements
28
29
jal sum # call sum function
30
31
32
33
move $a0, $v0 # call print
34
35
jal print
36
37
38
39
j finish # finish
40
41
42
43
sum:
44
45
bge $t2, $a1, done #if t2>=$a1, goto done
46
47
lw $t0, 0($a0) #load value at addr a0 to t0
48
49
add $t1, $t1, $t0 # sum = sum + array[i]
50
51
addi $a0, $a0, 4 # add addr by 4 to get the address of next value in the array
52
53
addi $t2, $t2, 1 # i = i + 1
54
55
j sum
56
57
58
59
done:
60
61
move $v0, $t1 # move result to v0
62
63
jr $ra #go to return addr
64
65
66
67
print:
68
69
# code to print ansl, sum and end
70
71
# deleted for brevity
72
73
jr $ra # return
74
75
76
77
finish:
78
79
li $v0, 10 # exit
80
81
syscall