MIPS:从字符串中删除非字母数字字符

时间:2010-03-20 01:59:15

标签: mips

我正在使用MIPS编写程序,该程序将确定用户输入的字符串是否为回文结构。它有三个子程序正在建设中 这是代码的主要块,子程序遵循相关信息:

.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 

首先,它应该预先从字符串中删除所有非字母数字字符,但是当它遇到指定要删除的字符时,之后的所有字符都将被删除。

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

其次,它应该将所有小写字符转换为大写。

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

最后一个子程序,检查字符串是否为回文序列,目前还没有接近完成。我无法找到字符串的结尾,因为我不确定PC-SPIM使用什么作为回车字符。

任何帮助都表示赞赏,我觉得我的大部分问题都来自于愚蠢和愚蠢的事情,所以请随意指出任何事情,无论多么小。

2 个答案:

答案 0 :(得分:3)

呃,这是一个非常古老的问题,但问题是你要用空字符替换非字母数字字符,这会在那时终止字符串。

答案 1 :(得分:0)

你可以通过这样的方式找出价值:

syscall to reading a string
mov first value to $2
check the value of $2 with PC-SPIM or a debugger
相关问题