Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Below program has a Bubble Sort algorithm written in Assembly. Change this to use Selection Sort instead. Use the following data in your array: 4
Below program has a Bubble Sort algorithm written in Assembly. Change this to use Selection Sort instead.
Use the following data in your array:
Sort the data in ascending order and print out the result. The following assembly program implements the Bubble Sort matching the pseudocode
algorithm in the previous section.
text
globl main
main:
la $a
array
base
lw $a
array
size
jal PrintIntArray
la $a
array
base
lw $a
array
size
jal BubbleSort
jal PrintNewLine
la $a
array
base
lw $a
array
size
jal PrintIntArray
jal Exit
data
array
size:
word
array
base:
word
word
word
word
word
word
word
word
# Subproram: Bubble Sort
# Purpose: Sort data using a Bubble Sort algorithm
# Input Params: $a
array
# $a
array size
# Register conventions:
# $s
array base
# $s
array size
# $s
outer loop counter
# $s
inner loop counter
text
BubbleSort:
addi $sp
$sp
# save stack information
sw $ra
$sp
sw $s
$sp
# need to keep and restore save registers
sw $s
$sp
sw $s
$sp
sw $s
$sp
move $s
$a
move $s
$a
addi $s
$zero,
#outer loop counter
OuterLoop:
addi $t
$s
slt $t
$s
$t
beqz $t
EndOuterLoop
addi $s
$zero,
#inner loop counter
InnerLoop:
addi $t
$s
sub $t
$t
$s
slt $t
$s
$t
beqz $t
EndInnerLoop
sll $t
$s
# load data
j
Note offset is
bytes
add $t
$s
$t
lw $t
$t
addi $t
$t
# load data
j
lw $t
$t
sgt $t
$t
$t
beqz $t
NotGreater
move $a
$s
move $a
$s
addi $t
$s
move $a
$t
jal Swap # t
is &data
j
t
is &data
j
NotGreater:
addi $s
$s
b InnerLoop
EndInnerLoop:
addi $s
$s
b OuterLoop
EndOuterLoop:
lw $ra
$sp
#restore stack information
lw $s
$sp
lw $s
$sp
lw $s
$sp
lw $s
$sp
addi $sp
$sp
jr $ra
# Subprogram: swap
# Purpose: to swap values in an array of integers
# Input parameters: $a
the array containing elements to swap
# $a
index of element
# $a
index of elelemnt
# Side Effects: Array is changed to swap element
and
Swap:
sll $t
$a
# calcualate address of element
add $t
$a
$t
sll $t
$a
# calculate address of element
add $t
$a
$t
lw $t
$t
#swap elements
lw $t
$t
sw $t
$t
sw $t
$t
jr $ra
# Subprogram: PrintIntArray
# Purpose: print an array of ints
# inputs: $a
the base address of the array
# $a
the size of the array
#
PrintIntArray:
addi $sp
$sp
# Stack record
sw $ra
$sp
sw $s
$sp
sw $s
$sp
sw $s
$sp
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started