简体   繁体   中英

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

So I'm pretty much at a loss in the way of figuring out what is wrong with my assembly code. The intent is to take a 4x4 4-byte integer array stored in the .data segment and treat it like a matrix to transpose it. I am able to access and print the values stored in the original array just fine, but when I try to store and print them (in a label address - Second: .space 64), and apply the same printing logic, it gives me only a 4x4 grid of 0's. I'm very new to assembly, but I've looked over my code multiple times and it seems like it should work. Any help is appreciated. Here is my storing code:

.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

Edit: Got the problem fixed. $t6 wasn't being properly initialized, and I wasn't correctly multiplying by 4 for the first element index counter. All help offered has been much appreciated, though!

You're trying to load the addresses of the non-existent symbols first and second :

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

I'm guessing that first should be original , and second should be Second .

If you ran your program in SPIM you would get a dialog informing you that those symbols are undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM