Exercises 1 and 2 -- Capitalization (2 versions) Declare a string in the data se
ID: 3586632 • Letter: E
Question
Exercises 1 and 2 -- Capitalization (2 versions)
Declare a string in the data section such as:
.data
string: .asciiz "in a hole in the ground there lived a hobbit"
Notice there are extra spaces in this string. Write an MIPS program that capitalizes the first letter of each word, so that after running your program the data will look like this:
.data
string: .asciiz "In A Hole In The Ground There Lived A Hobbit"
Easy version (exercise 1): assume that the data comprises only lower case characters and spaces. There may, however, be several spaces in a row (as in "the ground" above). Be sure to capitalize only the first letter of the words.
Medium-hard version (exercise 2): Rewrite your program so that it assumes that the data comprises only upper and lower case characters and spaces, and alters a character only if it is lower case, and follows a space or is the first character on the line.
For both versions, print the strings before and after translation by using the syscall print string service.
Explanation / Answer
This COde works for both the exercise (I have edited the code from before ).Please check once and comment if you need any help:-
.data
msg_before: .asciiz "String before conversion"
string: .asciiz "in a hole in the ground there lived a hobbit"
msg_after: .asciiz "String after conversion"
.text
.globl main
main:
la $a0,msg_before #print message
li $v0,4
syscall
la $a0,string #print string before conversion
li $v0,4
syscall
la $t0,string #loading base address of string
loop: #continuing loop character by character till end of string
lb $t1,($t0)
beqz $t1,loop_end #condition for loop end
bne $t1,' ',next_element #checking for space
addiu $t0,$t0,1 #if space character;then we are going for next character in order to #change it's case
lb $t1,($t0)
bgt $t1,'z',next_element #checking for small letter alphabet
blt $t1,'a',next_element
addiu $t1,$t1, 32 #if small letter alphabet then changing its case to upper
sb $t1,($t0)
next_element:
addiu $t0,$t0,1 #next character
j loop
loop_end:
la $a0,msg_after #print msg
li $v0,4
syscall
la $a0,string #print string after conversion
li $v0,4
syscall
li $v0,10 #exit
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.