C to Assembly MIPS by Cross Compiler (need explain $sp, $fp)

VAK5d6

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?

Dolda2000

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

what is "sw $fp($sp)" mean in MIPS

From Dev

C to MIPS Assembly

From Dev

C to MIPS Assembly

From Dev

Need help solving this loop in MIPS(assembly)

From Dev

MIPS - Call C function in Assembly code

From Dev

Compile C to MIPS Assembly in 7 instructions?

From Dev

Translating single C line to MIPS Assembly

From Dev

Converting C code to MIPS Assembly Language

From Dev

Converting C++ function to MIPS assembly

From Dev

Converting C to mips assembly, Unaligned address error

From Dev

C51 C compiler inline assembly to SDCC inline assembly

From Dev

Does a compiler that compiles to machine code need to be written in assembly?

From Dev

Seeking help regarding installing and using mips-gcc cross compiler to generate custom ASM

From Dev

C Cross compiler links extern declared as static?

From Dev

Converting from C Loop to MIPS assembly language (bad address error)

From Dev

Is this MIPS assembly code converted from C code correct?

From Dev

sorting array in mips (assembly)

From Dev

string length for mips assembly

From Dev

MIPS Assembly Labels

From Dev

MIPS assembly string array

From Dev

Tribonacci in Mips Assembly

From Dev

BNE branch in MIPS assembly

From Dev

string length for mips assembly

From Dev

MIPS assembly string array

From Dev

understanding MIPS assembly with pipelining

From Dev

MIPS Assembly, matrix multiplication

From Dev

MIPS Assembly - if condition with substraction

From Dev

In MIPS Assembly, why is it important to save and restore saved registers that need to be used by a procedure?

From Dev

MIPS Compiler local variables