본문 바로가기

List/Embedded

[mipsel] add(a, b) 분석

소스

#include <stdio.h>


int add ( int a, int b )

{

int c;

c=a+b;

return c;

}


int main ()

{

int a, b, result;

printf("input A:");

scanf("%d", &a);

printf("input B:");

scanf("%d", &b);


result = add(a, b);

printf("%d+%d=%d\n", a,b,result);

}




objdump로 디스어셈블



004006cc <main>:

  4006cc: 27bdffd0 addiu  sp,sp,-48

  4006d0: afbf002c sw ra,44(sp)

  4006d4: afbe0028 sw s8,40(sp)

  4006d8: 03a0f021 move  s8,sp



  4006dc: 3c020040 lui v0,0x40

  4006e0: 24420930 addiu  v0,v0,2352

  4006e4: 00402021 move  a0,v0                            printf("input A:");

  4006e8: 0c100150 jal 400540 <printf@plt>

  4006ec: 00200825 move  at,at


  4006f0: 3c020040 lui v0,0x40

  4006f4: 2443093c addiu  v1,v0,2364

  4006f8: 27c2001c addiu  v0,s8,28

  4006fc: 00602021 move  a0,v1                            scanf("%d", &a);

  400700: 00402821 move  a1,v0

  400704: 0c100154 jal 400550 <__isoc99_scanf@plt>

  400708: 00200825 move  at,at


  40070c: 3c020040 lui v0,0x40

  400710: 24420940 addiu  v0,v0,2368

  400714: 00402021 move  a0,v0                            printf("input B:");

  400718: 0c100150 jal 400540 <printf@plt>

  40071c: 00200825 move  at,at


  400720: 3c020040 lui v0,0x40

  400724: 2443093c addiu  v1,v0,2364

  400728: 27c20020 addiu  v0,s8,32

  40072c: 00602021 move  a0,v1                            scanf("%d", &b);

  400730: 00402821 move  a1,v0

  400734: 0c100154 jal 400550 <__isoc99_scanf@plt>

  400738: 00200825 move at,at


  40073c: 8fc3001c lw  v1,28(s8)

  400740: 8fc20020 lw v0,32(s8)

  400744: 00602021 move  a0,v1

  400748: 00402821 move  a1,v0                            result = add(a, b);

  40074c: 0c1001a4 jal 400690 <add>

  400750: 00200825 move  at,at

  400754: afc20018 sw v0,24(s8)


  400758: 3c020040 lui v0,0x40

  40075c: 2444094c addiu  a0,v0,2380

  400760: 8fc3001c lw v1,28(s8)

  400764: 8fc20020 lw v0,32(s8)                        printf("%d+%d=%d\n", a,b,result);

  400768: 00602821 move a1,v1

  40076c: 00403021 move  a2,v0

  400770: 8fc70018 lw a3,24(s8)

  400774: 0c100150 jal 400540 <printf@plt>

  400778: 00200825 move  at,at



  40077c: 03c0e821 move sp,s8

  400780: 8fbf002c lw ra,44(sp)

  400784: 8fbe0028 lw s8,40(sp)

  400788: 27bd0030 addiu  sp,sp,48

  40078c: 03e00008 jr ra






00400690 <add>:

  400690: 27bdffe8 addiu  sp,sp,-24

  400694: afbe0014 sw s8,20(sp)         => push ebp

  400698: 03a0f021 move  s8,sp              => move esp, ebp(at&t문법)



  40069c: afc40018 sw a0,24(s8)

  4006a0: afc5001c sw a1,28(s8)     => 스택에 매개변수 a, b를 저장

  4006a4: 8fc30018 lw v1,24(s8)

  4006a8: 8fc2001c lw v0,28(s8)      => 스택에서 레지스터로 다시 로드

  4006ac: 00621021 addu v0,v1,v0      => 레지스터끼리 더함

  4006b0: afc20008 sw v0,8(s8)       => 결과를 다시 스택의 지역변수(result)에 저장

  4006b4: 8fc20008 lw v0,8(s8)       => 지역변수(result)에서 레지스터로 다시 저장



  4006b8: 03c0e821 move  sp,s8

  4006bc: 8fbe0014 lw s8,20(sp)

  4006c0: 27bd0018 addiu  sp,sp,24

  4006c4: 03e00008 jr ra