After compiling this C code :
int main(void)
{
unsigned int i = 5;
switch(i)
{
case 1: i = i + 3; break;
case 5: i = i<<3; break;
case 10: i = i>>2;
}
}
i have this Assembly MIPS :
.file 1 "SwitchCase.c"
.text
.align 2
.globl main
.ent main
main:
.frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-8
.fmask 0x00000000,0
addiu $sp,$sp,-16
sw $fp,8($sp)
move $fp,$sp
li $2,5 # 0x5
sw $2,0($fp)
lw $2,0($fp)
sw $2,4($fp)
li $2,5 # 0x5
lw $3,4($fp)
beq $3,$2,$L4
lw $3,4($fp)
sltu $2,$3,6
beq $2,$0,$L6
li $2,1 # 0x1
lw $3,4($fp)
beq $3,$2,$L3
j $L2
$L6:
li $2,10 # 0xa
lw $3,4($fp)
beq $3,$2,$L5
j $L2
$L3:
lw $2,0($fp)
addiu $2,$2,3
sw $2,0($fp)
j $L2
$L4:
lw $2,0($fp)
sll $2,$2,3
sw $2,0($fp)
j $L2
$L5:
lw $2,0($fp)
srl $2,$2,2
sw $2,0($fp)
$L2:
move $sp,$fp
lw $fp,8($sp)
addiu $sp,$sp,16
j $31
.end main
I don't really understand this Assembly MIPS, especially the instructions with $sp, $fp register (the purpose of those registers for this code), could someone help me explain this asm?
Judging the actual question from the comments:'"i think that just by having subprogram that we have to use $sp, $fp, but i don't use any subprogram(procedure) here, why this code have $sp, $fp registers ?"
For one thing, main
still needs a stackframe of its own to hold local variables (and temporary, non-named values), so that's why it needs a valid frame-pointer ($fp
).
If you're wondering why it saves the previous frame-pointer, that's because even main
is called by other functions (usually called _start
in some CRT library), which needs to have its frame preserved.
Whether main calls other subroutines in turn is irrelevant, because that doesn't free it from its own need for a stackframe, as above.
Also, it is usually specified as part of a platform's ABI that all functions should set up proper frames, regardlessly of whether they actually need them. This is because a proper frame-chain on the stack is often useful for debuggers and other analysis tools, and it often turns out afterwards to be a pain to lack them in time of need (such as post-mortem debugging of a crashed program). If you really want to, however, you can tell most compilers to omit them in violation of the ABI by use of some compiler-specific flag like -fomit-frame-pointer
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments