I am a beginner of MIPS programming and I have some problems on my homework I wa
ID: 3698477 • Letter: I
Question
I am a beginner of MIPS programming and I have some problems on my homework
I was given this C++ program and I need to finish the sort function in MIPS.
I need to finish sortarray function in the below program. I have tried it, but I don't know what mistakes I made.
Would you please help me to solve the problem or give me some hints, thank you!!!!
#------- Data Segment ----------
.data
# Define the string messages and the array
msg1: .asciiz "The original list of random points are "
msg2: .asciiz " The ascending sorted point array are "
space: .asciiz " "
newline: .asciiz " "
leftbracket: .asciiz "("
rightbracket: .asciiz ")"
# x y
# point: .word 20 20
# x_1 y_1 x_2 y_2 x_3 y_3 x_4 y_4 x_5 y_5 x_6 y_6 x_7 y_7 x_8 y_8 x_9 y_9 x_10 y_10
# point_array: .word 20 3 35 173 0 68 0 0 650 456 124 124 16 45 23 14 16 15 20 1
point_array: .word 0:20
#------- Text Segment ----------
.text
.globl main
main:
# $s1 is the array size
addi $s1,$zero,20
# set random seed
li $v0, 30
syscall
addi $a1, $a0, 0
li $a0, 0
li $v0, 40
syscall
# load the starting address of the pointarray to $s0
la $s0, point_array
addi $t0, $zero, 0
# fill the array with $s1 random elements within range [1, 100]
array_filling:
jal random_number_generate
sll $t1, $t0, 2
add $t1, $s0, $t1
sw $v1, 0($t1)
addi $t0, $t0, 1
bne $t0, $s1, array_filling
# Print the original array
jal printoriginal
# Sort array
jal sortarray # sortarry function is what you should implement
# Print the sorted array
jal printresult
# Terminate the program
li $v0, 10
syscall
# Implement sortarray function
# What sortarray should do: Sort the point_array
# $s1 stores the array size =n
# $s0 stores the starting address of the point_array
sortarray:
addi $sp, $sp, -12
sw $s0 ,8($sp)
sw $s1, 4($sp)
sw $ra, 0($sp)
# TODO below:
#addi $sp, $sp, -12
#sw $ra, 0($sp)
addi $t0, $zero, 0 #i
addi $t7,$s1,-1 #n-2
add $t8,$t7,$t0 #n-2-i
addi $t9,$zero,0 #j
loop1:
slt $t5,$t0,$t7
# beq $t0,$t7,end_loop1
beq $t5,$zero,end_loop1
sll $t1, $t0, 2
add $t1, $s0, $t1
loop2:
slt $t5,$t9,$t8
beq $t5,$zero,end_loop2
sll $t5,$t9,2 #offset of j
add $t5,$t5,$s0 #t6 =a[j]
lw $s2,4($t5) #a[j+1]
lw $s3,8($t5) #a[j+2]
lw $s4,12($t5) #a[j+3]
slt $t6,$s3,$t5 #a[j+2]<a[j]
bne $zero,$t6,stat2
beq $s3,$t5, stat2 #[aj+2]==a[j]
j ed
stat2: slt $t6,$s4,$s2
beq $zero,$t6,ed
lw $t2,0($t5)
lw $t3,8($t5)
sw $t3,0($t5)
sw $t2,8($t5)
lw $t2,4($t5)
lw $t3,12($t5)
sw $t3,4($t5)
sw $t1,12($t5)
# sw $t6,8($s0)
ed: addi $t9, $t9, 2
j loop2
end_loop2:
addi $t0, $t0, 2
j loop1
#bne $t0, $s1, array_filling
end_loop1:
lw $ra, 0($sp)
lw $s0 ,8($sp)
lw $s1, 4($sp)
sw $ra, 0($sp)
addi $sp, $sp, 12
jr $ra
# TODO above
# print original array
printoriginal:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, msg1
li $v0, 4
syscall
jal printmain
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#print the sorted array
printresult:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, msg2
li $v0, 4
syscall
jal printmain
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# print array
printmain:
addi $sp, $sp, -4
sw $ra, 0($sp)
#Print a new line
la $a0, newline
li $v0, 4
syscall
addi $t1, $zero, 0 #$t1 is the index for printing sorted A[i]
printloop:
slt $t0, $t1, $s1 #check to see if $t1 (i) is still within the correct range
beq $t0, $zero, endprintloop #if i>=20 end print numbers
addi $t2, $zero, 2
div $t1, $t2
mfhi $t3
bne $t3, $zero, printY
printX:
la $a0, leftbracket
li $v0, 4
syscall
sll $t2, $t1, 2 #$t1*4 to get the byte offset
add $t3, $s0, $t2 #base+byte offset to get address of A[i]
lw $a0, 0($t3)
li $v0, 1
syscall
j printSpace
printY:
sll $t2, $t1, 2 #$t1*4 to get the byte offset
add $t3, $s0, $t2 #base+byte offset to get address of A[i]
lw $a0, 0($t3)
li $v0, 1
syscall
la $a0, rightbracket
li $v0, 4
syscall
#Print a space to separate the numbers
printSpace:
la $a0, space
li $v0, 4
syscall
#i=i+1 and start another iteration of the loop
addi $t1,$t1,1
j printloop
endprintloop:
#Print a new line
la $a0, newline
li $v0, 4
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#no input paramter
#output result in $v0
random_number_generate:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $a0, 4($sp)
addi $a1, $zero, 100 #set upper bound to be 100
li $a0, 0
li $v0, 42
syscall
addi $v1, $a0, 1
lw $a0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
#includeExplanation / Answer
Try this. It should work.
.data
# Define the string messages and the array
msg1: .asciiz "The original list of random points are "
msg2: .asciiz " The ascending sorted point array are "
space: .asciiz " "
newline: .asciiz " "
leftbracket: .asciiz "("
rightbracket: .asciiz ")"
# x y
# point: .word 20 20
# x_1 y_1 x_2 y_2 x_3 y_3 x_4 y_4 x_5 y_5 x_6 y_6 x_7 y_7 x_8 y_8 x_9 y_9 x_10 y_10
# point_array: .word 20 3 35 173 0 68 0 0 650 456 124 124 16 45 23 14 16 15 20 1
point_array: .word 0:20
#------- Text Segment ----------
.text
.globl main
main:
# $s1 is the array size
addi $s1,$zero,20
# set random seed
li $v0, 30
syscall
addi $a1, $a0, 0
li $a0, 0
li $v0, 40
syscall
# load the starting address of the pointarray to $s0
la $s0, point_array
addi $t0, $zero, 0
# fill the array with $s1 random elements within range [1, 100]
array_filling:
jal random_number_generate
sll $t1, $t0, 2
add $t1, $s0, $t1
sw $v1, 0($t1)
addi $t0, $t0, 1
bne $t0, $s1, array_filling
# Print the original array
jal printoriginal
# Sort array
jal sortarray # sortarry function is what you should implement
# Print the sorted array
jal printresult
# Terminate the program
li $v0, 10
syscall
# Implement sortarray function
# What sortarray should do: Sort the point_array
# $s1 stores the array size =n
# $s0 stores the starting address of the point_array
sortarray:
addi $sp, $sp, -12
sw $s0 ,8($sp)
sw $s1, 4($sp)
sw $ra, 0($sp)
# TODO below:
#addi $sp, $sp, -12
#sw $ra, 0($sp)
addi $t0, $zero, 0 #i
addi $t7,$s1,-1 #n-2
add $t8,$t7,$t0 #n-2-i
addi $t9,$zero,0 #j
loop1:
slt $t5,$t0,$t7
# beq $t0,$t7,end_loop1
beq $t5,$zero,end_loop1
sll $t1, $t0, 2
add $t1, $s0, $t1
loop2:
slt $t5,$t9,$t8
beq $t5,$zero,end_loop2
sll $t5,$t9,2 #offset of j
add $t5,$t5,$s0 #t6 =a[j]
lw $s2,4($t5) #a[j+1]
lw $s3,8($t5) #a[j+2]
lw $s4,12($t5) #a[j+3]
slt $t6,$s3,$t5 #a[j+2]<a[j]
bne $zero,$t6,stat2
beq $s3,$t5, stat2 #[aj+2]==a[j]
j ed
stat2: slt $t6,$s4,$s2
beq $zero,$t6,ed
lw $t2,0($t5)
lw $t3,8($t5)
sw $t3,0($t5)
sw $t2,8($t5)
lw $t2,4($t5)
lw $t3,12($t5)
sw $t3,4($t5)
sw $t1,12($t5)
# sw $t6,8($s0)
ed: addi $t9, $t9, 2
j loop2
end_loop2:
addi $t0, $t0, 2
j loop1
#bne $t0, $s1, array_filling
end_loop1:
lw $ra, 0($sp)
lw $s0 ,8($sp)
lw $s1, 4($sp)
sw $ra, 0($sp)
addi $sp, $sp, 12
jr $ra
# TODO above
# print original array
printoriginal:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, msg1
li $v0, 4
syscall
jal printmain
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#print the sorted array
printresult:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, msg2
li $v0, 4
syscall
jal printmain
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# print array
printmain:
addi $sp, $sp, -4
sw $ra, 0($sp)
#Print a new line
la $a0, newline
li $v0, 4
syscall
addi $t1, $zero, 0 #$t1 is the index for printing sorted A[i]
printloop:
slt $t0, $t1, $s1 #check to see if $t1 (i) is still within the correct range
beq $t0, $zero, endprintloop #if i>=20 end print numbers
addi $t2, $zero, 2
div $t1, $t2
mfhi $t3
bne $t3, $zero, printY
printX:
la $a0, leftbracket
li $v0, 4
syscall
sll $t2, $t1, 2 #$t1*4 to get the byte offset
add $t3, $s0, $t2 #base+byte offset to get address of A[i]
lw $a0, 0($t3)
li $v0, 1
syscall
j printSpace
printY:
sll $t2, $t1, 2 #$t1*4 to get the byte offset
add $t3, $s0, $t2 #base+byte offset to get address of A[i]
lw $a0, 0($t3)
li $v0, 1
syscall
la $a0, rightbracket
li $v0, 4
syscall
#Print a space to separate the numbers
printSpace:
la $a0, space
li $v0, 4
syscall
#i=i+1 and start another iteration of the loop
addi $t1,$t1,1
j printloop
endprintloop:
#Print a new line
la $a0, newline
li $v0, 4
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#no input paramter
#output result in $v0
random_number_generate:
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $a0, 4($sp)
addi $a1, $zero, 100 #set upper bound to be 100
li $a0, 0
li $v0, 42
syscall
addi $v1, $a0, 1
lw $a0, 4($sp)
lw $ra, 0($sp)
addi $sp, $sp, 8
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.