2016-04-18 24 views
0
.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 

此代码编译,但我有我的输出有一些错误。MIPS Palindrome不关心标点符号

在行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 

,我需要的输出是 amanaplanacanalpanama

在ASCII字符必须添加32从大写去小写 为其=一个示例A = 65所以65 + 32 = 97。

当我试着做我的代码 SUBI $ T5,$ T5相反,32

我与大写值

相同的输出。如果我将其更改为

阿迪$ T5,$ T5,32

输出是

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

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

A■■■■■■■■■■■■■■■■P■■■■■ 

This is not a palindrome 

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

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

+0

我发现如何输出小写字母必须改变dobuild: bgt $ t5,96,makelow #built value byte已经小写了 j更低。但是仍然需要做一个递归函数。 – Jake

回答

0

在ascii字符中,你必须添加32个从大写字母到 的小写字母,例如A = 65所以65 + 32 = 97其中= a。

在这两种情况下,您都会减去32,而不是增加。 addi $t5,$t5, -32(加上-32)与subi $t5,$t5, 32(减去32)相同。

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

因为您将小写字符添加到32位,并且最终显示为不可打印的字符,它们显示为正方形。

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

我看不到test环回。无论如何,你的代码中没有递归,只有循环。递归是指函数自己调用并且代码中没有函数。

+0

所以我的问题仍然存在,我将如何去做递归? – Jake

+0

首先你需要适当的功能。你可以检查[this](http://people.cs.pitt.edu/~xujie/cs447/Mips/sub。html)来解释什么函数/子程序是什么,以及如何实现它们。 – m0skit0