Declare a string in the data section such as: .data string: .asciiz \"in a hole
ID: 3586158 • Letter: D
Question
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"
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.
Print the strings before and after translation by using the syscall print string service.
Explanation / Answer
.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.