-1

我有这个作业问题。

编写一个产生以下序列的程序;1,2,2,4,8, 32, 256,... 并将其存储在数组中,具体取决于用户选择的术语数。序列中的每个元素都可以通过将其前面的两个元素相乘来计算。换言之,第n个序号Sn由等式Sn=Sn-1×Sn-2计算。

我试过但它没有运行

我的代码

^ ^

# UNTITLED PROGRAM

    .data   # Data declaration section
str1: .ascii "Please enter the number of terms to produce: "
arr: .space 40

    .text

main:       # Start of code section


li $v0, 4   # system call code for printing string = 4
la $a0, str1    # load address of string to be printed into $a0
syscall         # call operating system to perform print operation


li $v0, 5   # get ready to read in integers
syscall     # system waits for input
move $s0,$v0    # store the result of the read (returned in $v0) in num1


la $s1,arr
addi $t2,$zero,2    # i=2
addi $t0,$zero,1
add $t1,$t0,$t0
sw $t0,0($s1)
sw $t1,0($s1)

L1:
addi $t2,$t2,1       #i++
addi $s1,$s1,4
lw $t4,0($s1)        #A[i-1]
lw $t5,4($s1)
mul $t3,$t4,$t5
sw $t3,8($s1)
beq $t2,$s0,print 
j L1

print:
lw $t3,0($s1)
li $v0, 1   # system call code for print_int
move $a0, $t3   # integer to print
syscall     # print it
addi $s1,$s1,4
beq $t2,$s0,Exit 
j print


Exit:
li $v0, 10      # exits program
syscall




# END OF PROGRAM
4

1 回答 1

4

MARS 错误信息:

第 26 行错误:0x00400030 处的运行时异常:存储地址未在字边界 0x1001002d 上对齐

错误消息告诉您,您正在尝试在此指令中使用非法(非字对齐)地址访问内存:

sw $t0,0($s1)

当您遇到此类问题时,您需要使用调试器。首先,在抛出异常的指令处设置断点。

brkpt

运行程序,当它在断点处停止时,检查您尝试访问的地址(在 $s1 中)。您会看到它是268501037or 0x1001002d,并且由于它以 7 结尾,因此它不是字对齐的。

注册文件

$s1具有正确的数组地址,但我认为您假设当您在数据段中创建数组时,它将从字对齐地址开始。不是这种情况。要解决此问题,您需要对齐数据。

    .data   # Data declaration section
str1: .ascii "Please enter the number of terms to produce: "
    .align 2
arr: .space 40
于 2012-04-15T23:21:39.380 回答