I am in the process of writing a program in MIPS that will determine if the string entered by the user is a palindrome. It has three routines that are under development.
Here is the main block of code, the routines that need to be executed with the relevant information:
.data
Buffer: .asciiz " "
intro: .asciiz "Hello, please enter a string of up to 80 characters. I will then tell you if that string was a palindrome!"
.text
main:
li $v0, 4
la $a0, intro
syscall
li $v0, 8
la $a0, Buffer
li $a1, 80
syscall
li $s0, 0
li $t0, 80
la $a0, Buffer
jal stripNonAlpha
li $v0, 4
la $a0, Buffer
syscall
li $s0, 0
jal findEnd
jal toUpperCase
li $v0, 4
la $a0, Buffer
syscall
First, he must remove all non-alphanumeric characters from the string in front of his hand, but when he encounters a character intended to be deleted, all characters are then deleted.
stripNonAlpha:
beq $s0, $t0, stripEnd
add $t4, $s0, $a0
lb $s1, 0($t4)
addi $s0, $s0, 1
slti $t1, $s1, 48
bne $t1, $zero, strip
slti $t1, $s1, 58
slti $t2, $s1, 65
slt $t3, $t1, $t2
bne $t3, $zero, strip
slti $t1, $s1, 91
slti $t2, $s1, 97
slt $t3, $t1, $t2
bne $t3, $zero, strip
slti $t1, $s1, 123
beq $t1, $zero, strip
j stripNonAlpha
strip:
sb $0, 0($t4)
j stripNonAlpha
stripEnd:
la $a0, Buffer
jr $ra
Secondly, it must convert all lowercase letters to uppercase.
toUpperCase:
beq $s0, $s2, upperEnd
add $t4, $s0, $a0
lb $s1, 0($t4)
addi $s1, $s1, 1
slti $t1, $s1, 97
slti $t2, $s1, 123
slt $t3, $t1, $t2
bne $t1, $zero, upper
j toUpperCase
upper:
add $t5, $s0, $a0
addi $t6, $t6, -32
sb $t6, 0($t5)
j toUpperCase
upperEnd:
la $a0, Buffer
jr $ra
, , , . , , PC-SPIM .
, , - , , , .