-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6_1.asm
45 lines (45 loc) · 1.54 KB
/
lab6_1.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
.data
A: .word -2, 6, -1, 3, -2
.text
main:
la $a0,A
li $a1,5
j mspfx
nop
continue:
lock:
j lock
nop
end_of_main:
#-----------------------------------------------------------------
#Procedure mspfx
# @brief find the maximum-prefix sum in a list of integers
# @param[in] a0: thebase address of thelist(A) need to be processed
# @param[in] a1: the number of elements in list(A)
# @param[out]v0: the length of sub-listof Athat has the max sum.
# @param[out]v1: the max prefix sum
#-----------------------------------------------------------------
#Procedure mspfx
#function: find the maximum-prefix sum in a list of integers
#the base address of the list(A) is stored in $a0
#the number of elements is stored in a1
mspfx: addi $v0,$zero,0 #initialize the length in $v0 to0
addi $v1,$zero,0 #initialize the max sum in $v1to 0
addi $t0,$zero,0 #initialize the index i in $t0 to 0
addi $t1,$zero,0 #initialize the running sum in $t1 to 0
loop: add $t2,$t0,$t0 #put 2i in $t2
add $t2,$t2,$t2 #put 4i in $t2
add $t3,$t2,$a0 #put 4i+A (address of A[i]) in $t3
lw $t4,0($t3) #load A[i] from mem(t3) into $t4
add $t1,$t1,$t4 #add A[i] to the running sum in $t1
slt $t5,$v1,$t1 #set $t5 to 1 if max sum < new sum
bne $t5,$zero,mdfy #if the max sum is less, modify results
j test #done?
mdfy: addi $v0,$t0,1 #new max-sum prefix has length i+1
addi $v1,$t1,0 #new max sum is the running sum
test: addi $t0,$t0,1 #incrementthe index i
slt $t5,$t0,$a1 #set $t5 to 1 if i<n
bne $t5,$zero,loop #repeat if i<n
done:
#j continue
mspfx_end: