Skip to content

Instantly share code, notes, and snippets.

@Arham4
Created February 9, 2020 20:09
Show Gist options
  • Save Arham4/76611f496843867035f2eb9ac1af1827 to your computer and use it in GitHub Desktop.
Save Arham4/76611f496843867035f2eb9ac1af1827 to your computer and use it in GitHub Desktop.
SE 3340: Computer Architecture - Homework 3
.data
prompt: .asciiz "Input some text dude"
charactersMessage: .asciiz "Characters: "
wordsMessage: .asciiz "Words: "
input: .space 100
newLine: .asciiz "\n"
.text
# prompts for the text to analyze
li $v0, 54
la $a0, prompt
la $a1, input
li $a2, 100
syscall
la $t0, input # store the text to analyze in $t0
li $t1, 0x20 # store the space character in $t1
li $v0, -1 # makes $v0, the number of characters, initally equal to -1 to ignore the new line character
li $v1, 1 # makes $v0, the number of words, initially equal to 1 to account for the last word in an input
jal count # calls the count function
la $s0, ($v0) # loads the number of characters to $s0
la $s1, ($v1) # loads the number of words to $s1
bne $s0, -1, printInput
# handles the case in which an empty string is given
li $s0, 0 # set the characters to 0
li $s1, 0 # set the words to 0
printInput:
# prints the input that was analyzed
li $v0, 4
la $a0, input
syscall
printWords:
# prints the message "Characters: "
li $v0, 4
la $a0, charactersMessage
syscall
# prints the amount of characters in the input text
li $v0, 1
la $a0, ($s0)
syscall
# prints a new line character
li $v0, 4
la $a0, newLine
syscall
printLetters:
# prints the message "Words: "
li $v0, 4
la $a0, wordsMessage
syscall
# prints the amount of words in the input text
li $v0, 1
la $a0, ($s1)
syscall
# ends the program
li $v0, 10
syscall
count:
lb $a0, ($t0) # stores the single byte we are analyzing into $a0
beqz $a0, exit # if we reach a zero character, then we are done analyzing the input, so exit
# stores our current PC memory location in the $sp (stack pointer) to refer back to the previous
# location we wish to go back to if we do, in fact, traverse to the countword function
addi $sp, $sp, -4
sw $ra, ($sp)
# if we encounter a space character, count the word using the countword function
bne $a0, $t1, continue
jal countword
continue:
# restores the $ra that was cached from the $sp
lw $ra, ($sp)
addi $sp, $sp, 4
addi $v0, $v0, 1 # increments the letter counter
addi $t0, $t0, 1 # increments the byte being read from the input
j count # loop back to counting
exit:
jr $ra # jumps back to where we came from
countword:
addi $v1, $v1, 1 # increments the word counter
jr $ra # jumps back from where we came from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment