Building memory with multiple arrays

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

la $s0, X
la $s1, Y

if you “initialize” it in this way, the allocation of memory is contiguous, for example

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

To "fix", I thought about loading the address of the fist array, initializing the values n, and then from there initialized another. for instance

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

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

This did not work, as when indicated la $s0, Xand la $s1, Yit continues to store values ​​contiguously.

I mean other ways to do this, for example, to work; Filling elements is contiguous when reading the values ​​for the memory address of array 1, so x [0] = Address 2234 → x [1] = 2234 + 8. But this does not seem like good programming practice.

, , . !


, ( , )

+3
1

, , , . , , , . , X 100 int Y- 200. :

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

".byte" "defs". "* 4" - , , int 4 . " ". , , , , . X 1 Y 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

, , , Mnemonics MIPS .

- . , , . , . , , , , . , , , .

. , . :

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

, , . , "X" . "X" - 4 , .

-, "la $t0, X" :

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

C, , "int X [100];" vs. "int * X = malloc (100 * sizeof (int));". "X [n]", C .

+2

Source: https://habr.com/ru/post/1772969/


All Articles