home / study / engineering / computer science / computer science questions and a
ID: 3601465 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers /'
mips insertion sort outline:
for i 1 to length(a) j i
while j > 0 and a[j-1] > a[j] ... Question: MIPS Insertion sort Outline: for i 1 to length(A) j i while j > 0 and A[j-1] > A[j... MIPS Insertion sort Outline: for i 1 to length(A) j i while j > 0 and A[j-1] > A[j] swap A[j] and A[j-1] j j - 1 end while end for Code: I have already started on it, but there is error that I couldn't figure out why :( .text #------------------------------------------- # Procedure: insertion_sort # Argument: # $a0: Base address of the array # $a1: Number of array element # Return: # None # Notes: Implement insertion sort, base array # at $a0 will be sorted after the routine # is done. #------------------------------------------- insertion_sort: # Caller RTE store (TBD) addi $sp, $sp, -40 sw $fp, 36($sp) sw $ra, 32($sp) sw $a0, 28($sp) sw $a1, 24($sp) sw $a2, 20($sp) sw $a3, 16($sp) sw $s0, 12($sp) sw $s1, 8($sp) addi $fp, $sp, 40 # Implement insertion sort (TBD) move $a2, $zero # counter i for: add $a2, $a2, 1 #increment counter i by 1 beq $a2, $a1, insertion_sort_end #if i == number of array element move $a3, $a2 # a3 is the counter j, set j = i whilestatement: ble $a3, $zero, for #if j is less than 0, jump to bgt $a3, $zero, while # while the counter j is less than a1(#of array element), jump to while while: move $t0, $a3 mul $t0, $t0, 4 # multiply j * 4 add $s0, $t0, $a0 #A[j] add $s1, $s0, -4 #A[J-1] bgt $s1, $s0, swap # if A[j-1] > A[j] swap: move $t4, $s0 # set t4 to A[j] move $s0, $s1 move $s1, $t4 add $a3, $a3, -1 j whilestatement insertion_sort_end: # Caller RTE restore (TBD) lw $fp, 36($sp) lw $ra, 32($sp) lw $a0, 28($sp) lw $a1, 24($sp) lw $a2, 20($sp) lw $a3, 16($sp) lw $s0, 12($sp) lw $s1, 8($sp) addi $sp, $sp, 40 # Return to Caller jr $ra
Explanation / Answer
i have modified and changed the code for insertion sort in MIPS
.data
arr: .space 100
spaceP: .asciiz" "
.text
.globl main
.ent main
main:
la $a0, arr
addi $s0, $a0, 0
inputLoop:
li $v0, 5
syscall
addi $t0, $v0, 0
beq $t0, 0, exit
sw $t0, ($a0) # add number to array
addi $a0, $a0, 4
j inputLoop
exit:
addi $a0, $s0, 0
outerLoop:
addi $a0, $a0, 4
lw $s1, ($a0)
beq $s1, $0, exit2
addi $s4, $a0, 0
sub $s4, $s4, 4
innerLoop:
lw $s2, ($s4)
beq $s2, $0, exit1
ble $s2, $s1, exit1
addi $s3, $s4, 0
addi $s3, $s3, 4
sw $s2, ($s3)
sub $s4, $s4, 4
addi $s3, $s4, 0
addi $s3, $s3, 4
sw $s1, ($s3)
j innerLoop
exit1:
j outerLoop
exit2:
addi $a1, $s0, 0
printLoop:
lw $t0, ($a1)
beq $t0, $0, exit3
li $v0, 1
addi $a0, $t0, 0
syscall
addi $a1, $a1, 4
li $v0, 4
la $a0, spaceP
syscall
j printLoop
exit3:
li $v0, 10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.