Prompt the user and read in a string. Make your string large enough to hold 100
ID: 3769253 • Letter: P
Question
Prompt the user and read in a string. Make your string large enough to hold 100 characters.
Assuming there is just one blank between each word, count the number of words in the string.
Create a new string which is a copy of the original string, except that the new string is converted to uppercase. All non-letter characters should be unchanged. Convert the letters using difference between the ASCII codes for capital letters and the ASCII codes for small letters.
Print the original string, the new string, and the word count. Make sure you print messages to label your output.
When we call syscall to read in a string, we give a length n in $a1. syscall reads in at most n-1 characters and it adds a null byte (which has the value 0x00) to the end of the string. If the string is less than n-1 characters, then syscall adds a newline (which has the value 0xa) after the null byte. To find the end of the string, test for the null byte. You can ignore the newline when counting words and converting to uppercase.
Explanation / Answer
ANSWER:
#MIPS PROGRAM TO CONVERT THE STRING INTO UPPERCASE
.data
promptUser: .asciiz " ENTER STRING: "
printString: .asciiz " ORIGINAL STRING: "
printConvertedString: .asciiz "CONVERTED STRING:"
userString: .space 100 ## space for input string
printCount: .asciiz "NUMBER OF WORDS IN THE STRING:"
.text
.globl main
main:
la $a0,promptUser ## PROMPT THE USER TO ENTER THE STRING
li $v0,4
syscall
la $a0,userString ## RREAD THE STRING
li $a1,101 ## at most 30 chars + 1 null char
li $v0,8
syscall
#PRINT THE ORIGINAL STRING
la $a0,printString
li $v0,4
syscall
la $a0,userString
li $v0,4
syscall
la $t0,userString #$t0 POINTS TO userString
li $t2,32
li $t3,0
#LOOP TO CONVERT STRING INTO UPPERCASE LETTERS
LOOP1:
lb $t1,0($t0)
beqz $t1,LOOP2 #JUMP TO LOOP2 WHEN STRING ENDS
blt $t1,'a',NOMODIFICATION
bgt $t1,'z',NOMODIFICATION
addiu $t1,$t1,-32
sb $t1,0($t0)
addiu $t0,$t0,1
j LOOP1
NOMODIFICATION:
addiu $t0,$t0,1 ## GET NEXT CHARACTER IN THE STRING
j LOOP1 #JUMP TO BEGINING OF THE LOOP
la $t0,userString
#LOOP TO COUNT THE NUMBER OF WORDS IN THE STRING
LOOP2:
lb $t1,0($t0)
beqz $t1,END_LOOPS
beq $t1,$t2,NEWWORDOCCUR
addiu $t0,$t0,1
j LOOP2
NEWWORDOCCUR:
addiu $t3,$t3,1 #INCREMENT NEW WORD COUNTER BY 1
addiu $t0,$t0,1
j LOOP2
END_LOOPS:
la $a0,printConvertedString:
li $v0,4
syscall
la $a0,userString ## PRINT THE CONVERTED STRING
li $v0,4
syscall
la $a0,printCount #PRINT THE NUMBER OF WORDS IN THE STRING
li $v0,4
syscall
la $a0,$t3
li $v0,1
syscall
li $v0,10 ## EXIT FROM THE PROGRAM
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.