How do data types get allocated in stack in MIPS architecture? If i have 2 char and 1 int data, is stack going to allocate them in 8 byte form(2 chars are in same memory segment and 1 int is in another memory segment) or 12 byte form(memory segment for each chars and 1 memory segment for int)? I am trying to understand 32 bit MIPS architecture.
To the question, it matters if the data types being allocated are for local variables vs. for parameter passing.
For locals you can allocate whatever you want as long as the int
is aligned on 4 byte boundary. The total stack allocation is rounded up to 8 bytes (though some don't bother with this, e.g. for homework, and, is only strictly necessary if your function calls other functions that may rely on the expected 8 byte alignment of the stack.)
For parameters you should follow the documented calling convention — there are several so you have to know which you're working with. See here to see some of them; look for "MIPS EABI 32-bit Calling Convention" and/vs. "MIPS O32 32-bit Calling Convention".
What they have in common is that the first four parameters are passed in registers, which effectively means that char
s take a full 32-bit word; char
parameters passed on the stack also follow that form, so take a full 32-bit word each.
In this example data types allocated are for local variables, our professor says regardless of the datatype, 4 bytes of stack space is allocated for each variable in the registers during stack operations but it seems MIPS does not allocate like that, so for this example can i say definitely it will be 8 bytes?
The registers are 32-bits and you can't subdivide them like on other processors, so when we put 8-bit data into a register the other 24-bits are practically unusable for some other purpose (you could find a way but it is usually too awkward to be practical). The stack is different, a
char
does not need more than one byte of the stack, and there is no good reason to give it more unless we're talking parameter passing, where we expanding chars to int size for various reasons.