MIPS

时间:2017-03-23 05:05:51

标签: c++ mips

我需要帮助我在运行我的程序时在QtSimp中发生了id异常9断点的错误。我不知道问题是什么。

我的MIPS编码C ++程序:

#include <iostream>

using namespace std;

int main(void)
{


    int sum = 0;
    for(int i = 1; i <= 100; i++){
        sum = sum + i;
    }

    int num = 45689;
    int rev = 0;
    int d = -1;
    while( num > 0){
        d = num % 10;
        rev = rev*10 + d;
        num = num / 10;
    }

    int arr[] = {1,2,3,4,5,4,3,2,1};
    int beg = 0;
    int end = 8;
    int isPalindrome = 1;
    while(beg < end){
        if (arr[beg] != arr[end]){
            isPalindrome = -1;
            break;
        }
        beg++;
        end--;

    }


    cout << "Sum: " << sum << endl;
    cout << "Reversed Number: " << rev << endl;
    cout << "Is Palindrome: " << isPalindrome << endl;
    return 0;
}

可能存在编码错误,但我无法解决。但是我在QtSimp中一直遇到异常9错误。特别是在数组部分,我认为该部分是错误的。

.data
    endl:    .asciiz  "\n"   # used for cout << endl;
    sumlbl:    .asciiz  "Sum: " # label for sum
    revlbl:    .asciiz  "Reversed Number: " # label for rev
    pallbl:    .asciiz  "Is Palindrome: " # label for isPalindrome
    arr:       .word 1
               .word 2
               .word 3
               .word 4
               .word 5
               .word 4
               .word 3
               .word 2
               .word 1
.text

# sum            --> $s0
# rev            --> $s1
# num            --> $s2
# isPalindrome   --> $s3
# address of arr --> $s4
# i              --> $s5
# beg            --> $s6
# end            --> $s7
# d              --> $t0
# 10             --> $t1
# 100            --> $t3
main:
    li $s0, 0       #int sum = 0;
    li $s5, 1       #int i = 1
    li $t3, 100
loop:
    bge $s5, $t3, loop2        #i <= 100
    add $s0, $s0, $s5          #sum = sum + i
    addi $s5, $s5, 1           #i++
    j loop

    li $s2, 45689   #int num = 45689
    li $s1, 0       #int rev = 0
    li $t0, -1      #int d = -1
    li $t1, 10
    ble $s2, 0, loop3   #( num > 0)
loop2:

    rem $t0, $s2, $t1   #d = num % 10

    mul $t4, $s1, $t1   # store in t4 rev*10
    add $s1, $t4, $t0   #rev = rev*10 + d
    div $s2, $s2, $t1   #num = num / 10
    j loop2

    li $s6, 0       #int beg = 0
    li $s7, 8       #int end = 8
    li $s3, 1       #int isPalindrome = 1
    la $s4, arr     #address for array

    bge $s6,$s7, exit #while(beg < end)

loop3:

    lw $s6, 0
    lw $s7, 8
    sll $t5, $s6, 4
    add $t5, $t5, $s4
    lw $t6, 0($t5)

    sll $t7, $s7, 4
    add $t7, $t7, $s4
    lw $t8, 0($t7)

    ble $t6, $t8,if    # if (arr[beg] != arr[end]){
    li $s3, -1
    j loop3
if:
    addi $s6, $s6, 1
    addi $s7, $s7, -1

0 个答案:

没有答案