Skip to content

Instantly share code, notes, and snippets.

@tvand7093
Last active August 29, 2015 14:10
Show Gist options
  • Save tvand7093/f188966d44d4aa2235ca to your computer and use it in GitHub Desktop.
Save tvand7093/f188966d44d4aa2235ca to your computer and use it in GitHub Desktop.
A bubble sort Algorithm for the Gettysburg Address
# Tyler Vanderhoef - CS 260 - Quicksort.asm
.data
newline: .asciiz "\n"
str0: .asciiz "Four"
str1: .asciiz "score"
str2: .asciiz "and"
str3: .asciiz "seven"
str4: .asciiz "years"
str5: .asciiz "ago"
str6: .asciiz "our"
str7: .asciiz "fathers"
str8: .asciiz "brought"
str9: .asciiz "forth"
str10: .asciiz "on"
str11: .asciiz "this"
str12: .asciiz "continent"
str13: .asciiz "a"
str14: .asciiz "new"
str15: .asciiz "nation"
str16: .asciiz "conceived"
str17: .asciiz "in"
str18: .asciiz "liberty"
str19: .asciiz "and"
str20: .asciiz "dedicated"
str21: .asciiz "to"
str22: .asciiz "the"
str23: .asciiz "proposition"
str24: .asciiz "that"
str25: .asciiz "all"
str26: .asciiz "men"
str27: .asciiz "are"
str28: .asciiz "created"
str29: .asciiz "equal"
str30: .asciiz "Now"
str31: .asciiz "we"
str32: .asciiz "are"
str33: .asciiz "engaged"
str34: .asciiz "in"
str35: .asciiz "a"
str36: .asciiz "great"
str37: .asciiz "civil"
str38: .asciiz "war"
str39: .asciiz "testing"
str40: .asciiz "whether"
str41: .asciiz "that"
str42: .asciiz "nation"
str43: .asciiz "or"
str44: .asciiz "any"
str45: .asciiz "nation"
str46: .asciiz "so"
str47: .asciiz "conceived"
str48: .asciiz "and"
str49: .asciiz "so"
str50: .asciiz "dedicated"
str51: .asciiz "can"
str52: .asciiz "long"
str53: .asciiz "endure"
str54: .asciiz "We"
str55: .asciiz "are"
str56: .asciiz "met"
str57: .asciiz "on"
str58: .asciiz "a"
str59: .asciiz "great"
str60: .asciiz "battlefield"
str61: .asciiz "of"
str62: .asciiz "that"
str63: .asciiz "war"
str64: .asciiz "We"
str65: .asciiz "have"
str66: .asciiz "come"
str67: .asciiz "to"
str68: .asciiz "dedicate"
str69: .asciiz "a"
str70: .asciiz "portion"
str71: .asciiz "of"
str72: .asciiz "that"
str73: .asciiz "field"
str74: .asciiz "as"
str75: .asciiz "a"
str76: .asciiz "final"
str77: .asciiz "resting"
str78: .asciiz "place"
str79: .asciiz "for"
str80: .asciiz "those"
str81: .asciiz "who"
str82: .asciiz "here"
str83: .asciiz "gave"
str84: .asciiz "their"
str85: .asciiz "lives"
str86: .asciiz "that"
str87: .asciiz "that"
str88: .asciiz "nation"
str89: .asciiz "might"
str90: .asciiz "live"
str91: .asciiz "It"
str92: .asciiz "is"
str93: .asciiz "altogether"
str94: .asciiz "fitting"
str95: .asciiz "and"
str96: .asciiz "proper"
str97: .asciiz "that"
str98: .asciiz "we"
str99: .asciiz "should"
str100: .asciiz "do"
str101: .asciiz "this"
str102: .asciiz "But"
str103: .asciiz "in"
str104: .asciiz "a"
str105: .asciiz "larger"
str106: .asciiz "sense"
str107: .asciiz "we"
str108: .asciiz "cannot"
str109: .asciiz "dedicate"
str110: .asciiz "we"
str111: .asciiz "cannot"
str112: .asciiz "consecrate"
str113: .asciiz "we"
str114: .asciiz "cannot"
str115: .asciiz "hallow"
str116: .asciiz "this"
str117: .asciiz "ground"
str118: .asciiz "The"
str119: .asciiz "brave"
str120: .asciiz "men"
str121: .asciiz "living"
str122: .asciiz "and"
str123: .asciiz "dead"
str124: .asciiz "who"
str125: .asciiz "struggled"
str126: .asciiz "here"
str127: .asciiz "have"
str128: .asciiz "consecrated"
str129: .asciiz "it"
str130: .asciiz "far"
str131: .asciiz "above"
str132: .asciiz "our"
str133: .asciiz "poor"
str134: .asciiz "power"
str135: .asciiz "to"
str136: .asciiz "add"
str137: .asciiz "or"
str138: .asciiz "detract"
str139: .asciiz "The"
str140: .asciiz "world"
str141: .asciiz "will"
str142: .asciiz "little"
str143: .asciiz "note"
str144: .asciiz "nor"
str145: .asciiz "long"
str146: .asciiz "remember"
str147: .asciiz "what"
str148: .asciiz "we"
str149: .asciiz "say"
str150: .asciiz "here"
str151: .asciiz "but"
str152: .asciiz "it"
str153: .asciiz "can"
str154: .asciiz "never"
str155: .asciiz "forget"
str156: .asciiz "what"
str157: .asciiz "they"
str158: .asciiz "did"
str159: .asciiz "here"
str160: .asciiz "It"
str161: .asciiz "is"
str162: .asciiz "for"
str163: .asciiz "us"
str164: .asciiz "the"
str165: .asciiz "living"
str166: .asciiz "rather"
str167: .asciiz "to"
str168: .asciiz "be"
str169: .asciiz "dedicated"
str170: .asciiz "here"
str171: .asciiz "to"
str172: .asciiz "the"
str173: .asciiz "unfinished"
str174: .asciiz "work"
str175: .asciiz "which"
str176: .asciiz "they"
str177: .asciiz "who"
str178: .asciiz "fought"
str179: .asciiz "here"
str180: .asciiz "have"
str181: .asciiz "thus"
str182: .asciiz "far"
str183: .asciiz "so"
str184: .asciiz "nobly"
str185: .asciiz "advanced"
str186: .asciiz "It"
str187: .asciiz "is"
str188: .asciiz "rather"
str189: .asciiz "for"
str190: .asciiz "us"
str191: .asciiz "to"
str192: .asciiz "be"
str193: .asciiz "here"
str194: .asciiz "dedicated"
str195: .asciiz "to"
str196: .asciiz "the"
str197: .asciiz "great"
str198: .asciiz "task"
str199: .asciiz "remaining"
str200: .asciiz "before"
str201: .asciiz "us"
str202: .asciiz "that"
str203: .asciiz "from"
str204: .asciiz "these"
str205: .asciiz "honored"
str206: .asciiz "dead"
str207: .asciiz "we"
str208: .asciiz "take"
str209: .asciiz "increased"
str210: .asciiz "devotion"
str211: .asciiz "to"
str212: .asciiz "that"
str213: .asciiz "cause"
str214: .asciiz "for"
str215: .asciiz "which"
str216: .asciiz "they"
str217: .asciiz "gave"
str218: .asciiz "the"
str219: .asciiz "last"
str220: .asciiz "full"
str221: .asciiz "measure"
str222: .asciiz "of"
str223: .asciiz "devotion"
str224: .asciiz "that"
str225: .asciiz "we"
str226: .asciiz "here"
str227: .asciiz "highly"
str228: .asciiz "resolve"
str229: .asciiz "that"
str230: .asciiz "these"
str231: .asciiz "dead"
str232: .asciiz "shall"
str233: .asciiz "not"
str234: .asciiz "have"
str235: .asciiz "died"
str236: .asciiz "in"
str237: .asciiz "vain"
str238: .asciiz "that"
str239: .asciiz "this"
str240: .asciiz "nation"
str241: .asciiz "under"
str242: .asciiz "God"
str243: .asciiz "shall"
str244: .asciiz "have"
str245: .asciiz "a"
str246: .asciiz "new"
str247: .asciiz "birth"
str248: .asciiz "of"
str249: .asciiz "freedom"
str250: .asciiz "and"
str251: .asciiz "that"
str252: .asciiz "government"
str253: .asciiz "of"
str254: .asciiz "the"
str255: .asciiz "people"
str256: .asciiz "by"
str257: .asciiz "the"
str258: .asciiz "people"
str259: .asciiz "for"
str260: .asciiz "the"
str261: .asciiz "people"
str262: .asciiz "shall"
str263: .asciiz "not"
str264: .asciiz "perish"
str265: .asciiz "from"
str266: .asciiz "the"
str267: .asciiz "earth"
sarray: .word str0
.word str1
.word str2
.word str3
.word str4
.word str5
.word str6
.word str7
.word str8
.word str9
.word str10
.word str11
.word str12
.word str13
.word str14
.word str15
.word str16
.word str17
.word str18
.word str19
.word str20
.word str21
.word str22
.word str23
.word str24
.word str25
.word str26
.word str27
.word str28
.word str29
.word str30
.word str31
.word str32
.word str33
.word str34
.word str35
.word str36
.word str37
.word str38
.word str39
.word str40
.word str41
.word str42
.word str43
.word str44
.word str45
.word str46
.word str47
.word str48
.word str49
.word str50
.word str51
.word str52
.word str53
.word str54
.word str55
.word str56
.word str57
.word str58
.word str59
.word str60
.word str61
.word str62
.word str63
.word str64
.word str65
.word str66
.word str67
.word str68
.word str69
.word str70
.word str71
.word str72
.word str73
.word str74
.word str75
.word str76
.word str77
.word str78
.word str79
.word str80
.word str81
.word str82
.word str83
.word str84
.word str85
.word str86
.word str87
.word str88
.word str89
.word str90
.word str91
.word str92
.word str93
.word str94
.word str95
.word str96
.word str97
.word str98
.word str99
.word str100
.word str101
.word str102
.word str103
.word str104
.word str105
.word str106
.word str107
.word str108
.word str109
.word str110
.word str111
.word str112
.word str113
.word str114
.word str115
.word str116
.word str117
.word str118
.word str119
.word str120
.word str121
.word str122
.word str123
.word str124
.word str125
.word str126
.word str127
.word str128
.word str129
.word str130
.word str131
.word str132
.word str133
.word str134
.word str135
.word str136
.word str137
.word str138
.word str139
.word str140
.word str141
.word str142
.word str143
.word str144
.word str145
.word str146
.word str147
.word str148
.word str149
.word str150
.word str151
.word str152
.word str153
.word str154
.word str155
.word str156
.word str157
.word str158
.word str159
.word str160
.word str161
.word str162
.word str163
.word str164
.word str165
.word str166
.word str167
.word str168
.word str169
.word str170
.word str171
.word str172
.word str173
.word str174
.word str175
.word str176
.word str177
.word str178
.word str179
.word str180
.word str181
.word str182
.word str183
.word str184
.word str185
.word str186
.word str187
.word str188
.word str189
.word str190
.word str191
.word str192
.word str193
.word str194
.word str195
.word str196
.word str197
.word str198
.word str199
.word str200
.word str201
.word str202
.word str203
.word str204
.word str205
.word str206
.word str207
.word str208
.word str209
.word str210
.word str211
.word str212
.word str213
.word str214
.word str215
.word str216
.word str217
.word str218
.word str219
.word str220
.word str221
.word str222
.word str223
.word str224
.word str225
.word str226
.word str227
.word str228
.word str229
.word str230
.word str231
.word str232
.word str233
.word str234
.word str235
.word str236
.word str237
.word str238
.word str239
.word str240
.word str241
.word str242
.word str243
.word str244
.word str245
.word str246
.word str247
.word str248
.word str249
.word str250
.word str251
.word str252
.word str253
.word str254
.word str255
.word str256
.word str257
.word str258
.word str259
.word str260
.word str261
.word str262
.word str263
.word str264
.word str265
.word str266
.word str267
.text
main:
la $a0, sarray #load array for sorting
li $a1, 0 #load initial left bound
li $a2, 267 #load right bound
jal quicksort #do sort
la $a0, sarray #load array to print
li $a1, 268 # set number of items to print
jal printArray # do printing!
#print file stuff
jal getDictFile
la $a0, fileName
jal zeroNewLine #ZERO THE NEWLINE AT END OF FILENAME
#open the dict file
jal openDict
#read the dict file
jal readDict
# print the dictionary
jal printEntries
li $v0, 10
syscall #exit program
li $v0, 10 #return from main
syscall #exit
quicksort:
#save all registers needed
addi $sp, $sp, -44
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
sw $a2, 12($sp)
sw $s0, 16($sp)
sw $s1, 20($sp)
sw $s2, 24($sp)
sw $s3, 28($sp)
sw $s4, 32($sp)
sw $s5, 36($sp)
sw $s6, 40($sp)
setup:
#function to do the sort
addi $s0, $a1, 0 # set t0 to == left (i)
addi $s1, $a2, 0 #set t1 to == right (j)
addi $s5, $s0, 0 # original left (i) will not change unless function recalled.
addi $s6, $s1, 0 # original right(j) will not change unless function recalled.
addi $s3, $a0, 0 #save array
getPivot:
#function to setup variables and to set the pivot
add $s2, $s1, $s0 # add i + j
srl $s2, $s2, 1 #divide by 2
sll $s1, $s1, 2 #mult left by 4 to get word size
sll $s0, $s0, 2 #mult left by 4 to get word size
sll $s2, $s2, 2 #mult left by 4 to get word size
add $s2, $s2, $a0 #set pivot address
lw $a1, ($s2) #a1 = array[pivot]
iLoop:
#function to do comparison of i
add $t0, $s3, $s0 #$t0 = array + i
lw $a0, ($t0) #a0 = arrray[i]
jal strcmp #do string cmp
bltz $v0, incrementI # if less than zero, increment i
j jLoop # compareLoop2 #if result >= 0, go to next loop
incrementI:
#function to increment i
addi $s0, $s0, 4 #else, increment i++
j iLoop # re-loop
jLoop:
#function to do comparison of j
add $t0, $s3, $s1 #$t0 = array + j
lw $a0, ($t0) #a0 = arrray[j]
jal strcmp #do string cmp
bgtz $v0, decrementJ #greater than 0, decrement j
j checkSwapValues #next go to swap values section
decrementJ:
#function to decrement value j
addi $s1, $s1, -4 #else, decrement j--
j jLoop #re-loop
checkSwapValues:
# check to see if we need to swap values or not
ble $s0, $s1, swapValues #if i less than j, swap
j reloopCheck # go check if we need re-looping
swapValues:
# funciton to swap array values
# get value of array[i]
add $t0, $s0, $s3 # $t0 = array[i]
lw $t1, ($t0) # temp = array[i]
# get value of array[j]
add $t2, $s1, $s3 #get address of array[j
lw $t3, ($t2) # temp2 = array[j]
# put array[j] into array[i]
sw $t3, ($t0) # array[i] = array[j]
#put array[i] value into array[j]
sw $t1, ($t2) # array[j] = temp
addi $s0, $s0, 4 #i++
addi $s1, $s1, -4 #j--
reloopCheck:
ble $s0, $s1, iLoop #if i less than or equal to j, reloop
partition:
#setup inital checks for partitioning.
la $a0, ($s3) #load array
addi $a1, $s5, 0 #set left bound
bltz $s1, jLessThan0 #check if j is less than 0
addi $a2, $s1, 0 #load j
srl $a2, $a2, 2 #divide by 4 to get base 10 number for index.
partitionFirstHalf:
#first half sorting
bge $a1, $a2, teardown #left >= j
jal quicksort # recursive call to sort left part
la $a0, ($s3) # load up array again
addi $a1, $s0, 0 # set i variable
srl $a1, $a1, 2 #divide by 4 to get base 10 number for index.
addi $a2, $s6, 0 #set right variable
# now do right side of pivot
bge $a1, $a2, teardown #i >= right
jal quicksort #recurse
j teardown #unwind the stack
jLessThan0:
#if j less than 0, set to 0 instead of shifting
addi $a2, $zero, 0
j partitionFirstHalf #go to first partitioin step
teardown:
#function to restore registers and return
lw $ra, 0($sp)
lw $a0, 4($sp)
lw $a1, 8($sp)
lw $a2, 12($sp)
lw $s0, 16($sp)
lw $s1, 20($sp)
lw $s2, 24($sp)
lw $s3, 28($sp)
lw $s4, 32($sp)
lw $s5, 36($sp)
lw $s6, 40($sp)
addi $sp, $sp, 44
jr $ra #return from func
######################## STRING COMPARE FUNCTION
strcmp:
#save values on stack
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
#set initial values
addi $t0, $a0, 0 # t0 is array1 of char*
addi $t1, $a1, 0 #t1 is array2 of char*
body:
#loop over all char's in strings.
lb $t2, ($t0) #load next byte from first string
lb $t3, ($t1) #load next byte from second string
sub $v0, $t2, $t3 #subtract to see how they compare
bne $t3, $t2, exitStrComp #if they aren't equal chars, exit
beqz $t2, exitStrComp #if char1 == 0, exit
beqz $t3, exitStrComp #if char2 == 0, exit
addi $t0, $t0, 1 #increment string1 char
addi $t1, $t1, 1 #increment string2 char
j body # re-loop
exitStrComp:
#unwind the stack
lw $ra, 0($sp)
lw $a0, 4($sp)
lw $a1, 8($sp)
addi $sp, $sp, 12
jr $ra # return from function
####################### END STRING COMPARE
printArray:
# save registers on stack
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $a0, 4($sp)
sw $a1, 8($sp)
addi $t0, $a0, 0 #array
addi $t1, $a1, 0 #count of items
sll $t1, $t1, 2 # multiply by 4 for easy incrementing
li $t2, 0 #index counter
li $v0, 4 # print register
printloop:
lw $a0, ($t0) #load item at index
syscall #print item
la $a0, newline #load comma
syscall #print comma
addi $t2, $t2, 4 #increment index
addi $t0, $t0, 4 #increment counter
bne $t2, $t1, printloop #if we havent printed all, continue
exitLoop:
#unwind the stack.
lw $ra, 0($sp)
lw $a0, 4($sp)
lw $a1, 8($sp)
addi $sp, $sp, 12
jr $ra
####################### READFILE STUFF
.data
#prgram limits
strLimit: .word 200 #max string to store in table
strMax: .word 16 #max length of string
wordCount: .word -1 # count of words read from dict.
fileName: .space 80 #dict file name
fileDesc: .word -2 #dict fileDescriptor
strTable: .space 200 # space for strLimit table entries
strArea: .space 10000 # space for limit*strMax strings
strError: .asciiz "Mi spiace. Questo non funzione."
.data
filePrompt: .asciiz "Enter filename: "
openFileError: .asciiz "Error opening file.\n"
readErrorMsg: .asciiz "Error reading from file.\n"
countMsg: .asciiz "Counted: "
.text
getDictFile: la $a0, filePrompt
li $v0, 4
syscall
li $v0, 8
la $a0, fileName
la $a1, strMax
syscall
jr $ra
openDict:
la $a0, fileName
move $a1, $zero #read
move $a2, $zero # open
li $v0, 13
syscall
sw $v0, fileDesc($zero)
slt $t0, $v0, $zero
beq $t0, $zero, fileOK
la $a0, openFileError
li $v0, 4
syscall
li $v0, 10
syscall
fileOK: jr $ra
zeroNewLine: move $t0, $a0
zLoop:
lb $t1, 0($t0)
beq $t1, $zero, zDone
subi $t3, $t1, '\n'
beq $t3, $zero, gotNl
addi $t0, $t0, 1
j zLoop
gotNl: sb $zero, 0($t0)
zDone: jr $ra
readDict: addi $sp, $sp, -16
sw $ra 0($sp)
sw $a0 4($sp)
sw $a1 8($sp)
sw $a2 12($sp)
#setup loop variables
la $t0, fileDesc
lw $a0, 0($t0)
la $a1, strArea
li $a2, 1
move $t0, $zero
la $t1, strTable
sw $a1, 0($t1)
rdLoop: li $v0, 14
syscall
beq $v0, $zero, endOfFile
slt $t2, $v0, $zero
bne $t2, $zero, rdError
lb $t2, 0($a2)
subi $t3, $t2, ' '
beq $t3, $zero, rdLoop
subi $t3, $t2, '\n'
beq $t3, $zero, endWord
addi $a1, $a1, 1
j rdLoop
endWord: sb $zero, 0($a1)
addi $a1, $a1, 1
addi $t0, $t0, 1
addi $t1, $t1, 4
sw $a1, 0($t1)
j rdLoop
rdError: la $a0, readErrorMsg
li $v0, 4
syscall
li $v0, 10
syscall
openError: la $a0, openFileError
li $v0, 4
syscall
li $v0, 10
syscall
endOfFile:
la $a0, countMsg
li $v0, 4
syscall
la $t4, wordCount
sw $t0, 0($t4)
move $a0, $t0
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
lw $ra 0($sp)
lw $a0 4($sp)
lw $a1 8($sp)
lw $a2 12($sp)
addi $sp, $sp, 16
jr $ra
printEntries:
la $a0, strError
li $v0, 4
syscall
jr $ra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment