MIPS - How does MIPS allocate memory for arrays on the stack?

I am new to MIPS assembly language, and I am currently working on a computer architecture class that has a large section on MIPS coding. In the past, I studied several other high-level programming languages ​​(C, C #, Python), so you have some basic programming.

My question specifically asks here: how does MIPS allocate memory for arrays on the stack? I hope that the answer to this question, we hope, will give me a better understanding of MIPS, as I am still a little versed in the conceptualization of MIPS and its architecture. I don’t quite understand how pointers work in all this respect ...

It would be great if someone could take the time to help this confused student! :)

+8
assembly malloc mips computer-architecture
source share
1 answer

Well .. you should know that MIPS, like C, essentially has three different ways of allocating memory.

Consider the following C code:

int arr[2]; //global variable, allocated in the data segment int main() { int arr2[2]; //local variable, allocated on the stack int *arr3 = malloc(sizeof(int) * 2); //local variable, allocated on the heap } 

The MIPS assembly supports all of these data types.

To allocate an int array in a data segment, you can use:

 .data arr: .word 0, 0 #enough space for two words, initialized to 0, arr label points to the first element 

To allocate an int array on the stack, you can use:

 #save $ra addi $sp $sp -4 #give 4 bytes to the stack to store the frame pointer sw $fp 0($sp) #store the old frame pointer move $fp $sp #exchange the frame and stack pointers addi $sp $sp -12 #allocate 12 more bytes of storage, 4 for $ra and 8 for our array sw $ra -4($fp) # at this point we have allocated space for our array at the address -8($fp) 

To allocate space on the heap, a system call is required. In the spima simulator, this is a system call 9 :

 li $a0 8 #enough space for two integers li $v0 9 #syscall 9 (sbrk) syscall # address of the allocated space is now in $v0 
+14
source share

All Articles