簡體   English   中英

為什么我的數組顯示循環不打印存儲的值? (MIPS組裝)

[英]Why doesn't my array display loop print the stored values? (MIPS assembly)

因此,在弄清楚我的匯編代碼出什么問題的方式上,我幾乎無所適從。 目的是獲取存儲在.data段中的4x4 4字節整數數組,並將其視為矩陣進行轉置。 我能夠很好地訪問和打印存儲在原始數組中的值,但是當我嘗試存儲和打印它們(在標簽地址中-第二個:.space 64)並應用相同的打印邏輯時,它只會給我一個4x4的0網格。 我對匯編語言很陌生,但是我已經多次檢查了我的代碼,看來它應該可以工作。 任何幫助表示贊賞。 這是我的存儲代碼:

.data
strA: .asciiz "Original Array:\n "
strB: .asciiz "Second Array:\n "
newline: .asciiz "\n"
space : .asciiz " "

# This is the start of the original array.
original:   .word 200, 270, 250, 100
            .word 205, 230, 105, 235
            .word 190, 95, 90, 205
            .word 80, 205, 110, 215

Second: .space 64

.align 2
.globl main
.text

main: # Your fully commented program starts here.

li $t1, 0 #offset
la $t2, original
li $t3, 0 #loop counter
li $t4, 16
li $t5, 4 #newline comparator

li $v0, 4 #loads system call to print string and address of string
la $a0, strA
syscall

origLoop:
li $v0, 1
lw $a0, 0($t2) #loads int to $a0
syscall #prints val in $a0
addi $t3, $t3, 1 #increments loop counter
addi $t2, $t2, 4 #advances to next byte
li $v0, 4 #prints array val
la $a0, space
syscall
addi $t1, $t1, 1 #increments newline counter

beq $t1, $t5, printNew #prints newline if $t1 = 4
bne $t3, $t4, origLoop #execs til $t3 =16
li $t6, 4 #newline comparator


#printing done, all registers free

proceed:

li $t1, -1 #outer loop counter
li $t2, 0 #inner loop counter
la $t4, Second #loads address of first element in altered array


outerLoop:
addi $t1, $t1, 1 #increments outer loop counter by 1
add $t0, $t1, $t1 #sets $t0 to four times $t1 (0,4,8,12)
add $t0, $t1, $t1
la $t3, original #loads address of first element in orig array
add $t3, $t3, $t0 #adds (0,4,8,12) to address of first array element
bne $t1, $t6, innerLoop #loops until $t1 = 4
j firstEnd


innerLoop:
lw $t5, 0($t3) #loads element of orig array into $t5
sw $t5, 0($t4) #stores element in $t5 to address $t4
addi $t3, $t3, 16 #advances $t3 to next element in column
addi $t4, $t4, 4 #advances $t4 to next element in new array
addi $t2, $t2, 1 #increments loop counter by 1
bne $t2, $t6, innerLoop #loops until $t1 = 4
li $t2, 0 #resets inner loop count to 0
j outerLoop


printNew:
li $v0, 4
la $a0, newline
syscall
li $t1, 0
bne $t3, $t4, origLoop #execs til $t3 = 16
j proceed

firstEnd:
li $t1, 0 #offset
la $t2, Second
li $t3, 0 #loop counter
li $t4, 16
li $t5, 4 #newline comparator

li $v0, 4 #loads system call to print string and address of string
la $a0, strB
syscall

secondLoop:
li $v0, 1
lw $a0, 0($t2) #loads int to $a0
syscall #prints val in $a0
addi $t3, $t3, 1 #increments loop counter
addi $t2, $t2, 4 #advances to next byte
li $v0, 4 #prints array val
la $a0, space
syscall
addi $t1, $t1, 1 #increments newline counter

beq $t1, $t5, printNew2 #prints newline if $t1 = 4
bne $t3, $t4, secondLoop #execs til $t3 =16

printNew2:
li $v0, 4
la $a0, newline
syscall
li $t1, 0
bne $t3, $t4, secondLoop #execs til $t3 = 16


End:


jr $ra

編輯:解決了問題。 $ t6沒有正確初始化,並且我沒有正確將第一個元素索引計數器乘以4。 但是,所提供的所有幫助都非常感謝!

你試圖加載不存在的符號的地址firstsecond

la $t3, first #loads address of first element in orig array
la $t4, second #loads address of first element in altered array

我猜first應該是originalsecond應該是Second

如果您在SPIM中運行程序,則會出現一個對話框,通知您這些符號未定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM