sparc v8 stack frame

◇◆丶佛笑我妖孽 提交于 2020-02-15 18:47:39

main.c

int enable=1;

int main()
{
    int a, b;
    int sum;
    
    a = 1;
    b = 3;
    
    sum = add(a, b);
    
    return 0;
}

int add(int a, int b)
{
    return (a+b);
}

int del()
{
    return 0;
}
View Code

汇编如下:

main.elf:     file format elf32-sparc

Disassembly of section .text:

70000000 <main>:
70000000:    9d e3 bf 88     save  %sp, -120, %sp
70000004:    90 10 20 01     mov  1, %o0
70000008:    d0 27 bf f4     st  %o0, [ %fp + -12 ]
7000000c:    90 10 20 03     mov  3, %o0
70000010:    d0 27 bf f0     st  %o0, [ %fp + -16 ]
70000014:    d0 07 bf f4     ld  [ %fp + -12 ], %o0
70000018:    d2 07 bf f0     ld  [ %fp + -16 ], %o1
7000001c:    40 00 00 08     call  7000003c <add>
70000020:    01 00 00 00     nop 
70000024:    d0 27 bf ec     st  %o0, [ %fp + -20 ]
70000028:    90 10 20 00     clr  %o0
7000002c:    b0 10 00 08     mov  %o0, %i0
70000030:    01 00 00 00     nop 
70000034:    81 c7 e0 08     ret 
70000038:    81 e8 00 00     restore 

7000003c <add>:
7000003c:    9d e3 bf 98     save  %sp, -104, %sp
70000040:    f0 27 a0 44     st  %i0, [ %fp + 0x44 ]
70000044:    f2 27 a0 48     st  %i1, [ %fp + 0x48 ]
70000048:    f2 07 a0 44     ld  [ %fp + 0x44 ], %i1
7000004c:    f0 07 a0 48     ld  [ %fp + 0x48 ], %i0
70000050:    b0 06 40 18     add  %i1, %i0, %i0
70000054:    01 00 00 00     nop 
70000058:    81 c7 e0 08     ret 
7000005c:    81 e8 00 00     restore 

70000060 <del>:
70000060:    9d e3 bf 98     save  %sp, -104, %sp
70000064:    b0 10 20 00     clr  %i0
70000068:    01 00 00 00     nop 
7000006c:    81 c7 e0 08     ret 
70000070:    81 e8 00 00     restore 
Disassembly of section .data:

70000074 <enable>:
70000074:    00 00 00 01     unimp  0x1
View Code

 

1、3个局部变量,共12字节,按照8字节对齐,需要16字节,即104+16=120

70000000: 9d e3 bf 88 save  %sp, -120, %sp

2、main.c将局部变量a,b分配在%fp + -12,%fp + -16

70000004:    90 10 20 01     mov  1, %o0
70000008:    d0 27 bf f4     st  %o0, [ %fp + -12 ]
7000000c:    90 10 20 03     mov  3, %o0
70000010:    d0 27 bf f0     st  %o0, [ %fp + -16 ]

3、将输入参数放在o0和o1中,然后调用add

70000014:    d0 07 bf f4     ld  [ %fp + -12 ], %o0
70000018:    d2 07 bf f0     ld  [ %fp + -16 ], %o1
7000001c:    40 00 00 08     call  7000003c <add>

4、将局部变量sum,放在%fp + -20,调用结束,从o0取得add的返回值。

70000024:    d0 27 bf ec     st  %o0, [ %fp + -20 ]

5、add将输入参数放在%fp + 0x44,%fp + 0x48,这是为什么?0x44=68, 0x48=72

70000040:    f0 27 a0 44     st  %i0, [ %fp + 0x44 ]
70000044:    f2 27 a0 48     st  %i1, [ %fp + 0x48 ]

 

add函数里的%fp + 0x44感觉又到了main的堆栈里了,这是怎么回事?

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!