Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. # A peusudo code:
  2. # def count_abc(s):
  3. # longest_sequence = 0
  4. # current_sequence = 0
  5. # i = 0
  6. # flag = False
  7. # while i < (len(s) - 1):
  8. # if is_letter(s[i]) and is_letter(s[i + 1]) and (ord(s[i + 1]) - ord(s[i]) == 1):
  9. # flag = True
  10. # current_sequence += 1
  11. # if current_sequence > longest_sequence:
  12. # longest_sequence = current_sequence
  13. # else:
  14. # current_sequence = 0
  15. # i+=1
  16. # if flag:
  17. # longest_sequence += 1
  18. # return longest_sequence
  19.  
  20.  
  21. # B peusudo code:
  22. # def count_char(s, ch):
  23. # c = 0
  24. # i = 0
  25. # while i < len(s):
  26. # if s[i] == ch:
  27. # c += 1
  28. # i += 1
  29. # return c
  30.  
  31.  
  32. # def delete(s, num):
  33. # l = len(s)
  34. # if num >= l:
  35. # return
  36. # i = num
  37. # while i < len(s):
  38. # j = i
  39. # while j < len(s) - 1:
  40. # s[j] = s[j+1]
  41. # j += 1
  42. # del s[j]
  43. # i += num - 1
  44.  
  45. ############### Data segment ###############
  46. .data
  47. userInput: .space 32
  48. newLine: .asciiz "\n"
  49. ######## A constants ########
  50. stringInputMessage: .asciiz "Please enter a string (up to 32 chars): "
  51. longestSequenceMessage: .asciiz "The longest sequence is: "
  52.  
  53. ######## B constants ########
  54. charInputMessage: .asciiz "Please enter char to count: "
  55. charCountMessage: .asciiz "The number of occurences of the char is: "
  56.  
  57. ######## C constants ########
  58. numberInputMessage: .asciiz "Please enter a number between 1-9: "
  59.  
  60. ############### Code segment ###############
  61. .text
  62. .globl main
  63. main:
  64. jal printStringInputMessage
  65. la $a0, userInput
  66. li $a1, 32
  67. jal readStringInput
  68. ############### A ###############
  69. # jal countAbc
  70. # move $a0, $v0
  71. # jal printCountAbcOutput
  72. # jal printNewLine
  73. ############### B ###############
  74. # jal printCharInputMessage
  75. # jal readChar
  76. # move $a1, $v0
  77. # jal printNewLine
  78. # la $a0, userInput
  79. # jal countChar
  80. # move $a0, $v0
  81. # jal printCountCharOutput
  82. ############### C ###############
  83. jal printNewLine
  84. jal printNumberInputMessage
  85. jal readNumber
  86. la $a0, userInput
  87. move $a1, $v0
  88. jal delete
  89. j exit
  90. exit:
  91. li $v0, 10
  92. syscall
  93.  
  94. delete:
  95. move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
  96. move $s3, $a0 # save base address for jumping perposes
  97. move $s0, $a0 # address is in s0
  98. jal removeNewLineFromInputString
  99. move $s1, $a1 # number is in s1
  100. move $a0, $s3 # strlen on original base address (moved in removeNewLine)
  101. jal strlen
  102. move $t0, $v0 # l = length - 1 (without \n)
  103. addi $t0, $t0, 1 # l = length
  104. bge $s1, $t0, finishDelete
  105. move $s2, $s1 # first while index (i = num)
  106. firstWhile:
  107. move $a0, $s3
  108. jal strlen
  109. move $t3, $v0 # t3 = len(s) - 1 (without \n)
  110. addi $t3, $t3, 1 # t3 = len(s)
  111. bge $s2, $t3, finishDelete
  112. move $t4, $s2 # j = i
  113. addi $t3, $t3, -1 # len(s) - 1
  114. secondWhile:
  115. bge $t4, $t3, endSecondWhile # while j < len(s) -1
  116. move $s0, $s3
  117. add $s0, $s0, $t4 # move address to j
  118. addi $s0, $s0, 1 # move address to j + 1
  119. lbu $t7, 0($s0) # load s[j+1]
  120. sb $t7, -1($s0) # store to s[j]
  121. addi $t4, $t4, 1 # j++
  122. j secondWhile
  123.  
  124. endSecondWhile:
  125. move $s0, $s3
  126. add $s0, $s0, $t4 # move address to j
  127. sb $zero, 0($s0) # len(s) = 2 go back 1
  128. move $s0, $s3 # return to base address
  129. add $s2, $s2, $s1 # i = i + num
  130. addi $s2, $s2, -1 # i = i - 1
  131. j firstWhile
  132.  
  133. finishDelete:
  134. move $a0, $s3
  135. move $s0, $s3
  136. jal strlen
  137. move $t1, $v0
  138. addi $t1, $t1, 1
  139. li $t0, 0
  140. printLoop:
  141. bge $t0, $t1, endPrintLoop
  142. lbu $a0, 0($s0)
  143. addi $t0, $t0, 1
  144. addi $s0, $s0, 1
  145. li $v0, 11
  146. syscall
  147. j printLoop
  148. endPrintLoop:
  149. jr $s7
  150.  
  151. countChar:
  152. move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
  153. move $s0, $a0 # s0 will be copy of string address
  154. move $s1, $a1 # The char to count will be in s1
  155. jal strlen
  156. move $t0, $v0 # length
  157. li $s2, 0 # s2 will hold the number of occurences
  158. li $t1, 0 # loop index
  159.  
  160. loop:
  161. bge $t1, $t0, endLoop # while i < len(s)
  162. addi $t1, $t1, 1 # i++
  163. lbu $t4, 0($s0) # t4 = s[j]
  164. addi $s0, $s0, 1 # j++
  165. beq $s1, $t4, increaseNumberOfOccurences # if ch == s[j]: numberOfOccurences++
  166. j loop
  167. endLoop:
  168. nop
  169. move $v0, $s2 # Return the number of occurences
  170. jr $s7
  171.  
  172. increaseNumberOfOccurences:
  173. addi $s2, $s2, 1
  174. j loop
  175.  
  176. countAbc:
  177. move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
  178. li $s0, 0 # s0 will hold the longest sequence (and will be the output)
  179. li $s1, 0 # s1 will hold the current sequence
  180. li $s2, 0 # A 'boolean' flag that indicates if there's a sequrnce in order to increase longest by 1
  181. move $t5, $a0 # s2 will be a copy of the userInput
  182. move $a0, $t5
  183. jal strlen
  184. move $t4, $v0
  185. addi $t4, $t4, -1 # t4 = strlen(s) - 1
  186. li $s3, 0 # loop index
  187. while:
  188. bge $s3, $t4, endWhile
  189. addi $s3, $s3, 1 # i++
  190. lbu $a0, 0($t5)
  191. addi $t5, $t5, 1
  192. move $s4, $a0 # save the first letter for sequence comparison
  193. jal isLetter # s[i] is a letter
  194. beqz $v0, noSequence
  195.  
  196. lbu $a0, 0($t5)
  197. move $s5, $a0 # save the second letter for sequence comparison
  198. jal isLetter # s[i+1] is a letter
  199. beqz $v0, noSequence
  200. # We do have 2 letters! Now check if there's a sequence
  201. sub $t3, $s5, $s4 # s[i+1] - s[i]
  202. bne $t3, 1, noSequence # Last sequence check!
  203. j sequence # We do have a seqence!
  204. endWhile:
  205. nop
  206. move $a0, $s2
  207. jal shouldIncrementByOne
  208. move $v0, $s0 # Longest sequence as a return value
  209. jr $s7
  210.  
  211. printStringInputMessage:
  212. li $v0, 4
  213. la $a0, stringInputMessage
  214. syscall
  215. jr $ra
  216.  
  217. printCharInputMessage:
  218. li $v0, 4
  219. la $a0, charInputMessage
  220. syscall
  221. jr $ra
  222.  
  223. printNumberInputMessage:
  224. li $v0, 4
  225. la $a0, numberInputMessage
  226. syscall
  227. jr $ra
  228.  
  229. printCountAbcOutput:
  230. move $t0, $a0
  231. li $v0, 4
  232. la $a0, longestSequenceMessage
  233. syscall
  234. li $v0, 1
  235. move $a0, $t0
  236. syscall
  237. jr $ra
  238.  
  239. printCountCharOutput:
  240. move $t0, $a0
  241. li $v0, 4
  242. la $a0, charCountMessage
  243. syscall
  244. li $v0, 1
  245. move $a0, $t0
  246. syscall
  247. jr $ra
  248.  
  249.  
  250. printNewLine:
  251. li $v0, 4
  252. la $a0, newLine
  253. syscall
  254. jr $ra
  255.  
  256. sequence:
  257. li $s2, 1 # We found a sequence, so the flag is `true`
  258. addi $s1, $s1, 1 # Incerement current sequence by 1.
  259. bgt $s1, $s0, updateLongestSequence
  260. j while
  261.  
  262. updateLongestSequence:
  263. move $s0, $s1
  264. j while
  265.  
  266. noSequence:
  267. li $s1, 0 # current_sequence = 0
  268. j while
  269.  
  270. shouldIncrementByOne:
  271. beq $a0, 1, incrementLongestSequenceByOne
  272. jr $ra
  273.  
  274. incrementLongestSequenceByOne:
  275. addi $s0, $s0, 1
  276. jr $ra
  277.  
  278. isLetter:
  279. sge $t0, $a0, 'a'
  280. sle $t1, $a0, 'z'
  281. and $v0, $t0, $t1
  282. jr $ra
  283.  
  284. strlen:
  285. li $t0, 0
  286. move $t1, $a0
  287. L:
  288. lbu $t2, 0($t1)
  289. beqz $t2, finish
  290. addi $t1, $t1, 1
  291. addi $t0, $t0, 1
  292. j L
  293. finish:
  294. addi $t0, $t0, -1 # Remove null-terminator
  295. move $v0, $t0
  296. jr $ra
  297.  
  298. checkNoSequence:
  299. beqz $a0, noSequence
  300. beq $a0, 1, noSequence
  301. jr $ra
  302.  
  303. readStringInput:
  304. li $v0, 8
  305. syscall
  306. jr $ra # The number will be in $v0 (return value)
  307.  
  308. readChar:
  309. li $v0, 12
  310. syscall
  311. jr $ra
  312.  
  313. readNumber:
  314. li $v0, 5
  315. syscall
  316. jr $ra # The number will be in $v0 (return value)
  317.  
  318. removeNewLineFromInputString:
  319. move $s0, $a0
  320. removeNewLineLoop:
  321. lbu $t1, 0($s0)
  322. beq $t1, '\n', finishRemoveNewLine
  323. addi $s0, $s0, 1
  324. j removeNewLineLoop
  325. finishRemoveNewLine:
  326. sb $zero, 0($s0)
  327. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement