简体   繁体   中英

How to initialise a huge array in MIPS assembly?

For example, I'm trying to translate a C code into equivalent MIPS:

int a[50];
int i;
...
a[0] = 1;
a[1] = 1;
...

Looking at one of the answers , is there no other way than to do?:

     .data
array: .word  1,1,...,0  (till the 50th zero)

Literal data is not equivalent to your C code. This would be more like

mov [data], 1
mov [data+1], 1

with your .data in the uninitialized BSS section. If you are going this route, make sure to zero the data.

I find nothing wrong, though, with inserting actual literal data. A mere 50 zeros is nothing, although I would not as much as type them, but rather use the power of my text editor. For more random data, I wrote several short programs to convert its binary format into one I could insert into code.

Well, I would say that there is nothing wrong with what you've described. However, it is obviously also possible to use a loop to initialize an array.

initTable:

    la     $t0 table #$t0 stores first address in table
    addi   $t1 $t0 196 #$t1 stores address of one past end (49 * 4)
    addi   $t2 $zero 1

    intiTableLoop:

          sw   $t2 0($t0)
          addi $t0 $t0 4
          blt  $t0 $t1 initTableLoop

   sw $zero 0($t0)

   jr $ra

Using a loop is of course the only way that one could initialize a dynamically allocated array.


I have since discovered from the answer here: MIPS Data Directives that one can do this in mips as so:

array: .word 1:49
       .word 0

Where the number after the colon represents the number of words that should be assigned to the value before the colon. This is probably what you were looking for.

Isn't that what the .space directive is for?

So wouldn't the answer just be

.data 
a: .space 1:50

It creates an array, a, allocates 50 words for it, and stores '1' in each word.

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