main.c

int enable=;

int main()
{
int a, b;
int sum; a = ;
b = ; sum = add(a, b); return ;
} int add(int a, int b)
{
int x, y;
x = a;
y = b;
return (x+y);
} int del()
{
return ;
}

汇编如下:

main.elf:     file format elf32-sparc

Disassembly of section .text:

 <main>:
: 9d e3 bf save %sp, -, %sp
: mov , %o0
: d0 bf f4 st %o0, [ %fp + - ]
7000000c: mov , %o0
: d0 bf f0 st %o0, [ %fp + - ]
: d0 bf f4 ld [ %fp + - ], %o0
: d2 bf f0 ld [ %fp + - ], %o1
7000001c: call 7000003c <add>
: nop
: d0 bf ec st %o0, [ %fp + - ]
: clr %o0
7000002c: b0 mov %o0, %i0
: nop
: c7 e0 ret
: e8 restore 7000003c <add>:
7000003c: 9d e3 bf save %sp, -, %sp
: f0 a0 st %i0, [ %fp + 0x44 ]
: f2 a0 st %i1, [ %fp + 0x48 ]
: f0 a0 ld [ %fp + 0x44 ], %i0
7000004c: f0 bf f4 st %i0, [ %fp + - ]
: f0 a0 ld [ %fp + 0x48 ], %i0
: f0 bf f0 st %i0, [ %fp + - ]
: f2 bf f4 ld [ %fp + - ], %i1
7000005c: f0 bf f0 ld [ %fp + - ], %i0
: b0 add %i1, %i0, %i0
: nop
: c7 e0 ret
7000006c: e8 restore <del>:
: 9d e3 bf save %sp, -, %sp
: b0 clr %i0
: nop
7000007c: c7 e0 ret
: e8 restore
Disassembly of section .data: <enable>:
: unimp 0x1

混合源程序的反汇编如下:局部变量的定义不会增加程序指令。

main.elf:     file format elf32-sparc

Disassembly of section .text:

 <main>:
int enable=; int main()
{
: 9d e3 bf save %sp, -, %sp
int a, b;
int sum; a = ;
: mov , %o0
: d0 bf f4 st %o0, [ %fp + - ]
b = ;
7000000c: mov , %o0
: d0 bf f0 st %o0, [ %fp + - ] sum = add(a, b);
: d0 bf f4 ld [ %fp + - ], %o0
: d2 bf f0 ld [ %fp + - ], %o1
7000001c: call 7000003c <add>
: nop
: d0 bf ec st %o0, [ %fp + - ] return ;
: clr %o0
}
7000002c: b0 mov %o0, %i0
: nop
: c7 e0 ret
: e8 restore 7000003c <add>: int add(int a, int b)
{
7000003c: 9d e3 bf save %sp, -, %sp
: f0 a0 st %i0, [ %fp + 0x44 ]
: f2 a0 st %i1, [ %fp + 0x48 ]
int x, y;
x = a;
: f0 a0 ld [ %fp + 0x44 ], %i0
7000004c: f0 bf f4 st %i0, [ %fp + - ]
y = b;
: f0 a0 ld [ %fp + 0x48 ], %i0
: f0 bf f0 st %i0, [ %fp + - ]
return (x+y);
: f2 bf f4 ld [ %fp + - ], %i1
7000005c: f0 bf f0 ld [ %fp + - ], %i0
: b0 add %i1, %i0, %i0
}
: nop
: c7 e0 ret
7000006c: e8 restore <del>: int del()
{
: 9d e3 bf save %sp, -, %sp
return ;
: b0 clr %i0
}
: nop
7000007c: c7 e0 ret
: e8 restore

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

: 9d e3 bf  save  %sp, -, %sp

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

:            mov  , %o0
: d0 bf f4 st %o0, [ %fp + - ]
7000000c: mov , %o0
: d0 bf f0 st %o0, [ %fp + - ]

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

:    d0  bf f4     ld  [ %fp + - ], %o0
: d2 bf f0 ld [ %fp + - ], %o1
7000001c: call 7000003c <add>

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

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

5、add现将i0和i1放在%fp + 0x44,%fp + 0x48,这是为什么?0x44=68, 0x48=72

:    f0  a0      st  %i0, [ %fp + 0x44 ]
: f2 a0 st %i1, [ %fp + 0x48 ]

6、然后,赋值局部变量

:    f0  a0      ld  [ %fp + 0x44 ], %i0
7000004c: f0 bf f4 st %i0, [ %fp + - ]
: f0 a0 ld [ %fp + 0x48 ], %i0
: f0 bf f0 st %i0, [ %fp + - ]

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

The caller’s stack pointer %sp (%o6) automatically becomes the current procedure’s frame pointer %fp (%i6) when the SAVE instruction is executed.

SAVE同时也具有ADD的效果。

3803手册中,%fp + 0x44位置处写着,用于存储被调函数的寄存器变量,也就是add函数。在调用函数的堆栈中,为什么要存储被调函数的东西呢?见下面分析

System V Application Binary Interface - SPARC Processor Supplement -3dt,p24描述如下:

Although the first 6 words of arguments reside in registers, the standard stack frame reserves space
for them. ‘‘Coding Examples’’ below explains how these words may be used to implement variable
argument lists. Arguments beyond the sixth reside on the stack.

虽然前6个参数放在寄存器中,但是标准的栈帧也给它们预留了空间。

下面图来自abi_sparc,p24。

其中previous帧中说six words into which function may write incoming arguments 0 to 5,这是里may可能,调用函数可能将前6个输入参数既放入i0~i5,又放在这里。

current帧中说six words into which callee may write outgoing arguments 0 to 5,这是里may可能,被调函数可能将前6个输出参数既放入i0~i5,又放在这里。

但从实际sparc-elf-gcc的使用来看,被调函数做了调用函数的工作,即被调函数将输入参数i0~i5放在了调用函数栈帧的该位置处。

reference:

SYSTEM V APPLICATION BINARY INTERFACE, SPARC Processor Supplement, Third Edition

链接:https://pan.baidu.com/s/1lRCz7Z0nol-8gOzed3Rmyw
提取码:0kjr

BM3803MGRH用户手册

链接:https://pan.baidu.com/s/1T3sNflIAdCUGGcON3-tWpQ
提取码:xkty
复制这段内容后打开百度网盘手机App,操作更方便哦

sparc v8 stack frame calling convention的更多相关文章

  1. Sparc V8

    Sparc V8指令 在sparc V8手册中p83(Table A-1 Mapping of Synthetic Instructions to SPARC Instructions)有合成指令sy ...

  2. X86调用约定 calling convention

    http://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A 这里描述了在x86芯片架构上的调用约定(calling con ...

  3. Calling Convention的总结

    因为经常需要和不同的Calling Convention打交道,前段时间整理了一下它们之间的区别,如下: 清理堆栈 参数压栈顺序 命名规则 (MSVC++) 备注 Cdecl 调用者 (Caller) ...

  4. function calling convention

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31 February 19, 2013 function calling c ...

  5. Stack frame

    http://en.citizendium.org/wiki/Stack_frame In computer science, a stack frame is a memory management ...

  6. How a stack frame works 栈帧的要素与构建步骤

    http://en.citizendium.org/wiki/Stack_frame To use a stack frame, a thread keeps two pointers, often ...

  7. FUNCTION CALL STACK FRAME

    function call stack frame 两个寄存器 esp 栈顶指针寄存器,指向调用栈的栈顶(始终指向,意味着栈分配到哪里了,从当前栈往高地址是已经分配了的) ebp 基址指针寄存器,指向 ...

  8. 从栈不平衡问题 理解 calling convention

    最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...

  9. x86-64栈帧中的“红色区域” red zone of stack frame on x86-64

    前几天看System V AMD64 ABI标准的时候发现栈帧的顶部后面有一块"red zone",在学cs:app3e/深入理解操作系统的时候并没有遇到这个,总结一下. 引用标准 ...

随机推荐

  1. nacos-docker安装nacos并配置数据库

    拉取nacos/nacos-server镜像 docker pull nacos/nacos-server 配置数据库(MySQL) 创建存储nacos配置的数据库 create database n ...

  2. Python解析json字符串,json字符串用法

    json数据简介 json数据是一个轻量级的数据交换格式,采用完全独立于语言的文本格式,这些特性使json称为理想的数据交换语言,易于人阅读和编写,同时易于机器解析和生成. json中的字符集必须是U ...

  3. nginx介绍与安装

    1.nginx作用可以配置数十个场景 ​ ​ 2.环境安装 环境确认 ​ 安装环境 yum -y install gcc gcc-c++ autoconf pcre-devel make automa ...

  4. 3种基础的 REST 安全机制

    安全是 RESTful web service 的基石,我们主要讨论以下3种主要的方法: Basic authentication Oauth 2.0 Oauth 2.0 + JWT 1. Basic ...

  5. Java入门 - 语言基础 - 14.String类

    原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...

  6. 我的开源权限管理项目BeCore (基于.net core开发)

    首先 谢谢大家还记得我.. 新年快乐 祝大家工作顺利 事事顺心 人见人爱 车见车载 冬不寒 下雨有伞 全身哪都不疼 就是有人疼 ~~ Github地址:https://github.com/baby8 ...

  7. Python-直接存储类实例作为序列的元素

    如果我们需要存储的数据有很多属性,并且存储的数量很多,可选择定义一个类来表示数据类型,而类的实体作为单个的成员进行存储,这样做的好处是可以只存储一个容器,而不需要每次都存储大量的数据,并且可以限制对数 ...

  8. 对于kvm配置ssh

    首先我们要让自己的机器开启ssh服务 首先更新源 sudo apt-get update 安装ssh服务 sudo apt-get install openssh-server 检测是否已启动 ps ...

  9. selenium等待方式之显示等待

    有时候,页面元素并未及时加载出来导致后面的步骤无法执行 这里就需要在加载前添加等待时间,让目标元素有足够的时间加载出来 第一种方法:使用time.sleep() 这种方法过于强制,无论元素是否加载出来 ...

  10. GitHub Top 微信小程序——在家中憋了几天写点代码吧

    GitHub Top 本项目为 GitHub 热点项目微信小程序客户端,首页仅推荐一个热点项目,这个项目往往是社会热门事件所催生的一个项目,如 996.ICU.wuhan2020,所推荐项目标准为:积 ...