sparc v8 stack frame calling convention
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
链接:https://pan.baidu.com/s/1T3sNflIAdCUGGcON3-tWpQ
提取码:xkty
复制这段内容后打开百度网盘手机App,操作更方便哦
sparc v8 stack frame calling convention的更多相关文章
- Sparc V8
Sparc V8指令 在sparc V8手册中p83(Table A-1 Mapping of Synthetic Instructions to SPARC Instructions)有合成指令sy ...
- X86调用约定 calling convention
http://zh.wikipedia.org/wiki/X86%E8%B0%83%E7%94%A8%E7%BA%A6%E5%AE%9A 这里描述了在x86芯片架构上的调用约定(calling con ...
- Calling Convention的总结
因为经常需要和不同的Calling Convention打交道,前段时间整理了一下它们之间的区别,如下: 清理堆栈 参数压栈顺序 命名规则 (MSVC++) 备注 Cdecl 调用者 (Caller) ...
- function calling convention
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=31 February 19, 2013 function calling c ...
- Stack frame
http://en.citizendium.org/wiki/Stack_frame In computer science, a stack frame is a memory management ...
- How a stack frame works 栈帧的要素与构建步骤
http://en.citizendium.org/wiki/Stack_frame To use a stack frame, a thread keeps two pointers, often ...
- FUNCTION CALL STACK FRAME
function call stack frame 两个寄存器 esp 栈顶指针寄存器,指向调用栈的栈顶(始终指向,意味着栈分配到哪里了,从当前栈往高地址是已经分配了的) ebp 基址指针寄存器,指向 ...
- 从栈不平衡问题 理解 calling convention
最近在开发的过程中遇到了几个很诡异的问题,造成了栈不平衡从而导致程序崩溃. 经过几经排查发现是和调用规约(calling convention)相关的问题,特此分享出来. 首先,讲一下什么是调用规约. ...
- x86-64栈帧中的“红色区域” red zone of stack frame on x86-64
前几天看System V AMD64 ABI标准的时候发现栈帧的顶部后面有一块"red zone",在学cs:app3e/深入理解操作系统的时候并没有遇到这个,总结一下. 引用标准 ...
随机推荐
- Spring学习记录5——数据库事务基础知识
何为数据库事务 “一荣共荣,一损共损”这句话很能体现事务的思想,很多复杂的事务要分步进行,但它们组成了一个整体,要么整体生效,要么整体失效.这种思想反映到数据库上,就是多条SQL语句,要么全部成功,要 ...
- springcloud复习1
1.SpringCloud是什么?SpringCloud=分布式微服务架构下的一站式解决方案,是各个微服务架构落地技术的集合体,俗称微服务全家桶. 2.SpringCloud和SpringBoot是什 ...
- MOS 常用链接地址
主页面类 Exadata主页面 Exadata Database Machine and Exadata Storage Server Supported Versions (Doc ID 8888 ...
- crontab里的特殊符号%导致命令不能执行
有群里的小伙伴说crontab里的任务不执行,具体是这样的 * * * * /bin/date "+%Y-%m-%d %H:%M:%S" >>/data/tmp/tes ...
- Activiti脚本任务(ScriptTask)
Activiti脚本任务(ScriptTask) 作者:Jesai 你一直问为什么到不了远方,请停下数数你的脚步,是不是还没迈开腿 对于没有接触过groovy脚本语言的人来说,可能比较难使用 应用场景 ...
- 前端开发利器 Web Replay
前端开发人员收到测试发来的 bug 后,通常比较头疼复现的问题. 即使测试人员录了视频,照着一步步操作也不一定能复现,例如bug是与当时的数据相关的. 为了解决这个问题,Firefox 推出了一个重磅 ...
- 个人任务day7
今日计划: 整合程序,排除错误. 昨日成果: 写注册界面.
- 【java面试】算法篇之堆排序
一.堆的概念 堆是一棵顺序存储的完全二叉树.完全二叉树中所有非终端节点的值均不大于(或不小于)其左.右孩子节点的值. 其中每个节点的值小于等于其左.右孩子的值,这样的堆称为小根堆: 其中每个节点的值大 ...
- HUAWEI MateBook Fn 功能键/热键切换、设置方法
原文地址:https://club.huawei.com/thread-13130964-1-1.html HUAWE MateBook E/X/D的F1.F2 等键默认是热键优先.在热键模式下,要想 ...
- openjudge 拯救公主
点击打开题目 看到这道题,第一感觉是我有一句m2p不知当讲不当讲 传送门就算了,你提莫还来宝石,还不给我每种最多有几个~~ 在一般的迷宫问题里,无论已经走了多少步,只要到达同一个点,状态便是等价的,但 ...