Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A peusudo code:
- # def count_abc(s):
- # longest_sequence = 0
- # current_sequence = 0
- # i = 0
- # flag = False
- # while i < (len(s) - 1):
- # if is_letter(s[i]) and is_letter(s[i + 1]) and (ord(s[i + 1]) - ord(s[i]) == 1):
- # flag = True
- # current_sequence += 1
- # if current_sequence > longest_sequence:
- # longest_sequence = current_sequence
- # else:
- # current_sequence = 0
- # i+=1
- # if flag:
- # longest_sequence += 1
- # return longest_sequence
- # B peusudo code:
- # def count_char(s, ch):
- # c = 0
- # i = 0
- # while i < len(s):
- # if s[i] == ch:
- # c += 1
- # i += 1
- # return c
- # def delete(s, num):
- # l = len(s)
- # if num >= l:
- # return
- # i = num
- # while i < len(s):
- # j = i
- # while j < len(s) - 1:
- # s[j] = s[j+1]
- # j += 1
- # del s[j]
- # i += num - 1
- ############### Data segment ###############
- .data
- userInput: .space 32
- newLine: .asciiz "\n"
- ######## A constants ########
- stringInputMessage: .asciiz "Please enter a string (up to 32 chars): "
- longestSequenceMessage: .asciiz "The longest sequence is: "
- ######## B constants ########
- charInputMessage: .asciiz "Please enter char to count: "
- charCountMessage: .asciiz "The number of occurences of the char is: "
- ######## C constants ########
- numberInputMessage: .asciiz "Please enter a number between 1-9: "
- ############### Code segment ###############
- .text
- .globl main
- main:
- jal printStringInputMessage
- la $a0, userInput
- li $a1, 32
- jal readStringInput
- ############### A ###############
- # jal countAbc
- # move $a0, $v0
- # jal printCountAbcOutput
- # jal printNewLine
- ############### B ###############
- # jal printCharInputMessage
- # jal readChar
- # move $a1, $v0
- # jal printNewLine
- # la $a0, userInput
- # jal countChar
- # move $a0, $v0
- # jal printCountCharOutput
- ############### C ###############
- jal printNewLine
- jal printNumberInputMessage
- jal readNumber
- la $a0, userInput
- move $a1, $v0
- jal delete
- j exit
- exit:
- li $v0, 10
- syscall
- delete:
- move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
- move $s3, $a0 # save base address for jumping perposes
- move $s0, $a0 # address is in s0
- jal removeNewLineFromInputString
- move $s1, $a1 # number is in s1
- move $a0, $s3 # strlen on original base address (moved in removeNewLine)
- jal strlen
- move $t0, $v0 # l = length - 1 (without \n)
- addi $t0, $t0, 1 # l = length
- bge $s1, $t0, finishDelete
- move $s2, $s1 # first while index (i = num)
- firstWhile:
- move $a0, $s3
- jal strlen
- move $t3, $v0 # t3 = len(s) - 1 (without \n)
- addi $t3, $t3, 1 # t3 = len(s)
- bge $s2, $t3, finishDelete
- move $t4, $s2 # j = i
- addi $t3, $t3, -1 # len(s) - 1
- secondWhile:
- bge $t4, $t3, endSecondWhile # while j < len(s) -1
- move $s0, $s3
- add $s0, $s0, $t4 # move address to j
- addi $s0, $s0, 1 # move address to j + 1
- lbu $t7, 0($s0) # load s[j+1]
- sb $t7, -1($s0) # store to s[j]
- addi $t4, $t4, 1 # j++
- j secondWhile
- endSecondWhile:
- move $s0, $s3
- add $s0, $s0, $t4 # move address to j
- sb $zero, 0($s0) # len(s) = 2 go back 1
- move $s0, $s3 # return to base address
- add $s2, $s2, $s1 # i = i + num
- addi $s2, $s2, -1 # i = i - 1
- j firstWhile
- finishDelete:
- move $a0, $s3
- move $s0, $s3
- jal strlen
- move $t1, $v0
- addi $t1, $t1, 1
- li $t0, 0
- printLoop:
- bge $t0, $t1, endPrintLoop
- lbu $a0, 0($s0)
- addi $t0, $t0, 1
- addi $s0, $s0, 1
- li $v0, 11
- syscall
- j printLoop
- endPrintLoop:
- jr $s7
- countChar:
- move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
- move $s0, $a0 # s0 will be copy of string address
- move $s1, $a1 # The char to count will be in s1
- jal strlen
- move $t0, $v0 # length
- li $s2, 0 # s2 will hold the number of occurences
- li $t1, 0 # loop index
- loop:
- bge $t1, $t0, endLoop # while i < len(s)
- addi $t1, $t1, 1 # i++
- lbu $t4, 0($s0) # t4 = s[j]
- addi $s0, $s0, 1 # j++
- beq $s1, $t4, increaseNumberOfOccurences # if ch == s[j]: numberOfOccurences++
- j loop
- endLoop:
- nop
- move $v0, $s2 # Return the number of occurences
- jr $s7
- increaseNumberOfOccurences:
- addi $s2, $s2, 1
- j loop
- countAbc:
- move $s7, $ra # ra will be overriden by other jal calls so keep it (instead using a stack)
- li $s0, 0 # s0 will hold the longest sequence (and will be the output)
- li $s1, 0 # s1 will hold the current sequence
- li $s2, 0 # A 'boolean' flag that indicates if there's a sequrnce in order to increase longest by 1
- move $t5, $a0 # s2 will be a copy of the userInput
- move $a0, $t5
- jal strlen
- move $t4, $v0
- addi $t4, $t4, -1 # t4 = strlen(s) - 1
- li $s3, 0 # loop index
- while:
- bge $s3, $t4, endWhile
- addi $s3, $s3, 1 # i++
- lbu $a0, 0($t5)
- addi $t5, $t5, 1
- move $s4, $a0 # save the first letter for sequence comparison
- jal isLetter # s[i] is a letter
- beqz $v0, noSequence
- lbu $a0, 0($t5)
- move $s5, $a0 # save the second letter for sequence comparison
- jal isLetter # s[i+1] is a letter
- beqz $v0, noSequence
- # We do have 2 letters! Now check if there's a sequence
- sub $t3, $s5, $s4 # s[i+1] - s[i]
- bne $t3, 1, noSequence # Last sequence check!
- j sequence # We do have a seqence!
- endWhile:
- nop
- move $a0, $s2
- jal shouldIncrementByOne
- move $v0, $s0 # Longest sequence as a return value
- jr $s7
- printStringInputMessage:
- li $v0, 4
- la $a0, stringInputMessage
- syscall
- jr $ra
- printCharInputMessage:
- li $v0, 4
- la $a0, charInputMessage
- syscall
- jr $ra
- printNumberInputMessage:
- li $v0, 4
- la $a0, numberInputMessage
- syscall
- jr $ra
- printCountAbcOutput:
- move $t0, $a0
- li $v0, 4
- la $a0, longestSequenceMessage
- syscall
- li $v0, 1
- move $a0, $t0
- syscall
- jr $ra
- printCountCharOutput:
- move $t0, $a0
- li $v0, 4
- la $a0, charCountMessage
- syscall
- li $v0, 1
- move $a0, $t0
- syscall
- jr $ra
- printNewLine:
- li $v0, 4
- la $a0, newLine
- syscall
- jr $ra
- sequence:
- li $s2, 1 # We found a sequence, so the flag is `true`
- addi $s1, $s1, 1 # Incerement current sequence by 1.
- bgt $s1, $s0, updateLongestSequence
- j while
- updateLongestSequence:
- move $s0, $s1
- j while
- noSequence:
- li $s1, 0 # current_sequence = 0
- j while
- shouldIncrementByOne:
- beq $a0, 1, incrementLongestSequenceByOne
- jr $ra
- incrementLongestSequenceByOne:
- addi $s0, $s0, 1
- jr $ra
- isLetter:
- sge $t0, $a0, 'a'
- sle $t1, $a0, 'z'
- and $v0, $t0, $t1
- jr $ra
- strlen:
- li $t0, 0
- move $t1, $a0
- L:
- lbu $t2, 0($t1)
- beqz $t2, finish
- addi $t1, $t1, 1
- addi $t0, $t0, 1
- j L
- finish:
- addi $t0, $t0, -1 # Remove null-terminator
- move $v0, $t0
- jr $ra
- checkNoSequence:
- beqz $a0, noSequence
- beq $a0, 1, noSequence
- jr $ra
- readStringInput:
- li $v0, 8
- syscall
- jr $ra # The number will be in $v0 (return value)
- readChar:
- li $v0, 12
- syscall
- jr $ra
- readNumber:
- li $v0, 5
- syscall
- jr $ra # The number will be in $v0 (return value)
- removeNewLineFromInputString:
- move $s0, $a0
- removeNewLineLoop:
- lbu $t1, 0($s0)
- beq $t1, '\n', finishRemoveNewLine
- addi $s0, $s0, 1
- j removeNewLineLoop
- finishRemoveNewLine:
- sb $zero, 0($s0)
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement