确定阵列中的阵列是否为回文

时间:2017-11-11 10:59:14

标签: arrays assembly mips32 spim

我正在尝试检查阵列是否是回文。所以首先我在$ s0中取数组的大小,然后我循环用用户输入填充数组。

SPIM提供了一个超出范围的内存"消息,我无法弄清楚原因。有任何想法吗? :&#34)

.data
array: .space 80 # max 20 inputs
palMsg: .asciiz "This array is a palindrome!"
notPalMsg: .asciiz "This array is not a palindrome!"

.text

.globl main
main:

  # take number of elements in array and save value in $s0
  li $v0, 5
  syscall
  add $s0, $v0, $zero

  # initialize a counter $t0 and an offset $t1
  li $t0, 0
  li $t1, 0

  # initialize array with user's inputs
  loop:
  li $v0, 5
  syscall
  addi $t0, $t0, 1
  sw $v0, array($t1)
  addi $t1, $t1, 4
  bne $t0, $s0, loop

  # check if palindrome or not [$t0 goes from left to right and $t1 does the opposite]
  li $t0, 0
  mul $t1, $s0, 4
  addi $t1, $t1, -4
  test:
    lw $t5, array($t0)
    lw $t6, array($t1)
    bne $t5, $t6, notPal
    addi $t0, $t0, 4
    addi $t1, $t1, -4
    blt $t0, $t1, test
  lw $a0, palMsg
  j exit

  notPal:
    lw $a0, notPalMsg
    j exit

  # print msg then terminate
  exit:
    li $v0, 4
    syscall
    li $v0, 10
    syscall

.end

0 个答案:

没有答案