3

我正在用 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 使用什么作为回车符。

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

4

2 回答 2

3

嗯,这是一个非常古老的问题,但问题是您正在用空字符替换非字母数字字符,这会在该点终止字符串。

于 2011-03-02T06:10:17.870 回答
0

您可以通过执行以下操作来找出值:

syscall to reading a string
mov first value to $2
check the value of $2 with PC-SPIM or a debugger
于 2010-03-20T10:42:22.423 回答