#MIPS Program implementing the mergesort(..) function # #By Alex # # # .data space: .asciiz " " return: .asciiz "\n" .text .globl main_project1 main_project1:
main:
addi #addi addiu u
$sp, $sp, -56 $sp, $sp, $sp, sp, -56 -56
sw
$ra, 52($sp)
add #addu
$s0, $sp, $zero $s0, $sp, $zero
#set $s0 to Y1[0] #set $s0 to Y1[0]
addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw addi sw
$t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $t0, $s1, $s1,
#Set $t0 = 13 #Y1[0] = 13 ($t0)
add add addi jal add loop: slt beq ) branch to loopend
$zero, 13 0($s0) $zero, 101 4($s0) $zero, 79 8($s0) $zero, 23 12($s0) $zero, 154 16($s0) $zero, 4 20($s0) $zero, 11 24($s0) $zero, 38 28($s0) $zero, 88 32($s0) $zero, 45 36($s0) $zero, 17 40($s0) $zero, 94 44($s0) $zero, 62 48($s0) $zero, 13 52($sp)
$a0, $s0, $zero $a1, $zero, $zero $a2, $s1, -1 Mergesort $s3, $zero, $zero $t0, $s3, $s1 $t0, $zero, loopend
#Y1[1] = 101 #Y1[2] = 79 #Y1[3] = 23 #Y1[3] = 154 #Y1[3] = 4 #Y1[3] = 11 #Y1[3] = 38 #Y1[3] = 88 #Y1[3] = 45 #Y1[3] = 17 #Y1[3] = 94 #Y1[3] = 62 #set $s1 to lenY1 = 13; #Set address of Y1[0] as a0 #Set $a1 = 0 #Set $a2 to lenY1-1 #Set $s3 to i = 0 #if i < lenY1, set $t0 = 1 #if $t0 == 0 (i >= lenY1
addi #li
$v0, $zero, 1 $v0, 1
#system call code for print_int
lw $a0, 0($s0) syscall
#int printed will be Y1[i]
addi #li
$v0, $zero, 4 $v0, 4
#system call code for print_str #system call code for print_str
lui ori #la
$t0, 4097 $a0, $t0, 0 $a0, space
#str printed will be " "
addi
$s0, $s0, 4
#increment to next element in Yi
#addiu
$s0, $s0, 4
#increment to next element in Yi
addi
$s3, $s3, 1
#increment i
beq #j
$zero, $zero, loop loop
#return to start of loop #return to start of loop
addi #li
$v0, $zero, 4 $v0, 4
#system call code for print_str #system call code for print_str
lui ori #la
$t0, 4097 $a0, $t0, 2 $a0, return
#str printed will be "\n"
syscall [] []
loopend:
syscall addi $v0, $zero, 10 #li $v0 10
#system call code for print_str # syscall 10 (exit)
syscall Mergesort:
Else:
slt
$t0, $a1, $a2
beq #bne
$t0, 1, Else $t0, $zero, Else
jr
$ra
addi sw sw sw sw sw add add add
$sp $sp -20 $s0, 16($sp) $s1, 12($sp) $s2, 8($sp) $s3, 4($sp) $ra, 0($sp) $s0, $a0, $zero $s1, $a1, $zero $s2, $a2, $zero #Set arguments for recursive call to mer
gesort sub
$t1, $a2, $a1
#right - left
Call1:
Call2:
addi srl add add
$t1, $t1, $s3, $a2,
$t1, $t1, $a1, $s3,
jal
Mergesort
add add add
$a2, $s2, $zero $a1, $s3, $zero $a0, $s0, $zero
jal
Mergesort
add add add add
$a3, $a2, $a1, $a0,
jal
Merge
lw lw lw lw lw addi
$s0, $s1, $s2, $s3, $ra, $sp,
$s2, $s3, $s1, $s0,
1 1 $t1 -1
#right-left+1 #(right-left+1)>>1 #left+((right-left+1)>>1)
$zero $zero $zero $zero
16($sp) 12($sp) 8($sp) 4($sp) 0($sp) $sp, 20
jr ntil recursion done)
$ra
Merge:
addi sw sw sw sw sw sw
$sp $sp -24 $s0, 20($sp) $s1, 16($sp) $s2, 12($sp) $s3, 8($sp) $s4, 4($sp) $ra, 0($sp)
#Make Room on the Stack
add
$s0, $a0, $zero
#Save address of X[0]
add add addi add
$s1, $s2, $s3, $s4,
#Save leftind #Save rightind #Save endleft #Save endright
While: slt dleft) set $t0=1 beq #ble beq #j Check2: slt =endright) set $t0=1 beq #ble beq #j
#return to calling function (Mergesort u
$a1, $a2, $a2, $a3,
$zero $zero -1 $zero
$t0, $s3, $s1
#if endleft
$t0, $zero, Check2 $s1, $s3, Check2
#leftind<=endleft?
$zero, $zero, End End $t0, $s4, $s2
#if endright
$t0, $zero, If $s2, $s4, If
#rightind<=endright?
$zero, $zero, End End
If:
add add #sll
$t2, $s1, $s1 $t2, $t2, $t2 $t2, $s1, 2
#t2 = 2 * s1 #t2 = 2 * t2 (4 * s1) #multiply leftind by 4 and store
add
$t2, $a0, $t2
#Store address of leftind-th ele
lw
$t0, 0($t2)
#t0 = x[leftind]
add add #sll
$t2, $s2, $s2 $t2, $t2, $t2 $t2, $s2, 2
#t2 = 2 * s2 #t2 = 2 * t2 (4 * s2) #multiply rightind by 4 and stor
add
$t2, $a0, $t2
#Store address of rightind-th el
lw
$t1, 0($t2)
#t1 = x[rightind]
slt
$t8, $t1, $t0
#if x[rightind]
beq #bgt
$t8, 1, Else1 $t0, $t1, Else1
#If x[leftind]>x[rightind] goto
addi
$s1, $s1, 1
#increment leftind
beq #j
$zero, $zero, ElseEnd ElseEnd
add addi add
$t3, $t1, $zero $t4, $s2, -1 $t5, $s1, $zero
in t2 ment t2
e in $t2 ement into t2
t8 = 1 Else
Else1:
For: slt $t9, $t4, $t5 t branch out of for loop beq $t9, 1, ForEnd #blt $t4, $t5, ForEnd ch out of for loop add add #sll
#t3 = temp #t4 = i #t5 = loop end condition #if loop end condition < i, don' #if i < loop end condition, bran
$t7, $t4, $t4 $t7, $t7, $t7 $t7, $t4, 2
#t7 = 2 * t4 #t7 = 2 * t7 (4 * t4) #multiply i by 4 and store in t7
add $t2, $a0, $t7 ement of array x into t2 lw $t6, 0($t2) sw $t6, 4($t2) i+1] addi $t4, $t4, -1
#Store the address of the ith el
beq #j
$zero, $zero, For For
ForEnd:
sw addi addi addi
$t3, $s1, $s2, $s3,
ElseEnd:
beq
$zero, $zero, While
0($t2) $s1, 1 $s2, 1 $s3, 1
#load the value of x[i] into t6 #store the value of x[i] into x[ #decrement i by 1
End:
#j
While
lw lw lw lw lw lw addi jr
$s0, $s1, $s2, $s3, $s4, $ra, $sp, $ra
20($sp) 16($sp) 12($sp) 8($sp) 4($sp) 0($sp) $sp, 24