Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this part, you are required to write a MAL program that prompts the user for

ID: 3831956 • Letter: I

Question

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:

1.The number of non-whitespace characters in the line.

2.The number of words in the line.

3.The maximum length of a word in the line.

4.The minimum length of a word in the line.

5.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 first in the line.)

1.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 first in the line.)

Example: Suppose the line typed by the user is the following:

It was the best of times and it was the worst of times.

The answers for the above line are:

No. of non-whitespace characters: 43

No. of words: 13

Maximum length of a word: 6

Minimum length of a word: 2

Word of maximum length: times.

Word of minimum length: It

Note that the word of maximum length (6) is "times." (without the quotes), which includes the punctuation mark at the end. (Recall that a word is any sequence of characters that does not include a whitespace character.) There are several words of minimum length (2) in the above text. The first such word is "It" (again, without the quotes).

Program outline:

The outline for your program for Part (b) must be the following.

Prompt the user for a line of text.

Read the line of text typed by the user.

If the line has only whitespace characters print the message "Line contains only white space characters" and stop.

Otherwise compute the quantities mentioned above and print the answers.

Stop.

Programming Suggestions:

1)MAL program must be in a file named .

2)It must have at least one function in addition to the main program.

3)Study Lecture 15 (arrays of character in MAL) before working on Part (b).

4)You may find it useful to write a function that returns information (e.g. starting and ending indices) about the next word.

5)A word is any sequence of characters that does not contain a whitespace character.

6)A whitespace character refers to a space, a tab or the newline character.

7)Any line of text typed by a user has at most 80 characters including the newline character.

8)End of any line is determined by the character.

9)It is excepted to check whether the input line consists of just whitespace characters, no other error checks are needed.

10)Each time your program is executed, it should handle just one line of text.

Explanation / Answer

Executable Code:-

.data
str: .space 81   
strNS: .space 81   
prompt: .asciiz "Enter a string up to 80 characters "
head1: .asciiz " Original String: "
head2: .asciiz " Number of spaces: "
head3: .asciiz " With spaces removed: "

.text
main:

li $v0, 4
la $a0, prompt   
syscall   
li $v0, 8   
la $a0, str   
li $a1, 81
syscall

jal countSpace

addi $t1, $v0, 0
li $v0, 4   
la $a0, head1
syscall
la $a0, str
syscall
la $a0, head2   
syscall
li $v0, 1   
addi $a0, $t1, 0   
syscall

li $v0, 4
la $a0, head3   
syscall
la $a0, strNS   
syscall


End:   
li $v0, 10   
syscall

countSpace:
la $s0, strNS
addi $sp, $sp, -12
sw $s0, 8($sp)   
sw $ra, 4($sp)   
sw $a0, 0($sp)   


addi $t3, $a0, 0   
addi $t5, $s0, 0   
li $t6, 0
li $t0, 0
li $t4, 0
loop:
add $t1, $t3, $t4   
lb $t2, 0($t1)   
beq $t2, $zero, exitCS
addi $a0, $t2, 0   


addi $sp, $sp, -28   
sw $t6, 24($sp)
sw $t5, 20($sp)
sw $t4, 16($sp)
sw $t3, 12($sp)
sb $t2, 8($sp)   
sw $t1, 4($sp)   
sw $t0, 0($sp)   

jal isSpace   


lw $t6, 24($sp)
lw $t5, 20($sp)
lw $t4, 16($sp)
lw $t3, 12($sp)
lb $t2, 8($sp)
lw $t1, 4($sp)
lw $t0, 0($sp)
addi $sp, $sp, 28
beq $v0, $zero, addTo
addi $t0, $t0, 1   
addTo:
bne $v0, $zero, nextChar
sll $t7, $t6, 2   
add $t7, $t7, $t5   
sb $t2, 0($t7)   
addi $t6, $t6, 1   

nextChar:

addi $t4, $t4, 1   
j loop

exitCS:
addi $v0, $t0, 0   
addi $v1, $t5, 0
lw $ra, 4($sp)   
lw $a0, 0($sp)   
addi $sp, $sp, 8   
jr $ra   

isSpace:
addi $sp, $sp, -12
sw $s0, 8($sp)
sw $ra, 4($sp)
sw $a0, 0($sp)


li $t0, 32
li $v0, 0   
bne $t0, $a0, endSC
li $v0, 1   

endSC:
lw $s0, 8($sp)
lw $ra, 4($sp)
lw $a0, 0($sp)
addi $sp, $sp, 12   

end: jr $ra

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote