MIPS回文

时间:2016-03-24 15:35:16

标签: mips palindrome uppercase lowercase punctuation

我看到的当前代码如此。如果没有输入标点符号,它可以成功读取字符串是回文。

.data
buffer: .space 80

input:  .asciiz "Enter a string: "
output: .asciiz "Your string: "
paly:   .asciiz "This is a palindrome "
notp:   .asciiz "This is not a palindrome"

.text

main:
    li $v0, 4        # system call code for print_str
    la $a0, input    # address of string to print
    syscall          # print the input

    li $v0, 8        # code for syscall read_string
    la $a0, buffer   # tell syscall where the buffer is
    li $a1, 80       # tell syscall how big the buffer is

    syscall

    la $a0, buffer  # move buffer into a0
    li $v0, 4       # print buffer
    syscall

    la $t1, buffer  # begining of the string
    la $t2, buffer  # end of the string

    li $t0, 0

loop:
    lb   $t3,($t2)  # load the byte of the end of the string
    beqz $t3,endl   # if its equal to 0 then branch out of the loop
    addu $t2, $t2,1 # if in loop the increment to next character
    jal  loop       # repeat the loop

upper:
    addi $t4,$t4,32
    j lowered

lowered:
    addi $t0,$t0,1
    sb   $t4, 0($a0)
    addi $a0,$a0,1
    j loop

endl:
    subu $t2, $t2, 2     # subtracting 2 to move back from \0 and \n

check:
    #lb   $t4, 0($a0)
    #beqz $t4, after
    #beq  $t4, 10,  after
    #slti $t2, $t4, 91
    #li   $t3, 1
    #beq  $t2, $t3, upper

    bge $t1, $t2, palindrome  # if both sides are equal then its a palindrome
                              # call palindrome

    lb $t3, ($t1)             # load the byte into register t3
    lb $t4, ($t2)             # load the end byte into register t4

    bne  $t3, $t4, notpaly    # if the two register bytes are not equal its it not  a palindrome
    addu $t1, $t1, 1          # increment the beginning of the string to next char

    subu $t2, $t2, 1          # decrement end of string to next char to compare
    jal  check                # repeat the loop

palindrome:
    la  $a0, paly   # calling paly from data
    li  $v0, 4      # call for reading string
    syscall
    jal exit        # jump to end

notpaly:
    la  $a0,notp    # calling notp from data
    li  $v0, 4      # call for reading string
    syscall
    jal exit        # jump to end

after:
    li $v0, 4
    la $a0, output
    syscall

    la $a0, buffer
    li $v0, 4
    syscall

exit:

    li $v0 ,10     # call to end program
    syscall        # call os

现在我知道我需要实现代码,例如使大写小写,并删除标点符号。

我已经存储的位被检查:我有一些注释代码,这将是测试一个字符是否为大写,然后跳转到该函数并通过添加32降低它。但它不能正确编译,我假设这是因为我没有正确存储这些位。

#lb   $t4, 0($a0)
#beqz $t4, after
#beq  $t4, 10,  after
#slti $t2, $t4, 91
#li   $t3, 1
#beq  $t2, $t3, upper

0 个答案:

没有答案