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
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?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
