Purpose of this project is to increase your understanding of data, address, memory contents, and strings. You will be expected to apply selected MIPS assembly language instructions, assembler directives and system calls sufficient enough to handle string manipulation tasks. You are tasked to develop a program that finds how many times a word is used in a given statement. To test your program, you should hardcode the below sample statement in your code, and ask user to input two different words, which are "UCF" and "KNIGHTS" in this project, however your code should work for any words with less than 10 characters. Your program should not be case sensitive and regardless of the way user inputs the words it should correctly find the words.

Sample Statement: UCF, its athletic program, and the university's alumni and sports fans are sometimes jointly referred to as the UCF Nation, and are represented by the mascot Knightro. The Knight was chosen as the university mascot in 1970 by student election. The Knights of Pegasus was a submission put forth by students, staff, and faculty, who wished to replace UCF's original mascot, the Citronaut, which was a mix between an orange and an astronaut. The Knights were also chosen over Vincent the Vulture, which was a popular unofficial mascot among students at the time. I11 1994, Knightro debuted as the Knights official athletic mascot.

Sample Output: Please input first word: Knight (or KnIGhT, knight, ...) Please input second word: UCF (or ucf, UcF, ...)

KNIGHT: - 6
UCF: ----3

Respuesta :

Answer:

See explaination for the details

Explanation:

#Starting point for code/programm

main:

la $a0,newLine #Print a new line

li $v0,4

syscall

# Find the number of occurence of a string in the given sentence

la $a0,prompt1 # Prompt the user to enter the first string.

li $v0,4

syscall

li $v0, 8 # Service 8 = read input string

la $a0, fword

li $a1, 9

syscall

la $a0,prompt2 # Prompt the user to enter the second string.

li $v0,4

syscall

li $v0, 8 # Service 8 = read input string

la $a0, sword

li $a1, 9

syscall

# process first word

li $t4,0 # Intialize the couter to 0

la $t0,sstatement # Store the statement into $t0

nstart1: la $t1,fword # Store the search word into $t1

loop1: # loop1 finds the number of occurences

# of input word in the given statment

lb $t2,($t0) # Load the starting address(character) of

# sstatement into $t2

lb $t3,($t1) # Load the starting address of input word

# into $t3

beq $t3,'\n',inc_counter1

beqz $t3,inc_counter1 # If $t3 is null , exit loop and print output

beqz $t2,print_output1 # If $t2 is null , exit loop and print output

move $a0,$t2 # Convert $t2 to lower, if it is upper case

jal convert2lower

move $t2,$v0 # Store the return($v0) value into $t2

move $a0,$t3 # Convert $t3 to lower, if it is upper case

jal convert2lower

move $t3,$v0 # Store the return($v0) value into $t3

bne $t2,$t3,next_char1 # If both characters are not matched current

# character in the string, go to next character

addiu $t0,$t0,1 # otherwise, increment both indexes

addiu $t1,$t1,1

j loop1 # go to starting of the loop

next_char1:

la $t5,fword

bne $t5,$t1,nstart1

la $t1,fword # Store the input word into $t1

addiu $t0,$t0,1 # Increment the index to goto next character

j loop1 # go to starting of the loop

inc_counter1:

addi $t4,$t4,1 # Increment the frequency counter by 1

la $t1,fword # Store input word into $t1

j loop1 # go to starting of the loop

print_output1:

la $t0,fword

L1:

lb $a0,($t0)

beq $a0,'\n',exL1

jal convert2upper

move $a0,$v0

li $v0,11

syscall

addiu $t0,$t0,1

j L1

exL1:

la $a0,colon

li $v0,4

syscall

la $a0, dash

li $v0, 4

syscall

move $a0,$t4

li $v0,1

syscall # print new line

la $a0,newLine

li $v0,4

syscall

# process second word

li $t4,0 # Intialize the couter to 0

la $t0,sstatement # Store the statement into $t0

nstart2: la $t1,sword # Store the search word into $t1

loop2: # loop1 finds the number of occurences

# of input word in the given statment

lb $t2,($t0) # Load the starting address(character) of

# sstatement into $t2

lb $t3,($t1) # Load the starting address of input word

# into $t3

beq $t3,'\n',inc_counter2

beqz $t3,inc_counter2 # If $t3 is null , exit loop and print output

beqz $t2,print_output2 # If $t2 is null , exit loop and print output

move $a0,$t2 # Convert $t2 to lower, if it is upper case

jal convert2lower

move $t2,$v0 # Store the return($v0) value into $t2

move $a0,$t3 # Convert $t3 to lower, if it is upper case

jal convert2lower

move $t3,$v0 # Store the return($v0) value into $t3

bne $t2,$t3,next_char2 # If both characters are not matched current

# character in the string, go to next character

addiu $t0,$t0,1 # otherwise, increment both indexes

addiu $t1,$t1,1

j loop2 # go to starting of the loop

next_char2:

la $t5,sword

bne $t5,$t1,nstart2

la $t1,sword # Store the input word into $t1

addiu $t0,$t0,1 # Increment the index to goto next character

j loop2 # go to starting of the loop

inc_counter2:

addi $t4,$t4,1 # Increment the frequency counter by 1

la $t1,sword # Store input word into $t1

j loop2 # go to starting of the loop

print_output2:

la $t0,sword

L2:

lb $a0,($t0)

beq $a0,'\n',exL2

jal convert2upper

move $a0,$v0

li $v0,11

syscall

addiu $t0,$t0,1

j L2

exL2:

la $a0,colon

li $v0,4

syscall

la $a0, dash2

li $v0, 4

syscall

move $a0,$t4

li $v0,1

syscall

exit:

# Otherwise, end the program

li $v0, 10 # Service 10 = exit or end program

syscall

############################ subroutine - convert2lower #################################

convert2lower: # Converts a character(stored in $a0) to

# its lower case, if it is upper case

# and store the result(lower case) in $v0

move $v0,$a0

blt $a0,'A',return

bgt $a0,'Z',return

subi $v0,$a0,-32

return: jr $ra # Return the converted(lower case) character

############################## subroutine - convert2upper ##################################

convert2upper: # Converts a character(stored in $a0) to

# its upper case, if it is lower case

# and store the result(upper case) in $v0

move $v0,$a0

blt $a0,'a',return2

bgt $a0,'z',return2

addiu $v0,$a0,-32

return2: jr $ra # Return the converted(lower case) character