Bryan Romero
Bryan Romero

Reputation: 174

MIPS , printing stored data from array

I want to print the 2 values of data1 with a loop , but im not able to locate the adress of the 2 elements properly and i get an out of bounds error .

When I run the code , the output is (null)(null).

.data



DATA:    .word data1,data2,data3

data1:   .word nome1,cognome1
name1:       .asciiz "john"
surname1:    .asciiz "cena"


data2:    .word nome2,cognome2
name2:       .asciiz "giorno"
surname2:    .asciiz "giovana"


data3:   .word nome3,cognome3
name3:       .asciiz "nikola"
surname3:    .asciiz "tesla"



.text

main:

addi $s0, $zero, 0
li $t5,1              ##counter

while:

la $s1,DATA
la $s2,0($s1)


do:

li $s3,3            ##if s3 = 3 end
beq $s3,$t5,exit

lw $t1,0($s2)       ## print elements in data1
la $a0,0($t2)
li $v0,4
syscall

addi $t5,$t5,1

increaseS1:       ##increase s1 so you can print next data

addi $s2, $s2, 4 

j do

exit:

li $v0,10 # exit
syscall

How can I locate the adress properly without making the code too huge ?, the idea of this programa is to print all the DATA elements one by one by looping and aumenting the indexs.

Upvotes: 0

Views: 1001

Answers (1)

Ped7g
Ped7g

Reputation: 16596

data1: .word nome1,cognome1 <- shouldn't that be data1: .word name1,surname1?

So in DATA you have three pointers on the dataN data, and dataN consist of two pointers to the ascii zero terminated strings.

But the code looks like you only load the pointer to the dataN data (lw $t1,0($s2)), not loading the pointers for strings itself. You have to use that $t1 to load first pointer to name, call the syscall to display it, and then again to load second pointer to surname and display it. Maybe something like lw $t2,0($t1) for name, and lw $t2,2($t1) for surname.

edit: next paragraph of my answer is wrong, now I see what you did there ($t5 being counter, and it's used probably correctly, except not using it for DATA access.. and that would need counting from 0, not from 1).

But it looks like you are using random registers all around without loading them by values, like li $s3,3, looks like $s3 was supposed to be index over DATA, but you don't init it to zero, and you don't use it in lw $t1,0($s2) to fetch *dataN* pointer either.

If you do have some interactive debugger, try your code and see how the registers are filled/used, I think you are copy/pasting the code from different pieces (each using it's own registers), so they will not work together.

You have to decide what to put into which register, and then stick to it in the rest of the code.

Upvotes: 2

Related Questions