Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- toUpper: .byte 1
- toLower: .byte 1
- string:
- .asciiz " A long time ago in a Galaxy far, far away...."
- # Your code goes below this line
- ogString: # Set up all strings that may
- .asciiz "Original string:" # be printed in the program
- ucString:
- .asciiz "Converted to upper-case:"
- lcString:
- .asciiz "Converted to lower-case:"
- fString:
- .asciiz "Forward:"
- bString:
- .asciiz "Backward:"
- counter: .byte 0
- newline:
- .asciiz "\n"
- .text
- main: # Function prologue -- even main has one
- addiu $sp, $sp, -24 # allocate stack space -- default of 24 here
- sw $fp, 0($sp) # save caller's frame pointer
- sw $ra, 4($sp) # save return address
- addiu $fp, $sp, 20 # setup main's frame pointer
- la $a0, ogString # print out "Original String:"
- addi $v0, $zero, 4
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, string # print out the string
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, newline # print a newline
- syscall
- la $s0, counter # initialize counter into $t3
- lb $t3, 0($s0)
- ###############################################################
- upperConversion: # begin conversion to upper case
- la $s0, toUpper
- lb $t1, 0($s0)
- beq $zero, $t1, lowerConversion # skip if toUpper is 0
- la $a0, ucString # print out "converting to uppercase"
- syscall
- la $a0, newline # print a newline
- syscall
- la $s0, string # load the address of string into $s0
- upperLoop:
- # $t0 is the current character
- # $s0 is the string address
- # $t3 is the array counter
- lb $t0, 0($s0) # load a byte from the array into $t0
- beq $zero, $t0, upperLoopEnd # skip to lowerConversion if we
- # reach the null character
- addi $t1, $zero, 'z'
- addi $t1, $t1, 1
- slt $t1, $t0, $t1 # if current character is less than 123
- beq $t1, 1, lessThan123 # advance to the next part, otherwise
- j upperLoopIncrement # it isn't a lowercase letter, so it
- # can't be capitalized, move on to the
- # next character in the string
- lessThan123:
- slt $t2, $t0, 'a' # if current character is greater
- bne $t2, 1, capitalize # than or equal to 97, it is already
- j upperLoopIncrement # less than 123, so it must be a
- # lowercase letter, so capitalize it.
- # Otherwise, move on to the next char
- capitalize:
- addi $t0, $t0, -32 # subtract 32 from the lowercase
- sb $t0, 0($s0) # character to get to its'
- # uppercase version, then store
- # it back into $t0.
- j upperLoopIncrement
- upperLoopIncrement:
- addi $s0, $s0, 1 # increment $s0 to point to the next element
- addi $t3, $t3, 1 # increment counter in $t3
- j upperLoop
- upperLoopEnd:
- la $a0, fString # print "Forward:"
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, string # print the new all uppercase string
- syscall
- addi $v0, $zero, 4
- la $a0, newline # print a newline
- syscall
- la $a0, bString # print "Backward:"
- syscall
- la $a0, newline # print a newline
- syscall
- #addi $s0, $s0, -1 # decrement $s0 to account for null char
- backWriter:
- beq $zero, $t3, backWriterEnd #end once $t0 is 0
- lb $a0, 0($t3)
- addi $v0, $zero, 11 # print the character in question
- syscall
- addi $t3, $t3, -1 # decrement $s0 to point to the prev. element
- j backWriter
- backWriterEnd:
- la $a0, string # print the string backwards
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, newline # print a newline
- syscall
- ###############################################################
- lowerConversion: # begin conversion to lower case
- la $s0, toLower
- lb $t1, 0($s0)
- beq $zero, $t1, done # skip if toLower is 0
- la $a0, lcString # print out "converting to lowercase"
- syscall
- la $a0, newline # print a newline
- syscall
- lowerLoop:
- # $t0 is the current character
- # $s0 is the string address
- lb $t0, 0($s0) # load a byte from the array into $t0
- beq $zero, $t0, lowerLoopEnd # skip to lowerConversion if we
- # reach the null character
- addi $t1, $zero, 'Z'
- addi $t1, $t1, 1
- slt $t1, $t0, $t1 # if current character is less than 123
- beq $t1, 1, lessThan90 # advance to the next part, otherwise
- j lowerLoopIncrement # it isn't a lowercase letter, so it
- # can't be capitalized, move on to the
- # next character in the string
- lessThan90:
- slt $t2, $t0, 'A' # if current character is greater
- bne $t2, 1, decapitalize # than or equal to 97, it is already
- j lowerLoopIncrement # less than 123, so it must be a
- # lowercase letter, so capitalize it.
- # Otherwise, move on to the next char
- decapitalize:
- addi $t0, $t0, 32 # add 32 from the uppercase
- sb $t0, 0($s0) # character to get to its'
- # uppercase version, then store
- # it back into $t0.
- j lowerLoopIncrement
- lowerLoopIncrement:
- addi $s0, $s0, 1 # increment $s0 to point to the next element
- j lowerLoop
- lowerLoopEnd:
- la $a0, fString # print "Forward:"
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, string # print the new all uppercase string
- syscall
- addi $v0, $zero, 4
- la $a0, newline # print a newline
- syscall
- la $a0, bString # print "Backward:"
- syscall
- la $a0, newline # print a newline
- syscall
- # BACKWRITER 2 HERE
- la $a0, string # print the string backwards
- syscall
- la $a0, newline # print a newline
- syscall
- la $a0, newline # print a newline
- syscall
- done: # Epilogue for main -- restore stack & frame pointers and return
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore the caller's frame pointer
- addiu $sp, $sp, 24 # restore the caller's stack pointer
- jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement