回文装配计划

时间:2015-04-30 18:45:09

标签: assembly stack mips palindrome

我一直在用Braton的MIPS装配书进行这项练习,我已经完成了整个编码和工作,但无论输入我得到的是“它是一个回文”我一直在盯着它3几个小时,一无所获。任何输入都是有帮助的。

程序概要:从用户那里获取输入的字符串,检查字符串是否是回文。即赛车,鲍勃,汉娜。

########################################################################
# Main program description:
# Get input from user for a string, test the string to see if the string
# entered is a palindrome or not.
#
# Returns: Confirmation or Denial of Palindrome
########################################################################    
    .data
str:    .space  16
prompt: .asciiz "Enter a string: "
yespal: .asciiz "This is a palindrome.\n"
nopal:  .asciiz "This is not a palindrome.\n"


    .text
main:   
    addiu   $sp, $sp, -8    #Allocating space
    li      $v0, 4          #promt user to enter string
    la      $a0, prompt
    syscall
    li      $v0, 8          #read in string
    li      $a1, 16         #string length 16 characters max
    la      $a0, str        #address of string loads into $a0
    syscall 
    jal     pal
    sw      $ra, 4($sp)     #store current value
    sw      $a0, 0($sp)     #store string memory address

    lw      $ra, 4($sp)
    move    $t9, $ra
    addi    $sp, $sp, 8
    bgtz    $t9, yess
    beqz    $t9, nopa

########################################################################
# pal program description:
# Stores and manipulates to pointer and pointer loops with counters
# that are going through the string determining if it is a palindrome
#
# Returns: 1 or 0 for palindrome or not palindrome
########################################################################
pal:    
    la      $t0, str        #load address of string
    addiu   $sp, $sp, -12   #Allocate more space
    sw      $t0, 0($sp)     #store string memory address
    sw      $ra, 4($sp)     #store current value
    jal     nchars          #go to nchars function

    lw      $ra, 4($sp)     #restore printable character value
    lw      $t1, 8($sp)     #move value to register
    lw      $t7, 12($sp)    #return current string value
    addi    $sp, $sp, 12    #release the extra stack

    bge     $t1, $t7, palin
    lbu     $s3, 0($t0)     #pointer 1. the first char
    lbu     $s4, 0($t7)     #pointer 2. the last char
    addi    $t1, $t1, 1     #increase pointer 1 by 1
    addi    $t7, $t7, -1    #decrease pointer 2 by 1
    bne     $t1, $t7, noPal

palin:  
    li      $s8, 1
    sw      $s8, 4($sp)
    jr      $ra

 noPal:  
    li      $s8, 0
    sw      $s8, 4($sp)
    jr      $ra

########################################################################
# nchars program description:
# steps through the string to determine the counter value to pass back
# to the pal function
#
# Returns: value counter to pal function
########################################################################
nchars: 
    li      $t8, 0          #register counter set to 0
    lw      $t9, 0($sp)     #pointer for start of string
 lloop: 
    lb      $s0, 0($t9)     #load current character
    beq     $s0, $0, endln
    addi    $t9, $t9, 1     #move pointer to next character
    addi    $t8, $t8, 1     #add 1 to counter
    j       lloop   
 endln: 
    sw      $t8, 8($sp)     #store counter for return in 8($sp)
    sw      $t9, 12($sp)    #store current value of string
    jr      $ra             #return counter to pal function


yess:   
    li      $v0, 4          #print result for yes
    la      $a0, yespal
    syscall
    li   $v0, 10         
    syscall 

nopa:   
    li      $v0, 4          #print result for no
    la      $a0, nopal
    syscall
    li   $v0, 10         
    syscall 

0 个答案:

没有答案