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 arraybase
lw $a arraysize
jal PrintIntArray
la $a arraybase
lw $a arraysize
jal BubbleSort
jal PrintNewLine
la $a arraybase
lw $a arraysize
jal PrintIntArray
jal Exit
data
arraysize: word
arraybase:
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 dataj Note offset is bytes
add $t $s $t
lw $t$t
addi $t $t # load dataj
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 &dataj t is &dataj
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
move $s $a # save the base of the array to $s
# Initialization for counter loop
# $s is the ending index of the loop
# $s is the loop counter
move $s $a
move $s $zero
la $a openbracket # print open bracket
jal PrintString
loop:
# check ending condition
sge $t $s $s
bnez $t endloop
sll $t $s # Multiply the loop counter by
# by to get offset each element
INTRODUCTION TO MIPS ASSEMBLY LANGUAGE PROGRAMMING
# is big
add $t $t $s # address of next array element
lw $a$t # Next array element
la $a comma
jal PrintInt # print the integer from array
addi $s $s #increment $s
b loop
endloop:
li $v # print close bracket
la $a closebracket
syscall
lw $ra$sp
lw $s$sp
lw $s$sp
lw $s$sp # restore stack and return
addi $sp $sp
jr $ra
data
openbracket: asciiz
closebracket: asciiz
comma: asciiz
include "utils.asm"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started