Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MIPS: removing non alpha-numeric characters from a string

Tags:

mips

I'm in the process of writing a program in MIPS that will determine whether or not a user entered string is a palindrome. It has three subroutines which are under construction.
Here is the main block of code, subroutines to follow with relevant info:

.data
Buffer: .asciiz "                                                                                "  # 80 bytes in Buffer
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      # print_string call number
    la  $a0, intro  # pointer to string in memory
    syscall
    li  $v0, 8      #syscall code for reading string
    la  $a0, Buffer #save read string into buffer
    li  $a1, 80     #string is 80 bytes long
    syscall
    li  $s0, 0      #i = 0
    li  $t0, 80     #max for i to reach
    la  $a0, Buffer
    jal stripNonAlpha
    li  $v0, 4      # print_string call number
    la  $a0, Buffer # pointer to string in memory
    syscall
    li  $s0, 0
    jal findEnd
    jal toUpperCase
    li  $v0, 4      # print_string call number
    la  $a0, Buffer # pointer to string in memory
    syscall 

Firstly, it's supposed to remove all non alpha-numeric characters from the string before hand, but when it encounters a character designated for removal, all characters after that are removed.

stripNonAlpha:
    beq $s0, $t0, stripEnd  #if i = 80 end
    add $t4, $s0, $a0       #address of Buffer[i] in $t4
    lb  $s1, 0($t4)     #load value of Buffer[i]
    addi    $s0, $s0, 1     #i = i + 1
    slti    $t1, $s1, 48        #if ascii code is less than 48
    bne $t1, $zero, strip   #remove ascii character
    slti    $t1, $s1, 58        #if ascii code is greater than 57
                    #and
    slti    $t2, $s1, 65        #if ascii code is less than 65
    slt $t3, $t1, $t2       
    bne $t3, $zero, strip   #remove ascii character
    slti    $t1, $s1, 91        #if ascii code is greater than 90
                    #and
    slti    $t2, $s1, 97        #if ascii code is less than 97
    slt $t3, $t1, $t2
    bne $t3, $zero, strip   #remove ascii character
    slti    $t1, $s1, 123       #if ascii character is greater than 122
    beq $t1, $zero, strip   #remove ascii character
    j   stripNonAlpha       #go to stripNonAlpha
strip:
    #add    $t5, $s0, $a0       #address of Buffer[i] in $t5
    sb  $0, 0($t4)      #Buffer[i] = 0
    #addi   $s0, $s0, 1     #i = i + 1
    j   stripNonAlpha       #go to stripNonAlpha
stripEnd:
    la  $a0, Buffer     #save modified string into buffer
    jr  $ra         #return

Secondly, it is supposed to convert all lowercase characters to uppercase.

toUpperCase:
    beq     $s0, $s2, upperEnd
    add $t4, $s0, $a0
    lb  $s1, 0($t4)
    addi    $s1, $s1, 1
    slti    $t1, $s1, 97
    #beq    $t1, $zero, upper
    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

The final subroutine, which checks if the string is a palindrome isn't anywhere near complete at the moment. I'm having trouble finding the end of the string because I'm not sure what PC-SPIM uses as the carriage return character.

Any help is appreciated, I have the feeling most of my problems result from something silly and stupid so feel free to point out anything, no matter how small.

like image 959
Kron Avatar asked Feb 13 '26 18:02

Kron


1 Answers

Uh, this is a really old question, but the problem is that you're replacing the non-alphanumeric characters with a null character, which terminates the string at that point.

like image 118
Kalphin Avatar answered Feb 21 '26 14:02

Kalphin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!