Dan14021
Dan14021

Reputation: 659

Array of variable length in MIPS assembly language

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.

Upvotes: 2

Views: 5769

Answers (2)

Patrik
Patrik

Reputation: 2691

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

Upvotes: 2

Richard Pennington
Richard Pennington

Reputation: 19965

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.

Upvotes: 2

Related Questions