For this part, your MAL program must be in a file named p5b.mal. It must have at
ID: 3579152 • Letter: F
Question
For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following. Any line of text typed by a user has at most 80 characters including the newline character. A whitespace character refers to a space, a tab or the newline character. A word is any sequence of characters that does not contain a whitespace character. In this part, you are required to write a MAL program that prompts the user for a line of text and reads the line typed by the user. If the line contains just white space characters your program should simply output the message "Line contains only white space characters" and stop. Otherwise, your program should compute and output the following. The number of non-whitespace characters in the line. The number of words in the line. The maximum length of a word in the line. The minimum length of a word in the line. The word of maximum length in the line. (If there are two or more words of maximum length in the line, then the program should print the word of maximum length that appears last in the line.) The word of minimum length in the line. (If there are two or more words of minimum length in the line, then the program should print the word of minimum length that appears last in the line.)Explanation / Answer
main:
#we must Print out request for user to input a line.
la $a0,Input_Message
li $v0, 4
syscall
#from the user Now we are going to read a line of words
la $a0,Input_Line
#it indicate the buffer length, so at the most 80 characters including ''
#it will be written to the buffer.
li $a1, 80
li $v0, 8
syscall
#Now initiailize all our variables.
li $s0,0 # stores the number of words
li $s1,0 #stores the number of nonwhitespace_Char
la $s3, Input_Line # ours loop varaible
sw $0,Max_Word_Length # initialize it to 0(In other words an impossible value)
li $t0,81
sw $t0,Min_Word_Length # initialize it to 81 (In other words an impossible value)
Analyze_Line_Loop:
#now Check if we have reached the end of the loop. that is we encountered the ''
#the character. In that case we sill simply stop our analysis and print out the results.
lb $t0,($s3)
beqz $t0,Analyze_Line_Loop_End
#v must Call the detect_Word function. This function will assume that the address passed is
#then start of the word. It will return the length of the word in case the word did
#starts from this place otherwise it will simply return 0 meaning no word found at this
#location.
move $a0,$s3
jal Function_Detect_Word
#Checks if we did found a word. In that case we are going to jump to Word_Found. Otherwise
#we are simply going to increment the pointer and jump back to start of the loop
bnez $v0,Word_Found
addu $s3,$s3,1
b Analyze_Line_Loop
Word_Found:
#here If we found a word than we will increment number of words and nonspace character counts
add $s0,$s0,1
move $s4,$v0
add $s1,$s1,$s4
#we are need to check if the word detected is longer than any of the previous words
lw $t3,Max_Word_Length
bgt $v0,$t3,Update_Max
b Check_Min
Update_Max:
#here We will call our strncpy function. Which copies the specified number of characters
#from source string into the destination address and appends a '' character at the
#end to make it a null terminated string.
la $a0,Max_Word #Destination address
move $a1,$s3 #Source address
move $a2,$s4 #Length of the string to be copied
jal Function_Strncpy
#We will also update the Max_Word_Length
sw $s4,Max_Word_Length
Check_Min:
#here we need to check if the word detected is smaller than any of the previous words.
#If it indeed is than we will update our variables.
lw $t3, Min_Word_Length
bgt $t3,$s4, Update_Min
b Increment_Pointer
Update_Min:
#We will call our strncpy function. Which copies the specified number of characters
#from source string into the destination address and appends a '' character at the
#end to make it a null terminated string.
la $a0,Min_Word #Destination address
move $a1,$s3 #Source address
move $a2,$s4 #Length of the string to be copied
jal Function_Strncpy
#We also need to update the Min_Word_Length variable
sw $s4,Min_Word_Length #Update the Min word length
Increment_Pointer:
#Now we need to increment our pointer, so that we can search the next word. The
#increment is of the equal to the length of the word detected. We will jump back
#to the top of the loop to start analyzing the input string again.
addu $s3,$s3,$s4
b Analyze_Line_Loop
Analyze_Line_Loop_End:
#If we were unable to detect even a single word than that means the line only
#contained space characters. So we will print the error messages and than jump
#to the end of the function. However if that is not the case than we will jump
#to Print_Results to print out all the results.
bnez $s1,Print_Results
#We need to print the error message that the string was completly empty
la $a0,Output_Error_Message
li $v0, 4
syscall
#We will jump to the end of te function and call the halt system call.
b Main_End
Print_Results:
#If we were able to find some resutls than we are going to print out the resutls.
#Print the number of nonspace characters.
la $a0,Output_NumberOfnonWhiteSpaceChar_Message
li $v0, 4
syscall
move $a0, $s1
li $v0, 1
syscall
la $a0,NewLine
li $v0, 4
syscall
#Print the number of words in the line.
la $a0,Output_NumberOfWords_Message
li $v0, 4
syscall
move $a0, $s0
li $v0, 1
syscall
la $a0,NewLine
li $v0, 4
syscall
#Print the length of the longest word.
la $a0,Output_MaxWordLength_Message
li $v0, 4
syscall
lw $a0,Max_Word_Length
li $v0, 1
syscall
la $a0,NewLine
li $v0, 4
syscall
#Print the length of the smallest word.
la $a0,Output_MinWordLength_Message
li $v0, 4
syscall
lw $a0,Min_Word_Length
li $v0, 1
syscall
la $a0,NewLine
li $v0, 4
syscall
#Print the longest word
la $a0,Output_MaxWord_Message
li $v0, 4
syscall
la $a0,Max_Word
li $v0, 4
syscall
la $a0,NewLine
li $v0, 4
syscall
#Print the smallest word
la $a0,Output_MinWord_Message
li $v0, 4
syscall
la $a0,Min_Word
li $v0, 4
syscall
la $a0,NewLine
li $v0, 4
syscall
Main_End:
#At the end of our execution we need to halt the system.
li $v0,10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.