Write a MAL PROGRAM with the following assumptions and instructions... (MAL = MI
ID: 3576340 • Letter: W
Question
Write a MAL PROGRAM with the following assumptions and instructions... (MAL = MIPS ASSEMBLY LANGUAGE)
1. Any line of text typed by a user has at most 80 characters including the newline character.
2. A whitespace character refers to a space, a tab or the newline character.
3. 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.
(a) The number of non-whitespace characters in the line.
(b) The number of words in the line.
(c) The maximum length of a word in the line.
(d) The minimum length of a word in the line.
(e) 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.)
3
(f) 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.)
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: of
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 last such word is "of" (again, without the quotes).
Explanation / Answer
.text # beginning of code .globl main # beginning of main main: # main procedure li $v0, 4 # print_string service number la $a0, prompt00 # load address of prompt syscall # print prompt li $v0, 8 # read_string service number la $a0, buffer # load address of buffer la $a1, 40 # max length of 40 syscall # read_string li $v0, 4 # print_string service number la $a0, buffer # load address of buffer syscall # print buffer li $v0, 10 # using service 10, terminate syscall # terminate .data # beginning of data area buffer: # container for input string .space 40 # max length of 40 characters newline: # variable to represent a newline .asciiz " " # a newline character prompt00: .asciiz "Enter up to 40 characters: "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.