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 Boot2 系列教程(二十) | SpringBoot 是如何实现日志的?
微信公众号:一个优秀的废人.如有问题,请后台留言,反正我也不会听. 前言 休息日闲着无聊看了下 SpringBoot 中的日志实现,把我的理解跟大家说下. 门面模式 说到日志框架不得不说门面模式.门面 ...
- Openstack之七:实现基于桥接的内外网络
一.在控制端进行配置网络 #启动实例文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/launch-instance.html# ...
- 01-web自动化基础篇
1.搭建环境 需要的环境 浏览器(Firefox/Chrome/IE…)-----选择Chrome Python Selenium Selenium IDE(如果用Firefox) FireBug.F ...
- ORM基础1
1.增删改查 .models.类.object.all() 获取所有对象->select * from 表 2.models.类.object.get(id=1) 获取id为1的对象->s ...
- 异数OS 织梦师-水桶(三)-- RAM共享存储方案
. 异数OS 织梦师-水桶(三)– RAM共享存储方案 本文来自异数OS社区 github: https://github.com/yds086/HereticOS 异数OS社区QQ群: 652455 ...
- js多图预览及上传功能
<%-- Created by IntelliJ IDEA. User: Old Zhang Date: 2018/12/27 Time: 11:17 To change this templa ...
- Oracle GoldenGate Best Practices: Active-Active Configuration with DML Auto CDR
Executive Overview This document is an introduction to Oracle GoldenGate (DIPC remote agent)’s best ...
- 我的第一个 Kaggle 比赛学习 - Titanic
背景 Titanic: Machine Learning from Disaster - Kaggle 2 年前就被推荐照着这个比赛做一下,结果我打开这个页面便蒙了,完全不知道该如何下手. 两年后,再 ...
- Html中div块居中显示
表面上这个问题很难,因为涉及到浏览器窗体大小,导致部分界面效果不一致.图中的方法适用于div块大小不变的界面. 如上所示,将其分为两块,margin-left和margin-top的值均分别为widt ...
- .net core ef动态orderby
前言 最近在给大家写一套开源的.net core权限管理框架.现在已经写到前台UI + 后台动态查询的部分. 发现需要动态orderby但是网上没有现成的例子 二话不说上代码 建议namespace ...