Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3321900
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:07:07+00:00 2026-05-17T23:07:07+00:00

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

  • 0

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

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 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:07:07+00:00Added an answer on May 17, 2026 at 11:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.