SlideShare a Scribd company logo
# Merge Sort
# AdamTuckerand AmandaVerno
# Assignment#6 Problem
# Version12/09/2015
.data # Data declarationsection
_msg0: .asciiz"UnsortedArray"
_msg1: .asciiz"SortedArray"
_newline: .asciiz"n"
_spaces: .asciiz" "
Array1: .word 7 16 3 1 5 9 8 2 6 4
10 15 19 13 14 17 20 18 12 11
Array2: .word 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
.text
.globl main
main:
addi $sp, $sp, -4 # make roomon the stack for returnaddress
sw $ra, 0($sp) # save the returnaddress
#Load array address
la $a0, Array2 # addressof temparray
la $a1, Array1 # loadaddressof unsortedarray
addi $a2, $zero,20 # loadsize of array into$a2
and $a3, $zero,$zero # initializinglow =0
or $t0, $a1, $zero # addressof unsortedarray
or $t3, $a2, $zero # array length
#Printmessage
li $v0, 4 # systemcall code forprint_str
la $a0, _msg0 # addressof stringto print
syscall
#Printnewline
li $v0, 4 # systemcall code forprint_str
la $a0, _newline # addressof stringto print
syscall
and $t4, $zero,$zero # seti to 0
j LoopPrintUn # jumpto printunsortedarray
PrepSort:
addi $sp, $sp, -16 # make roomon the stack
sw $ra, 0($sp) # returnaddress
sw $a1, 8($sp) # save addressunsortedarray
add $a2, $a2, -1 # setting$a2 to (high - 1)
sw $a2, 4($sp) # save the size of the (array-1)
sw $a3, 0($sp) # low parameter
jal MSORT # jumpto merge sortwitharguments(array,high,low)
PrintSorted:
#Printnewline
li $v0, 4 # systemcall code forprint_str
la $a0, _newline # addressof stringto print
syscall # printthe string
#Printmessage
li $v0, 4 # systemcall code forprint_str
la $a0, _msg1 # addressof stringto print
syscall
#Printnewline
li $v0, 4 # systemcall code forprint_str
la $a0, _newline # addressof stringto print
syscall
and $t7, $zero,$zero # seti to 0
jal LoopPrintSort # jumpto printthe sortedarray
LoopPrintUn: # printunsortedarray
#While (i < length)
slt $t6, $t4, $a2 # if i < the lengthof the array
beq $t6, $zero,PrepSort # if (length<=i) thenjumptoprep formerge sort
# Load Array[i] andprintit
sll $t0, $t4, 2 # i * 4
add $t6, $a1, $t0 # base addressof array + offest
li $v0, 1 # systemcall code forprint_int
lw $a0, 0($t6) # shiftamountarray itme
syscall # printit
#Printspaces
li $v0, 4 # systemcall code forprint_str
la $a0, _spaces # addressof stringto print
syscall
addi $t4, $t4, 1 # i ++
j LoopPrintUn # loopprintforunsortedarray
LoopPrintSort: # printsortedarray
#While (i < length)
slt $t6, $t7, 20 # if i < the lengthof the array
beq $t6, $zero,EndProgram # if (length<=i) thenexitloop
sll $t0, $t7, 2 # i * 4
add $t6, $a1, $t0 # base addressof array + offest
li $v0, 1 # systemcall code forprint_int
lw $a0, 0($t6) # shiftamountarray itme
syscall # printarray [i]
#Printspaces
li $v0, 4 # systemcall code forprint_str
la $a0, _spaces # addressof stringto print
syscall
addi $t7, $t7, 1 # i ++
jal LoopPrintSort # loopprint
EndProgram:
addi $sp, $sp, 20 # popall itemsfromthe stack inmain
li $v0, 10 # END OFPROGRAM
syscall # END OFPROGRAM
MSORT:
addi$sp,$sp, -20 # make roomon the stack
sw $ra, 16($sp) # save returnaddress
sw $s1, 12($sp) # save argumentsunsortedarrayaddress
sw $s2, 8($sp) # save argumentssize of array= high
sw $s3, 4($sp) # save low size of array
sw $s4, 0($sp) # save registerformid
or $s1, $zero,$a1 # $s1 <- array address
or $s2, $zero,$a2 # $s2 <- size of array - 1 = high
or $s3, $zero,$a3 # $s3 <- low size
slt$t3, $s3, $s2 # low < high
beq$t3, $zero,DONE # if $t3 == 0, DONE
add $s4, $s3, $s2 # low + high
div$s4, $s4, 2 # $s4 <- (low+high)/2
or $a2, $zero, $s4 # argumentlow
or $a3, $zero, $s3 # argumentmid
jal MSORT # recursive call for(array,low,mid)
# mergesort(a,mid+1,high)
addi$t4, $s4, 1 # argumentmid+1
or $a3, $zero, $t4 # low gets(mid+1)
or $a2, $zero, $s2 # highgetshigh
jal MSORT # recursive call for(a,mid+1,high)
or $a1, $zero,$s1 # Argument1 getsarray address
or $a2, $zero,$s2 # Argument2 getshigh
or $a3, $zero,$s3 # Argument3 getslow
or $a0, $zero,$s4 # Argument4 getsmid
jal MERGE # jumpto merge (array,high,low,mid)
DONE:
lw $ra, 16($sp) # loadreturnaddress
lw $s1, 12($sp) # loadargumentsarray address
lw $s2, 8($sp) # loadargumentssize of array = high
lw $s3, 4($sp) # low size of array
lw $s4, 0($sp) # registerformid
addi $sp, $sp, 20 # clearroom onthe stack
jr $ra # jumpto register
MERGE:
addi$sp,$sp, -20 # make roomon the stack
sw $ra, 16($sp) # save returnaddress
sw $s1, 12($sp) # save argumentsunsortedarrayaddress
sw $s2, 8($sp) # save argumentssize of array= high
sw $s3, 4($sp) # save low size of array
sw $s4, 0($sp) # save registerformid
or $s1, $zero,$a1 # array address
or $s2, $zero,$a2 # $s2 <- size of array = high
or $s3, $zero,$a3 # $s3 <- low size
or $s4, $zero,$a0 # $s4 <- mid
or $t1, $zero, $s3 # i= $t1 getslow
or $t2, $zero, $s4 # j= $t2 getsmid
addi$t2, $t2, 1 # j= $t2 getsmid+ 1
or $t3, $zero, $a3 # k = low
WHILE:
slt $t4, $s4, $t1 # mid< i (i>=mid)
bne $t4, $zero,while2 # go to while 2if i >= mid
slt $t5, $s2, $t2 # high< j (j>=high)
bne $t5, $zero,while2 # && go to while 2if j >= high
sll $t6, $t1, 2 # i*4
add $t6, $s1, $t6 # $t6 = addressa[i]
lw $s5, 0($t6) # $s5 = a[i]
sll $t7, $t2, 2 # j*4
add $t7, $s1, $t7 # $t7 = addressa[j]
lw $s6, 0($t7) # $s6 = a[j]
slt $t4, $s5, $s6 # a[i] < a[j]
beq $t4, $zero,ELSE # go to else if a[i] >=a[j}
sll $t8, $t3, 2 # k*4
la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint
statements)
add $t8, $a0, $t8 # $t8 = addressc[k]
sw $s5, 0($t8) # c[k] = a[i]
addi $t3, $t3, 1 # k++
addi $t1, $t1, 1 # i++
j WHILE
ELSE:
sll $t8, $t3, 2 # i*4
la $a0, Array2 # # loadaddressof temporaryarray(neccasaryif using$a0 for print
statements)
add $t8, $a0, $t8 # $t8 = addressc[k]
sw $s6, 0($t8) # c[k] = a[j]
addi $t3, $t3, 1 # k++
addi $t2, $t2, 1 # j++
j WHILE
while2:
slt $t4, $s4, $t1 # mid< i (i>=mid)
bne $t4, $zero,while3 # go to while3if i >= mid
sll $t6, $t1, 2 # i*4
add $t6, $s1, $t6 # $t6 = addressa[i]
lw $s5, 0($t6) # $s5 = a[i]
sll $t8, $t3, 2 # i*4
la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint
statements)
add $t8, $a0, $t8 # $t8 = addressc[k]
sw $s5, 0($t8) # c[k] = a[i]
addi $t3, $t3, 1 # k++
addi $t1, $t1, 1 # i++
j while2
while3:
slt $t5, $s2, $t2 # high< j (j>=high)
bne $t5, $zero,start # go to forloopif j >= high
sll $t7, $t2, 2 # i*4
add $t7, $s1, $t7 # $t7 = addressa[j]
lw $s6, 0($t7) # $s6 = a[j]
sll $t8, $t3, 2 # i*4
la $a0, Array2 # loadaddressof temporaryarray (neccasary if using$a0 forprint
statements)
add $t8, $a0, $t8 # $t8 = addressc[k]
sw $s6, 0($t8) # c[k] = a[j]
addi $t3, $t3, 1 # k++
addi $t2, $t2, 1 # j++
j while3
start:
or $t1, $zero,$s3 # i <- low
forloop:
slt $t5, $t1, $t3 # i < k
beq $t5, $zero,DONE # branchif mergingiscomplete
sll $t6, $t1, 2 # i*4
add $t6, $s1, $t6 # $t6 = addressa[i]
sll $t8, $t1, 2 # i*4
la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint
statements)
add $t8, $a0, $t8 # $t8 = addressc[i]
lw $s7, 0($t8) # $s7 = c[i]
sw $s7, 0($t6) # a[i]
addi $t1, $t1, 1 # i++
j forloop
Ad

More Related Content

What's hot (20)

Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
greedy algorithm Fractional Knapsack
greedy algorithmFractional Knapsack greedy algorithmFractional Knapsack
greedy algorithm Fractional Knapsack
Md. Musfiqur Rahman Foysal
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Space complexity
Space complexitySpace complexity
Space complexity
Bhanusree Koduru
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Heap sort
Heap sortHeap sort
Heap sort
GayathriS578276
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
Dr. Jasmine Beulah Gnanadurai
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 
Array operations
Array operationsArray operations
Array operations
ZAFAR444
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
ajmalcs
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
Bhanwar Singh Meena
 
Stack
StackStack
Stack
Seema Sharma
 
Queues
QueuesQueues
Queues
Hareem Aslam
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
Faraz Ahmed
 
Round Robin Algorithm.pdf
Round Robin Algorithm.pdfRound Robin Algorithm.pdf
Round Robin Algorithm.pdf
PrashantKhobragade3
 
Chapter 4: Vector Spaces - Part 1/Slides By Pearson
Chapter 4: Vector Spaces - Part 1/Slides By PearsonChapter 4: Vector Spaces - Part 1/Slides By Pearson
Chapter 4: Vector Spaces - Part 1/Slides By Pearson
Chaimae Baroudi
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Syeda
 
Bishop - Pattern Recognition and Machine Learning.pdf
Bishop - Pattern Recognition and Machine Learning.pdfBishop - Pattern Recognition and Machine Learning.pdf
Bishop - Pattern Recognition and Machine Learning.pdf
SaranyaThinakaran1
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Dynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack ProblemDynamic Programming-Knapsack Problem
Dynamic Programming-Knapsack Problem
Amrita Yadav
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
dchuynh
 
Array operations
Array operationsArray operations
Array operations
ZAFAR444
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
ajmalcs
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
Faraz Ahmed
 
Chapter 4: Vector Spaces - Part 1/Slides By Pearson
Chapter 4: Vector Spaces - Part 1/Slides By PearsonChapter 4: Vector Spaces - Part 1/Slides By Pearson
Chapter 4: Vector Spaces - Part 1/Slides By Pearson
Chaimae Baroudi
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
Syeda
 
Bishop - Pattern Recognition and Machine Learning.pdf
Bishop - Pattern Recognition and Machine Learning.pdfBishop - Pattern Recognition and Machine Learning.pdf
Bishop - Pattern Recognition and Machine Learning.pdf
SaranyaThinakaran1
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 

Similar to MIPS Merge Sort (20)

What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdfWhat is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
badshetoms
 
Mips1
Mips1Mips1
Mips1
Stefano Salvatori
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
osfameron
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docxcs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
mydrynan
 
Space Invaders Source Code
Space Invaders Source CodeSpace Invaders Source Code
Space Invaders Source Code
Mitchell Strobel
 
Hop ngu MIP
Hop ngu MIPHop ngu MIP
Hop ngu MIP
satlove02
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdf
fasttrackcomputersol
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
abrummett
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
brian d foy
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdfWhat is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
What is the Insertion Sort MIPS Assembly codeSolution.globl m.pdf
badshetoms
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
osfameron
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
Simon Proctor
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docxcs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
cs47_proj_sourceCS47_proj_alu_normal.asm.include .cs47_proj.docx
mydrynan
 
Space Invaders Source Code
Space Invaders Source CodeSpace Invaders Source Code
Space Invaders Source Code
Mitchell Strobel
 
Finish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdfFinish the program below that does several bit-wise manipulations of.pdf
Finish the program below that does several bit-wise manipulations of.pdf
fasttrackcomputersol
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
Pete McFarlane
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
abrummett
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Ad

MIPS Merge Sort

  • 1. # Merge Sort # AdamTuckerand AmandaVerno # Assignment#6 Problem # Version12/09/2015 .data # Data declarationsection _msg0: .asciiz"UnsortedArray" _msg1: .asciiz"SortedArray" _newline: .asciiz"n" _spaces: .asciiz" " Array1: .word 7 16 3 1 5 9 8 2 6 4 10 15 19 13 14 17 20 18 12 11 Array2: .word 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .text .globl main main: addi $sp, $sp, -4 # make roomon the stack for returnaddress sw $ra, 0($sp) # save the returnaddress #Load array address la $a0, Array2 # addressof temparray la $a1, Array1 # loadaddressof unsortedarray addi $a2, $zero,20 # loadsize of array into$a2 and $a3, $zero,$zero # initializinglow =0 or $t0, $a1, $zero # addressof unsortedarray or $t3, $a2, $zero # array length #Printmessage li $v0, 4 # systemcall code forprint_str la $a0, _msg0 # addressof stringto print syscall #Printnewline li $v0, 4 # systemcall code forprint_str la $a0, _newline # addressof stringto print syscall
  • 2. and $t4, $zero,$zero # seti to 0 j LoopPrintUn # jumpto printunsortedarray PrepSort: addi $sp, $sp, -16 # make roomon the stack sw $ra, 0($sp) # returnaddress sw $a1, 8($sp) # save addressunsortedarray add $a2, $a2, -1 # setting$a2 to (high - 1) sw $a2, 4($sp) # save the size of the (array-1) sw $a3, 0($sp) # low parameter jal MSORT # jumpto merge sortwitharguments(array,high,low) PrintSorted: #Printnewline li $v0, 4 # systemcall code forprint_str la $a0, _newline # addressof stringto print syscall # printthe string #Printmessage li $v0, 4 # systemcall code forprint_str la $a0, _msg1 # addressof stringto print syscall #Printnewline li $v0, 4 # systemcall code forprint_str la $a0, _newline # addressof stringto print syscall and $t7, $zero,$zero # seti to 0 jal LoopPrintSort # jumpto printthe sortedarray LoopPrintUn: # printunsortedarray #While (i < length) slt $t6, $t4, $a2 # if i < the lengthof the array beq $t6, $zero,PrepSort # if (length<=i) thenjumptoprep formerge sort # Load Array[i] andprintit sll $t0, $t4, 2 # i * 4 add $t6, $a1, $t0 # base addressof array + offest
  • 3. li $v0, 1 # systemcall code forprint_int lw $a0, 0($t6) # shiftamountarray itme syscall # printit #Printspaces li $v0, 4 # systemcall code forprint_str la $a0, _spaces # addressof stringto print syscall addi $t4, $t4, 1 # i ++ j LoopPrintUn # loopprintforunsortedarray LoopPrintSort: # printsortedarray #While (i < length) slt $t6, $t7, 20 # if i < the lengthof the array beq $t6, $zero,EndProgram # if (length<=i) thenexitloop sll $t0, $t7, 2 # i * 4 add $t6, $a1, $t0 # base addressof array + offest li $v0, 1 # systemcall code forprint_int lw $a0, 0($t6) # shiftamountarray itme syscall # printarray [i] #Printspaces li $v0, 4 # systemcall code forprint_str la $a0, _spaces # addressof stringto print syscall addi $t7, $t7, 1 # i ++ jal LoopPrintSort # loopprint EndProgram: addi $sp, $sp, 20 # popall itemsfromthe stack inmain li $v0, 10 # END OFPROGRAM syscall # END OFPROGRAM MSORT: addi$sp,$sp, -20 # make roomon the stack sw $ra, 16($sp) # save returnaddress sw $s1, 12($sp) # save argumentsunsortedarrayaddress sw $s2, 8($sp) # save argumentssize of array= high
  • 4. sw $s3, 4($sp) # save low size of array sw $s4, 0($sp) # save registerformid or $s1, $zero,$a1 # $s1 <- array address or $s2, $zero,$a2 # $s2 <- size of array - 1 = high or $s3, $zero,$a3 # $s3 <- low size slt$t3, $s3, $s2 # low < high beq$t3, $zero,DONE # if $t3 == 0, DONE add $s4, $s3, $s2 # low + high div$s4, $s4, 2 # $s4 <- (low+high)/2 or $a2, $zero, $s4 # argumentlow or $a3, $zero, $s3 # argumentmid jal MSORT # recursive call for(array,low,mid) # mergesort(a,mid+1,high) addi$t4, $s4, 1 # argumentmid+1 or $a3, $zero, $t4 # low gets(mid+1) or $a2, $zero, $s2 # highgetshigh jal MSORT # recursive call for(a,mid+1,high) or $a1, $zero,$s1 # Argument1 getsarray address or $a2, $zero,$s2 # Argument2 getshigh or $a3, $zero,$s3 # Argument3 getslow or $a0, $zero,$s4 # Argument4 getsmid jal MERGE # jumpto merge (array,high,low,mid) DONE: lw $ra, 16($sp) # loadreturnaddress lw $s1, 12($sp) # loadargumentsarray address lw $s2, 8($sp) # loadargumentssize of array = high lw $s3, 4($sp) # low size of array lw $s4, 0($sp) # registerformid addi $sp, $sp, 20 # clearroom onthe stack jr $ra # jumpto register MERGE: addi$sp,$sp, -20 # make roomon the stack sw $ra, 16($sp) # save returnaddress sw $s1, 12($sp) # save argumentsunsortedarrayaddress sw $s2, 8($sp) # save argumentssize of array= high sw $s3, 4($sp) # save low size of array sw $s4, 0($sp) # save registerformid or $s1, $zero,$a1 # array address
  • 5. or $s2, $zero,$a2 # $s2 <- size of array = high or $s3, $zero,$a3 # $s3 <- low size or $s4, $zero,$a0 # $s4 <- mid or $t1, $zero, $s3 # i= $t1 getslow or $t2, $zero, $s4 # j= $t2 getsmid addi$t2, $t2, 1 # j= $t2 getsmid+ 1 or $t3, $zero, $a3 # k = low WHILE: slt $t4, $s4, $t1 # mid< i (i>=mid) bne $t4, $zero,while2 # go to while 2if i >= mid slt $t5, $s2, $t2 # high< j (j>=high) bne $t5, $zero,while2 # && go to while 2if j >= high sll $t6, $t1, 2 # i*4 add $t6, $s1, $t6 # $t6 = addressa[i] lw $s5, 0($t6) # $s5 = a[i] sll $t7, $t2, 2 # j*4 add $t7, $s1, $t7 # $t7 = addressa[j] lw $s6, 0($t7) # $s6 = a[j] slt $t4, $s5, $s6 # a[i] < a[j] beq $t4, $zero,ELSE # go to else if a[i] >=a[j} sll $t8, $t3, 2 # k*4 la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint statements) add $t8, $a0, $t8 # $t8 = addressc[k] sw $s5, 0($t8) # c[k] = a[i] addi $t3, $t3, 1 # k++ addi $t1, $t1, 1 # i++ j WHILE ELSE: sll $t8, $t3, 2 # i*4 la $a0, Array2 # # loadaddressof temporaryarray(neccasaryif using$a0 for print statements) add $t8, $a0, $t8 # $t8 = addressc[k] sw $s6, 0($t8) # c[k] = a[j] addi $t3, $t3, 1 # k++ addi $t2, $t2, 1 # j++ j WHILE while2:
  • 6. slt $t4, $s4, $t1 # mid< i (i>=mid) bne $t4, $zero,while3 # go to while3if i >= mid sll $t6, $t1, 2 # i*4 add $t6, $s1, $t6 # $t6 = addressa[i] lw $s5, 0($t6) # $s5 = a[i] sll $t8, $t3, 2 # i*4 la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint statements) add $t8, $a0, $t8 # $t8 = addressc[k] sw $s5, 0($t8) # c[k] = a[i] addi $t3, $t3, 1 # k++ addi $t1, $t1, 1 # i++ j while2 while3: slt $t5, $s2, $t2 # high< j (j>=high) bne $t5, $zero,start # go to forloopif j >= high sll $t7, $t2, 2 # i*4 add $t7, $s1, $t7 # $t7 = addressa[j] lw $s6, 0($t7) # $s6 = a[j] sll $t8, $t3, 2 # i*4 la $a0, Array2 # loadaddressof temporaryarray (neccasary if using$a0 forprint statements) add $t8, $a0, $t8 # $t8 = addressc[k] sw $s6, 0($t8) # c[k] = a[j] addi $t3, $t3, 1 # k++ addi $t2, $t2, 1 # j++ j while3 start: or $t1, $zero,$s3 # i <- low forloop: slt $t5, $t1, $t3 # i < k beq $t5, $zero,DONE # branchif mergingiscomplete sll $t6, $t1, 2 # i*4 add $t6, $s1, $t6 # $t6 = addressa[i] sll $t8, $t1, 2 # i*4 la $a0, Array2 # loadaddressof temporaryarray (neccasaryif using$a0 forprint statements) add $t8, $a0, $t8 # $t8 = addressc[i]
  • 7. lw $s7, 0($t8) # $s7 = c[i] sw $s7, 0($t6) # a[i] addi $t1, $t1, 1 # i++ j forloop