Sample solution : 22C:040 Homework 4

 Sample solution to homework 4 by Todd Rouse 

       .data
visited_array: .word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0  #array to keep track of visited characters
Buffer: .space 60                                                         #Buffer to hold input string
prompt: .asciiz "\n Enter a >= 30 character string:"                      #Prompt for input
letter_response: .asciiz "a"                                              #array to hold letter strings
                 .asciiz "b"
		 .asciiz "c"
		 .asciiz "d"
		 .asciiz "e"
		 .asciiz "f"
		 .asciiz "g"
		 .asciiz "h"
		 .asciiz "i"
		 .asciiz "j"
		 .asciiz "k"
		 .asciiz "l"
		 .asciiz "m"
		 .asciiz "n"
		 .asciiz "o"
		 .asciiz "p"
		 .asciiz "q"
		 .asciiz "r"
		 .asciiz "s"
		 .asciiz "t"
		 .asciiz "u"
		 .asciiz "v"
		 .asciiz "w"
		 .asciiz "x"
		 .asciiz "y"
		 .asciiz "z"
response_one: .asciiz "\n Frequency of "                                #Output for frequncy-part one
response_two: .asciiz "="                                               #Output for frequncy-part two
bye: .asciiz "\n Goodbye!"                                              #Output before exit

       .globl main
       .text

main:  li $v0, 4                            #start of main-system call code for Print string
       la $a0, prompt                       #load address of prompt into $a0
       syscall                              #print string
       li $v0, 8                            #system call code for read string
       la $a0, Buffer                       #load address of Buffer into $a0
       li $a1, 60                           #system call code for size of string
       syscall                              #read string
       add $s0, $zero, $a0                  #put address of Buffer into $s0
loop:  lb $s1, 0($s0)                       #start of loop-load first character into $s1
       beq $s1, $zero, End                  #branch to End if character is null character
       addi $s0,$s0,1                       #add 1 to address of Buffer to get next character
       addi $s2, $zero, 97                  #put 97 into $s2-start of lower case charcter ascii code
       bge $s1, $s2, index_check            #if character greater than or equal to 97 branch to index_check
       addi $s1, $s1,32                     #if character less than 97 add 32 to it-convert upper case to lower case
       bge $s1, $s2, index_check            #if character greater than or equal to 97 branch to index_check
       j loop                               #jump to loop
index_check: addi $s3, $s1, -97             #subtract 97 to get index of character
       addi $s4, $zero, 4                   #put 4 into $s4
       mul $s5, $s3, $s4                    #multiply index by 4 and put in $s5
       la $s6, visited_array                #load address of visited_array into $s6
       add $s6, $s6, $s5                    #move up to proper index
       lw $s7, 0($s6)                       #load value of index into $s7
       bne $s7, $zero, loop                 #if value not equal to zero branch to loop-already visited
       addi $t7, $zero, 1                   #if first time character-change index value to 1
       sw $t7, 0($s6)                       #store index value back to memory
       move $a0, $s0                        #move Buffer address into $a0 for Count function argument
       move $a1, $s1                        #mover character into $a1 for Count function argument
       move $a2, $s3                        #move character index into $a2 for Count function argument
       jal Count                            #jump and link-call to Count function
       j loop                               #jump to loop on return from Count function
End:   li $v0, 4                            #End-system call to print string
       la $a0, bye                          #load address of bye prompt
       syscall                              #print string
       li $v0, 10                           #system call to terminate program run
       syscall                              #return control to system
####################################################################################################################################
#Count function-counts frequency of characters in input string and ouptut frequencies
#arguments $a0-address of string, $a1-value of character, $a2-index of character
Count:                                      #start of count
      addi $t0, $zero, 1                    #put 1 into St0-counter
loop_2: lb $t1, 0($a0)                      #start of loop_2-load characters from string
      beq $t1, $zero, return                #if character equals null character branch to return
      addi $a0, $a0, 1                      #add 1 to string address-to get next character
      bne $t1, $a1, loop_2                  #if character values not equal branch to loop_2
      addi $t0, $t0, 1                      #add 1 to counter
      j loop_2                              #jump to loop_2
return: addi $t3, $zero, 2                  #start return-put 2 in $t3
      mul $t4, $t3, $a2                     #multiply index by 2
      li $v0, 4                             #system call code for print string
      la $a0, response_one                  #load address for response_one in $a0
      syscall                               #print string
      la $t6, letter_response               #load base address of letter_response array
      add $t5, $t6, $t4                     #add offset to base address to get correct string
      li $v0, 4                             #system call code for print string
      move $a0, $t5                         #move address of correct letter string into $a0
      syscall                               #print string
      li $v0, 4                             #system call code for print string
      la $a0, response_two                  #load address of reponse_two prompt into $a0
      syscall                               #print string
      li $v0, 1                             #system call code for print integer
      move $a0, $t0                         #move counter value into $a0
      syscall                               #print integer
      jr $ra                                #jump register-return to main program