2012-04-15 75 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 
相關問題