Lab9
Code: 3
Give the following an array of integers in MIPS:
.data
Array: .float 43.01, 34.06, XY.09, 78.9, 64.98, 2126.1, 643.2, 451.4 ,423.4, 45.5, 566, 0
Where XY is the whole digit of your ID, e.g. ITITIU22016 → XY is 22016
1. Suppose that the array Array is zero-terminated. Write an assembly procedure
len(Array) using stack that returns the length of the array (20points). Run it step by step
and highlight the address, value of stack.
RESULT
2. Write sort(Array) procedure that prints out the ordered array using Quick sort (40points)
Hint: https://www.programiz.com/dsa/bubble-sort
Please compare this sort with Bubble-sort
Run it step by step and highlight the address, value of calling Sort.
RESULT
3. Write a secondMax(Array) that _nds second highest value in the array. This procedure
should call the sort(Array) procedure in Exercise 2. Hint: you can use any sort to find this value.
(15points)
.data
result:
result1:
result2:
msg1:
newLine:
nextValue:
intArray:
.asciiz "Result is (before sorting): "
.asciiz "Result is (after sorting): "
.asciiz "\nSecond maximum element is: "
.asciiz "The length is: "
.asciiz "\n"
.asciiz " "
.word 43, 6543, 34, 54, 4232, 64, 526, 643, 6435, 423, 4236, 566, 54, 0
sortedArray: .space 1024
.text
.globl main
main:
# a1 = address of arr
la $a1, intArray
li $s0, 0
jal LEN
la $a0, msg1
li $v0, 4
syscall
move $a0, $s0
li $v0, 1
syscall
la $a0, newLine
li $v0, 4
syscall
la $a1, intArray
jal DISPLAY_ARRAY
la $a0, newLine
li $v0, 4
syscall
la $a1, intArray
jal SORT
la $a0, newLine
li $v0, 4
syscall
jal DISPLAY_ARRAY_AFTER_SORT
la $a1, intArray
li $t0, 0 # generate t0 = i
jal FIND_SECOND_MAX
la $a0, newLine
li $v0, 4
syscall
li $v0, 10
syscall
LEN:
lw $t2, 0($a1)
beq $t2, $zero, end_function
addi $a1, $a1, 4
addi $s0, $s0, 1 # s0 = 13
j LEN
SORT:
j OUTER_LOOP
OUTER_LOOP:
bge $t0, $s0, end_function
li $t9, 0
li $t1, 0
j INNER_LOOP
INNER_LOOP:
sub $t2, $s0, $t0
addi $t2, $t2, -1
bge $t1, $t2, CHECK_LOOP
li $t3, 4
mult $t3, $t1
mflo $t3
add $t3, $a1, $t3
addi $t1, $t1, 1
lw $t4, 0($t3)
lw $t5, 4($t3)
ble $t4, $t5, INNER_LOOP
sw $t5, 0($t3)
sw $t4, 4($t3)
li $t9, 1
j INNER_LOOP
CHECK_LOOP:
beq $t9, 0, end_function
addi $t0, $t0, 1
j OUTER_LOOP
DISPLAY_ARRAY:
la $a0, result
li $v0, 4
syscall
j DISPLAY_LOOP_INTERNAL
DISPLAY_ARRAY_AFTER_SORT:
la $a0, result1
li $v0, 4
syscall
j DISPLAY_LOOP_INTERNAL
DISPLAY_LOOP_INTERNAL:
lw $t2, 0($a1)
beq $t2, $zero, end_function
move $a0, $t2
li $v0, 1
syscall
la $a0, nextValue
li $v0, 4
syscall
addi $a1, $a1, 4
j DISPLAY_LOOP_INTERNAL
FIND_SECOND_MAX:
move $t6, $zero
move $t7, $zero
FIND_LOOP:
lw $t8, 0($a1)
beq $t8, $zero, PRINT_RESULT
ble $t8, $t7, SKIP_UPDATE
move $t6, $t7
move $t7, $t8
SKIP_UPDATE:
addi $a1, $a1, 4
j FIND_LOOP
PRINT_RESULT:
li $v0, 4
la $a0, result2
syscall
move $a0, $t6
li $v0, 1
syscall
jr $ra
end_function:
jr $ra
RESULT
4. Rewrite Max(Array) without sorting the Array _rst. (25points)
RESULT