finish rest of code # Course: CSC 211 # Description: Create a algorithm # # # fo
ID: 3937838 • Letter: F
Question
finish rest of code
# Course: CSC 211
# Description: Create a algorithm
#
#
# for (i = 0; i < n-1; i++)
# for (j = i+1; j < n; j++)
# if ( a[i] > a[j])
# swap (a[i], a[j]);
#
#################################################################
jal sort # call the sort subprogram
PROGRAM LOGIC
sort: la $t0,array # t0 = address of a[i]
move $t1,$t0 # t1 = address of a[j]
add $t1,$t1,4
lw $t2,count # t2 = count (i counter)
add $t2,$t2,-1 # t2 = count-1
move $t3,$t2 # t3 = t2 (j counter)
sloop: lw $t4,($t0) # t4 = a[i]
lw $t5,($t1) # t5 = a[j]
ble $t4,$t5,noswap # if (a[i] <= a[j]) do not swap
sw $t4,($t1) # swap a[i] and a[j]
sw $t5,($t0)
noswap: add $t1,$t1,4 # t1 = next address of a[j]
# j count --
# if j count != 0 go back to sort loop
# t0 = next address of a[i]
# i count --
# j count = i count
# if i count = 0 sort is complete
# t1 = the next address of a[i]
# jump to sort loop
endsort:
# return to main procedure
Explanation / Answer
PROGRAM LOGIC
sort: la $t0,array # t0 = address of a[i]
move $t1,$t0 # t1 = address of a[j]
add $t1,$t1,4
lw $t2,count # t2 = count (i counter)
add $t2,$t2,-1 # t2 = count-1
move $t3,$t2 # t3 = t2 (j counter)
sloop: lw $t4,($t0) # t4 = a[i]
lw $t5,($t1) # t5 = a[j]
ble $t4,$t5,noswap # if (a[i] <= a[j]) do not swap
sw $t4,($t1) # swap a[i] and a[j]
sw $t5,($t0)
noswap: add $t1,$t1,4 # t1 = next address of a[j]
# j count --
# if j count != 0 go back to sort loop
# t0 = next address of a[i]
# i count --
# j count = i count
# if i count = 0 sort is complete
# t1 = the next address of a[i]
# jump to sort loop
endsort:
# return to main procedure
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.