I'm trying to understand how simple C code is translated into assembly MIPS.
int main(){
int a;
a = 9;
return 0;
}
is being translated into
main:
.frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-16
sw $fp,12($sp)
move $fp,$sp
li $2,9 # 0x2
sw $2,0($fp)
move $2,$0
move $sp,$fp
lw $fp,12($sp)
addiu $sp,$sp,16
jr $31
nop
I find a bit weird the use of $fp and $sp: it seems to me that there are unnecessary backups and restore. Probably it is just the way the compiler translates to assembly.
But I just can't understand why it reserves 16 bytes on the stack: 4 for the local variable, 4 for the $fp backup ... and the other 8 bytes? I'm talking about MIPS, I'm using gcc with toolchain mips-mti-elf.
I modified the code, now the variable is global. Guess what? Stack size is 8 bytes (4 for $fp, but the other 4 bytes?).