In: Computer Science
How do I write a program in MIPS that will change all the characters in a string into lowercase.
For instance: string: " CANBERRA AUSTRALIA"
Output: "canberra australia"
Screenshot of the code:-
Screenshot of the output(in Mars Simulator):-
MIPS code to copy(with comments):-
.data
in_str: .asciiz "Enter the string: "
out_str: .asciiz "Capitalized String is: "
buff: .space 80 #this is the buffer
.text
main:
la $a0, in_str # Load and print string asking for string
li $v0, 4
syscall
li $v0, 8 # Get the input
la $a0, buff # load the byte space into the address
li $a1, 80 # allot the byte space for string
syscall
move $s0, $a0 # save string
li $v0, 4
li $t0, 0
#Loop to convert to lowercase
loop:
lb $t1, buff($t0) #Load byte from 't0'th position in the buffer
into $t1
beq $t1, 0, exit #If input ends, go to exit
bge $t1, 'A', maybe_not_capital #If greater than A, go to
maybe_not_capital
ble $t1,'A',other_chars #If less than A,go to other_chars
j upper_to_lower
upper_to_lower:
addi $t1,$t1,32
sb $t1, buff($t0) #Store it back to 't0'th position in buffer
maybe_not_capital:
blt $t1, 'Z',upper_to_lower #If less than Z, go to
upper_to_lower
addi $t0, $t0, 1 #else, increment $t0 and continue
j loop
other_chars:
beq $t1,' ',space #If it is space,then go to space
space:
addi $t0, $t0, 1
j loop
exit:
la $a0, out_str # load and print the capitalised string
li $v0, 4
syscall
move $a0, $s0
li $v0, 4 # print string
syscall
li $v0, 10 # end program
syscall