所以我在弄清楚我的汇编代码有什么问题的时候几乎不知所措。目的是采用存储在.data段中的4x4 4字节整数数组,并将其视为矩阵来转置它。我可以很好地访问和打印存储在原始数组中的值,但是当我尝试存储和打印它们时(在标签地址中 - 第二个:.space 64),并应用相同的打印逻辑,它只给我0的4x4网格。我对组装很新,但我多次查看我的代码,看起来应该可以正常工作。任何帮助表示赞赏。这是我的存储代码:
.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。尽管如此,所有提供的帮助都非常受欢迎!
答案 0 :(得分:0)
您正在尝试加载不存在的符号first
和second
的地址:
la $t3, first #loads address of first element in orig array
la $t4, second #loads address of first element in altered array
我猜测first
应为original
,而second
应为Second
。
如果您在SPIM中运行程序,您将收到一个对话框,通知您这些符号未定义。