MIPS Palindrome不关心标点符号

时间:2016-04-18 00:06:43

标签: recursion mips

  <schema-selection match-catalog="mybd" match-schema="mybd"/>

此代码编译但我的输出有一些错误。

在addi $ t5,$ t5,-32行,将大写的值设为小写。它使一切都变成大写。

这是测试输出。

.data
input:.space 80 #buffer space for input
built:.space 80 #buffer space for built string from looping

#string values to call when printing
message: .asciiz "Please enter a string:"
newline: .asciiz "\n"
ispaly: .asciiz "\n This is a palindrome"
notpaly: .asciiz "\n This is not a palindrome"

.text
 main:
  la $a0, message # user input message
  li $v0, 4 #mips to print string
  syscall       #call return values

  la $a0, input #users input
  li $a1, 80    #allocating space for the buffer of size 80
  li $v0, 8 #reading the buffer
  syscall       #call return values

secondinput:
    la $t1,built    #creating a new buffer string while going through the   loop
loop:
    lb $t5,($a0)        #loading the byte into the buffer
    beq $t5, 10, check  # if it has a new line
    bgt $t5, 47, digittest  #and has value greater then ascii 47
    jal dontbuild       #if it is not then we increment to next character

digittest:
    blt $t5, 58, dobuild    #proceeding from above, if it is less then ascii   58(digits)
    bgt $t5, 64, capital    #and greater then ascii 64 we dont build it into   our new buffer(built)
    jal dontbuild       #jump to dontbuild function

capital:
    blt $t5, 91, dobuild    #buffer being (built) is less than ascii 91 its a capital letter
    bgt $t5, 96, punctuation #if its greater then 96 its is a lowercase
    j dontbuild     #jump to dontbuild

dobuild:
    bgt $t5, 96, makelow    #built value byte is already lowercase
    j lower         #go to lower function

punctuation:
    blt $t5,123, dobuild    #buffer being built has a value less then 123 and passed previous functions it is a capital and we make it lower
    j dontbuild     #otherwise we dont add to buffer(built) 

makelow:
    addi $t5,$t5, -32   #if value is lower subtract 32 to make it capital
    j lower         #jump to lower function and increment

lower:
    sb $t5,($t1)        #store the byte so it does not get modified
    addi $a0,$a0, 1     #increment for comparison
    addi $t1,$t1,1      #increment for comparison
    j loop          #go back up to loop

dontbuild:
    addi $a0,$a0,1      #increment to next character
    j loop          #jump to loop

check:

    la $t4, built       #buffer that was built
    sb, $zero,($a0)     #store the byte in argument
    addi $t1,$t1,-1     #decrement from the end of our $t1 register

loop2:
    lb $t3,($t4)        #load byte into temporary register
    lb $t2,($t1)        #load byte into temporary register
    beq $t3,$t2,next    #check if each byte is equal
    j notp          #if they are not go to not a palindrome

next:
    jal test    #if they are equal
    addi $t4,$t4,1  #increment through the string
    addi $t1,$t1,-1 #decrement through the string
    j loop2     #go through the loop
j notp          #if it runs into a non equal value its not a palindrome

test:
    beq $t4,$t1,isp     #if all values are equal it calls palindrome
    addi $t1,$t1,-1     
    beq $t4,$t1,isp     #do we need to check any other values
    addi $t1,$t1,1
    jr $ra          #return to os

isp:
    la $a0, input       # the users input
    li $v0, 4
    syscall         #call return values

    li      $v0, 4      #print a new line
    la      $a0, newline
    syscall

    la $a0,built
    li $v0, 4
    syscall

   la $a0,ispaly        #string that it is a palindrome
   syscall          #call return values
   j exit           #end
notp:
    la $a0, input       #users input
    li $v0, 4       
    syscall         #call return values

    li      $v0, 4      #print a new line
    la      $a0, newline
    syscall

    la $a0,built
    li $v0, 4
    syscall

    la $a0, notpaly     #string that it is not a palindrome
    syscall         #call return values
    j exit

exit:
    li $v0, 10      #end program
    syscall         #call return values

我需要的输出是 amanaplanacanalpanama

在ascii字符中,您必须添加32才能从大写变为小写 例如A = 65,所以65 + 32 = 97,其中= a。

当我尝试在我的代码中执行相反的操作时 subi $ t5,$ t5,32

我得到大写值相同的输出

如果我将其更改为

addi $ t5,$ t5,32

输出

Please enter a string:A man, a plan, a canal --Panama!

A man, a plan, a canal --Panama!

AMANAPLANACANALPANAMA

This is a palindrome

基本上每个小写的值都是一个正方形。

最后一个问题是我的测试循环不被视为递归。这是真的吗?

1 个答案:

答案 0 :(得分:0)

  

在ascii字符中,您必须添加32才能从大写字母转到   小写,例如A = 65,所以65 + 32 = 97,= a。

你在两种情况下都减去32,而不是添加。 addi $t5,$t5, -32(添加-32)与subi $t5,$t5, 32相同(减去32)。

  

基本上每个小写的值都是一个正方形。

因为您要为小写字符添加32,并且最终会显示不可打印的字符,这些字符显示为正方形。

  

最后一个问题是我的测试循环不被视为递归。是   这是真的吗?

我不知道test在哪里循环。无论如何,你的代码中没有递归,只有循环。递归是指函数调用自身并且代码中没有函数。