[英]MIPS Assembly: how to know if user inputted values are correctly stored into an array
[英]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。 但是,所提供的所有帮助都非常感谢!
你试图加载不存在的符号的地址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中运行程序,则会出现一个对话框,通知您这些符号未定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.