Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. .data
  2.  
  3. toUpper: .byte 1
  4.  
  5. toLower: .byte 1
  6.  
  7. string:
  8. .asciiz " A long time ago in a Galaxy far, far away...."
  9.  
  10. # Your code goes below this line
  11.  
  12. ogString: # Set up all strings that may
  13. .asciiz "Original string:" # be printed in the program
  14.  
  15. ucString:
  16. .asciiz "Converted to upper-case:"
  17.  
  18. lcString:
  19. .asciiz "Converted to lower-case:"
  20.  
  21. fString:
  22. .asciiz "Forward:"
  23.  
  24. bString:
  25. .asciiz "Backward:"
  26.  
  27. counter: .byte 0
  28.  
  29. newline:
  30. .asciiz "\n"
  31.  
  32. .text
  33.  
  34. main: # Function prologue -- even main has one
  35.  
  36. addiu $sp, $sp, -24 # allocate stack space -- default of 24 here
  37. sw $fp, 0($sp) # save caller's frame pointer
  38. sw $ra, 4($sp) # save return address
  39. addiu $fp, $sp, 20 # setup main's frame pointer
  40.  
  41. la $a0, ogString # print out "Original String:"
  42. addi $v0, $zero, 4
  43. syscall
  44.  
  45. la $a0, newline # print a newline
  46. syscall
  47.  
  48. la $a0, string # print out the string
  49. syscall
  50.  
  51. la $a0, newline # print a newline
  52. syscall
  53.  
  54. la $a0, newline # print a newline
  55. syscall
  56.  
  57. la $s0, counter # initialize counter into $t3
  58. lb $t3, 0($s0)
  59.  
  60. ###############################################################
  61.  
  62. upperConversion: # begin conversion to upper case
  63.  
  64. la $s0, toUpper
  65. lb $t1, 0($s0)
  66.  
  67. beq $zero, $t1, lowerConversion # skip if toUpper is 0
  68.  
  69. la $a0, ucString # print out "converting to uppercase"
  70. syscall
  71.  
  72. la $a0, newline # print a newline
  73. syscall
  74.  
  75. la $s0, string # load the address of string into $s0
  76.  
  77. upperLoop:
  78.  
  79. # $t0 is the current character
  80. # $s0 is the string address
  81. # $t3 is the array counter
  82.  
  83. lb $t0, 0($s0) # load a byte from the array into $t0
  84.  
  85. beq $zero, $t0, upperLoopEnd # skip to lowerConversion if we
  86. # reach the null character
  87.  
  88. addi $t1, $zero, 'z'
  89. addi $t1, $t1, 1
  90.  
  91. slt $t1, $t0, $t1 # if current character is less than 123
  92. beq $t1, 1, lessThan123 # advance to the next part, otherwise
  93. j upperLoopIncrement # it isn't a lowercase letter, so it
  94. # can't be capitalized, move on to the
  95. # next character in the string
  96.  
  97. lessThan123:
  98. slt $t2, $t0, 'a' # if current character is greater
  99. bne $t2, 1, capitalize # than or equal to 97, it is already
  100. j upperLoopIncrement # less than 123, so it must be a
  101. # lowercase letter, so capitalize it.
  102. # Otherwise, move on to the next char
  103.  
  104. capitalize:
  105. addi $t0, $t0, -32 # subtract 32 from the lowercase
  106. sb $t0, 0($s0) # character to get to its'
  107. # uppercase version, then store
  108. # it back into $t0.
  109. j upperLoopIncrement
  110.  
  111. upperLoopIncrement:
  112.  
  113. addi $s0, $s0, 1 # increment $s0 to point to the next element
  114. addi $t3, $t3, 1 # increment counter in $t3
  115. j upperLoop
  116.  
  117. upperLoopEnd:
  118.  
  119. la $a0, fString # print "Forward:"
  120. syscall
  121.  
  122. la $a0, newline # print a newline
  123. syscall
  124.  
  125. la $a0, string # print the new all uppercase string
  126. syscall
  127.  
  128. addi $v0, $zero, 4
  129.  
  130. la $a0, newline # print a newline
  131. syscall
  132.  
  133. la $a0, bString # print "Backward:"
  134. syscall
  135.  
  136. la $a0, newline # print a newline
  137. syscall
  138.  
  139. #addi $s0, $s0, -1 # decrement $s0 to account for null char
  140.  
  141. backWriter:
  142.  
  143. beq $zero, $t3, backWriterEnd #end once $t0 is 0
  144.  
  145. lb $a0, 0($t3)
  146. addi $v0, $zero, 11 # print the character in question
  147. syscall
  148.  
  149. addi $t3, $t3, -1 # decrement $s0 to point to the prev. element
  150.  
  151. j backWriter
  152.  
  153. backWriterEnd:
  154.  
  155. la $a0, string # print the string backwards
  156. syscall
  157.  
  158. la $a0, newline # print a newline
  159. syscall
  160.  
  161. la $a0, newline # print a newline
  162. syscall
  163.  
  164. ###############################################################
  165.  
  166. lowerConversion: # begin conversion to lower case
  167.  
  168. la $s0, toLower
  169. lb $t1, 0($s0)
  170.  
  171. beq $zero, $t1, done # skip if toLower is 0
  172.  
  173. la $a0, lcString # print out "converting to lowercase"
  174. syscall
  175.  
  176. la $a0, newline # print a newline
  177. syscall
  178.  
  179. lowerLoop:
  180.  
  181. # $t0 is the current character
  182. # $s0 is the string address
  183.  
  184. lb $t0, 0($s0) # load a byte from the array into $t0
  185.  
  186. beq $zero, $t0, lowerLoopEnd # skip to lowerConversion if we
  187. # reach the null character
  188.  
  189. addi $t1, $zero, 'Z'
  190. addi $t1, $t1, 1
  191.  
  192. slt $t1, $t0, $t1 # if current character is less than 123
  193. beq $t1, 1, lessThan90 # advance to the next part, otherwise
  194. j lowerLoopIncrement # it isn't a lowercase letter, so it
  195. # can't be capitalized, move on to the
  196. # next character in the string
  197.  
  198. lessThan90:
  199. slt $t2, $t0, 'A' # if current character is greater
  200. bne $t2, 1, decapitalize # than or equal to 97, it is already
  201. j lowerLoopIncrement # less than 123, so it must be a
  202. # lowercase letter, so capitalize it.
  203. # Otherwise, move on to the next char
  204.  
  205. decapitalize:
  206. addi $t0, $t0, 32 # add 32 from the uppercase
  207. sb $t0, 0($s0) # character to get to its'
  208. # uppercase version, then store
  209. # it back into $t0.
  210. j lowerLoopIncrement
  211.  
  212. lowerLoopIncrement:
  213.  
  214. addi $s0, $s0, 1 # increment $s0 to point to the next element
  215.  
  216. j lowerLoop
  217.  
  218. lowerLoopEnd:
  219.  
  220. la $a0, fString # print "Forward:"
  221. syscall
  222.  
  223. la $a0, newline # print a newline
  224. syscall
  225.  
  226. la $a0, string # print the new all uppercase string
  227. syscall
  228.  
  229. addi $v0, $zero, 4
  230.  
  231. la $a0, newline # print a newline
  232. syscall
  233.  
  234. la $a0, bString # print "Backward:"
  235. syscall
  236.  
  237. la $a0, newline # print a newline
  238. syscall
  239.  
  240. # BACKWRITER 2 HERE
  241.  
  242. la $a0, string # print the string backwards
  243. syscall
  244.  
  245. la $a0, newline # print a newline
  246. syscall
  247.  
  248. la $a0, newline # print a newline
  249. syscall
  250.  
  251. done: # Epilogue for main -- restore stack & frame pointers and return
  252. lw $ra, 4($sp) # get return address from stack
  253. lw $fp, 0($sp) # restore the caller's frame pointer
  254. addiu $sp, $sp, 24 # restore the caller's stack pointer
  255. jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement