2012-04-15 79 views
-1

我有这个家庭作业问题。如何将序列存储在MIPS中的数组中?

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

我试过,但它并没有跑

我的代码

^^

# 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 
+0

你做了什么调试工作?你认为问题在哪里? – 2012-04-15 22:35:48

+1

请格式化您的代码。 – blackcompe 2012-04-15 22:36:55

+0

@MichaelPetrotta我用栈来保存结果然后在数组中排序..但是我的代码没有运行..最后,编码程序__ for循环=> A [i] = A [i-1] * A [i -2] 我认为问题如何从数组中加载字和排序字指令 – Dalal 2012-04-15 23:10:42

回答

3

MARS错误消息:在第26行

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

错误消息告诉你,你试图在该指令与非法(非字对齐)地址访问内存:

sw $t0,0($s1) 

当你有这样的问题,你需要使用调试器。首先,在抛出异常的指令处设置一个断点。

brkpt

运行程序,当它停靠在破发点,检查(在$ S1)您尝试访问的地址。你会看到它是2685010370x1001002d,并且由于它以7结尾,所以它不是字对齐的。

regfile

$s1有正确的数组的地址,但我认为你假定当您创建的数据段的阵列,它将在一个字对齐的地址开始。不是这种情况。要解决这个问题,你需要​​的数据。

.data # Data declaration section 
str1: .ascii "Please enter the number of terms to produce: " 
    .align 2 
arr: .space 40 
相关问题