1

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)
Community
  • 1
  • 1
compski
  • 709
  • 3
  • 13
  • 28

3 Answers3

3

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.

Community
  • 1
  • 1
Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
2

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • yeah was just curious anyway but never seen the function 'mov' before interesting – compski Jul 22 '13 at 08:09
  • 1
    Ah, I see 'mov' is actually called 'load' and 'store' (lw/lb and sw/sb) for MIPS. 'mov' is the general Intel opcode, for both directions. – Jongware Jul 22 '13 at 09:19
  • By the way, maybe your assembler provides a .repeat pseudo-opcode for data. – Jongware Jul 22 '13 at 09:21
0

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.

john k
  • 6,268
  • 4
  • 55
  • 59