Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm quite new to the MIPS assembly language and am currently taking a class on computer architecture which has a large section on MIPS coding. I've studied several other high-level programming languages (C, C#, Python) in the past so have some bases in programming.

My question here specifically asks: How does MIPS allocate memory for arrays in the stack? I'm hoping that answering this question will hopefully give me a better total understanding of MIPS as I'm still a bit lot on conceptualizing the idea of the MIPS language and it's architecture. I don't quite understand how pointers work in this whole regard either...

Would be brilliant if someone could take the time to help out this confused student! :)

like image 924
Sam O'Mahony Avatar asked Oct 26 '13 22:10

Sam O'Mahony


People also ask

Can you allocate memory on the stack?

Stack memory allocation takes place on contiguous blocks of memory. As this allocation occurs in the function called stack, it is commonly referred to it as a stack memory allocation. The compiler calculates how much memory to allocate for each type of variable specified in the program.

What is the stack in MIPS?

In MIPS machines, part of main memory is reserved for a stack. — The stack grows downward in terms of memory addresses. — The address of the top element of the stack is stored (by convention) in the “stack pointer” register, $sp. ▪ MIPS does not provide “push” and “pop” instructions.

What is stored in the heap MIPS?

Dynamic data (data allocated during runtime) by the user program is stored in the heap. The stack is used by the user program to store temporary data during for example subroutine calls. Kernel level code (exception and interrupt handlers) are stored in the kernel text segment.


1 Answers

Well.. you should be aware 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
}

MIPS assembly supports all these types of data.

To allocate an int array in the data segment you could 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 could 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 spim simulator this is 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
like image 62
Konrad Lindenbach Avatar answered Oct 14 '22 00:10

Konrad Lindenbach