2

In MIPS, I know that I can declare an array as:

list: .space 20

However, what if I wanted to create an array of different size based on user input? Is this possible?

For example, the program would ask the user to input an integer N and create an array of length N.

2 Answers 2

2

That's a good question. In assembly language, variables declared as you've done are statically allocated, that is they're allocated at assembly time. If you want to allocate a variable based on user input at run time you have at least two choices: Allocate space on the stack (and watch for stack overflow) or allocate from a memory pool, usually called the heap. In either case, the allocation is done at rum time rather than at assembly time.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use system call 9 to allocate memory on the heap

li $a0, numbytes
li $v0, 9
syscall

The address is returned in $v0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.