Swap the Case in MIPS Assembly Homework Sample

Change the case of letters in an ascii string (upper case becomes lower case, and vice versa). The string should be 0 terminated. The string should be copied to the destination and not modified in place. For more assembly language programming assignments contact us for a quote.

Solution:

.global main
.set noreorder
.data
answer: .space 512
.text
.ent main
input: .ascii “Lorem ipsum dolor sit amet, consectetur adipiscing elit. ”
.ascii “Maecenas vestibulum laoreet est, a rutrum odio feugiat eu. ”
.ascii “Aenean vitae luctus lacus, non maximus risus. Maecenas ”
.ascii “tincidunt turpis quis mauris semper elementum. Suspendisse et ”
.ascii “urna lacinia, eleifend magna non, dignissim elit. Nulla lorem ”
.ascii “lectus, pharetra vitae posuere in, vehicula fringilla metus.”
.long 0
main:
la $t1, input # load input address in t0
la $t2, answer # load output address in t2
loop:
lb $t0, 0($t1) # load a character from the input array
beqz $t0, done # if the character is zero, end the loop
nop
blt $t0, ‘A’, saveChar # if it’s < A, is not a letter, skip
nop
ble $t0, ‘Z’, isUpper # if it’s >=A and <=Z, it’s an uppercase letter
nop
blt $t0, ‘a’, saveChar # if it’s < a, is not a letter, skip
nop
ble $t0, ‘z’, isLower # if it’s >=a and <=z, it’s a lowercase letter
nop
b saveChar # in any other case, go to next character
nop
isUpper:
addi $t0, $t0, 32 # convert upper to lowercase
b saveChar # save the character in the array
nop
isLower:
addi $t0, $t0, -32 # convert lower to uppercase
saveChar:
sb $t0, 0($t2) # save the converter character in the array
addi $t1, $t1, 1 # increment pointer to go to the next character in input array
addi $t2, $t2, 1 # increment pointer to go to the next character in output array
b loop # repeat the loop for loading the remaining characters
nop

done:
sb $zero, 0($t2) # save a zero at the end of the output array
la $v0, answer # load address of converted string in v0

endloop:
b endloop # repeat loop forever to end program
.end main