Assembly multiple arrays memory allocation

By : Carlos

How to allocate memory in assembly when using multiple arrays. For instance, I have 2 arrays;

la $s0, X
la $s1, Y

if to "initialize" it this way, the memory allocation is contiguous, for example

Address     +0       +4       +8       +12     .....
22112      x[0]     y[0]     x[1]      y[1]    ...

In order to "fix" that, i thought of loading the fist array address initializing n values and then from there initialize the other. For example

   la $t0, X
     fill with $zero until n-1($t0)
   la $s0, X

   la $t1, Y
      fill with $zero until n-1($t1)
   la $s1, Y

This did not work, as it when stating la $s0, X and la $s1, Y it continues to store values contiguously.

I though of other ways of doing it that might work, for example; Filling items contiguously and when reading the value for either array jump 1 memory address, so x[0] = Address 2234 -> x[1] = 2234+8. But that just does not seem as good programming practice.

Can you advise me on which is the correct way, of doing so. Thanks!

By the way, values are always entered, and read in sequence (first x then y)

By : Carlos


I hope I'm not misinterpreting your question, but allocating memory for an array is generally done by a special assembler directive and not through particular instructions. Unfortunately, the syntax varies but the general idea is to ask the assembler to allocate some space. Suppose that the X array needs 100 ints and the Y array 200. Here's how some assemblers do this:

X:   defs  100*4
Y:   defs  200*4

Others might say ".byte" instead of "defs". The "*4" is because you allocate space in bytes but each int is 4 bytes. Sometimes assemblers have a way of saying "allocate space and fill it with some value". What I've presented here won't do that so to be sure you need to now write the initial values. Let's fill X with 1's and Y with 2's:

     la   $t0,X         ; get address of X array into $t0
     mov  $t1,100       ; number of items in X into $t1
     mov  $s0,1         ; All X[] to be filled with 1
xlp: st   $s0,0($t0)    ; write next X[] value
     add  $t0,$t0,4     ; move to next position in X[] array
     add  $t1,$t1,-1    ; count down one less item
     bne  $t1,0,xlp     ; keep doing this until we get to zero

     la   $t0,Y
     mov  $t1,200
     mov  $s0,2
ylp: st   $s0,0($t0)
     add  $t0,$t0,4
     add  $t1,$t1,-1
     bne  $t1,0,ylp

The comments are kinda redundant but I wanted to re-iterate what I'm doing in the likely event that I've forgotten my MIPS assembler mnemonics or have made a mistake.

Allocating the arrays dynamically is quite a different proposition. Normally there will be an operating system subroutine you call to get a pointer to a chunk of memory of a certain size. If you're really at a low level you'll have to come up with your own scheme. Funnily enough, that itself is a matter of declaring a static array that covers all available memory and then handing off chunks of it as the program asks for it. Then you have to get tricky to keep track of what you've handed off so you can free up chunks.

In either case you'll be given a pointer to the amount of memory requested. Normally you'll need to save that pointer in a memory location but it could live in a register for a simple program. The allocation code might look something like this:

X:     word    0

mov    $t0,100*8    ; how much memory we will need
bal    alloc        ; get memory -- assume it returns pointer in $t1
la     $s0,X        ; X pointer address
st     $t1,0($s0)   ; keep track of start of array

Notice how we have to make two steps to get the array address. Before "X" was the address of the memory to use. Now "X" is the address of 4 bytes of memory that contain the address of the array.

The previous initialization code will work as before, but instead of a simple "la $t0,X" you'll have to:

la   $t0,X
l    $t0,0($t0)

If you're familiar with C, the difference here is the same as "int X[100];" vs. "int *X = malloc(100 * sizeof(int));". In both cases you can say "X[n]" but behind the scenes C uses the right assembler sequences.

This video can help you solving your question :)
By: admin