Declare a string in the data section such as: .data string: .asciiz \"in a hole
ID: 3587064 • 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 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.
Print the strings before and after translation by using the syscall print string service.
Here is my solution:
.text
.globl main
main:
li $v0, 4 # Print the original string.
la $a0, string
syscall
loop:
lb $t1, string($t0)
nop
nop
beq $t1, $0, end # if ($t1 == NULL) print the result
nop
nop
bne $t0, $0, next # Check if first character is lower case
nop
nop
blt $t1, 97, next2 # Convert from lower case to upper case
nop
nop
bgt $t1, 122, next2
nop
sub $t1, $t1, 32
sb $t1, string($t0)
addi $t0, $t0, 1
j loop
next:
bne $t1, 32, next2 # if ($t1 == ' ') check for next character
nop
addi $t0, $t0, 1
lb $t1, string($t0)
nop
nop
blt $t1, 97, next2 # Convert from lower case to upper case
nop
nop
bgt $t1, 122, next2
nop
sub $t1, $t1, 32
sb $t1, string($t0)
addi $t0, $t0, 1
j loop
next2:
addi $t0, $t0, 1
j loop
end:
li $v0, 4 # Print a new line.
la $a0, newline
syscall
li $v0, 4 # Print the new string.
la $a0, string
syscall
li $v0, 10 # End the program.
syscall
.data
string: .asciiz "in a hole in the ground there lived a hobbit"
newline: .asciiz " "
Here is my output:
"In A Hole In The Ground There Lived A hobbit"
I can't seem to capitalize the letter "h" in "hobbit". Can someone help me fix this?
Explanation / Answer
CODE
Can you use below code it is working
.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
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.