x86 calling conventions

2023-11-12

原文地址: http://en.wikipedia.org/wiki/X86_calling_conventions

x86 calling conventions

From Wikipedia, the free encyclopedia

This article describes the calling conventions used on x86 architecture chips.

Calling conventions describe the interface of called code:

  • The order in which atomic (scalar) parameters, or individual parts of a complex parameter, are allocated
  • How parameters are passed (pushed on the stack, placed in registers, or a mix of both)
  • Which registers the callee must preserve for the caller
  • How the task of preparing the stack for, and restoring after, a function call is divided between the caller and the callee

This is intimately related with the assignment of sizes and formats to programming-language types. Another closely related topic is name mangling, which determines how symbol names in the code map to symbol names used by the linker. Calling conventions, type representations, and name mangling are all part of what is known as an Application Binary Interface (ABI).

There are often subtle differences in how various compilers implement these conventions, so it is often difficult to interface code which is compiled by different compilers. On the other hand, conventions which are used as an API standard (such as stdcall) are very uniformly implemented.

Historical background[edit]

Prior to microcomputers, the machine manufacturer generally provided an operating system and compilers for several programming languages. The calling conventions adopted for the platform were those defined by the manufacturer's software implementation.

Early microcomputers before Apple II Computers generally came "bare" of an OS or compilers, as did the IBM PC. The only hardware standard for IBM PC compatiblemachines was defined by the Intel processors (8086, 80386) and the literal hardware IBM shipped. Hardware extensions and all software standards (save for a BIOScalling convention) were thrown open to market competition.

A multitude of independent software firms offered operating systems, compilers for many programming languages, and applications. Many different calling schemes were implemented by the firms, often mutually exclusive, based on different requirements, historical practices, and programmer creativity.

After the IBM compatible market shakeout, Microsoft operating systems and programming tools (with differing conventions) predominated, while second tier firms like Borland and Novell, and open source projects like GCC, still maintained their own standards. Provisions for inter-operability between vendors and products were eventually adopted, simplifying the problem of choosing a viable convention.[1]

Caller clean-up[edit]

In these conventions the caller cleans the arguments from the stack, which allows for variable argument lists; e.g., printf().

cdecl[edit]

The cdecl (which stands for C declaration) is a calling convention that originates from the C programming language and is used by many C compilers for the x86 architecture.[1] In cdecl, subroutine arguments are passed on the stack. Integer values and memory addresses are returned in the EAX register, floating point values—in the ST0 x87 register. Registers EAX, ECX, and EDX are caller-saved, and the rest are callee-saved. The x87 floating point registers ST0 to ST7 must be empty (popped or freed) when calling a new function, and ST1 to ST7 must be empty on exiting a function.

In context of the C programming language, function arguments are pushed on the stack in the reverse order. In GNU/LinuxGCC sets the de facto standard for calling conventions. Since GCC version 4.5, the stack must be aligned to a 16-byte boundary when calling a function (previous versions only required a 4-byte alignment.)[citation needed]

Consider the following C source code snippet:

int callee(int, int, int);
 
int caller(void)
{
        register int ret;
 
        ret = callee(1, 2, 3);
        ret += 5;
        return ret;
}

On x86, it will produce the following assembly code (AT&T syntax):

        .globl  caller
caller:
        pushl   %ebp
        movl    %esp,%ebp
        pushl   $3
        pushl   $2
        pushl   $1
        call    callee
        addl    $12,%esp
        addl    $5,%eax
        leave
        ret

The calling function cleans the stack after the function call returns.

There are some variations in the interpretation of cdecl,[2] particularly in how to return values. As a result, x86 programs compiled for different operating system platforms and/or by different compilers can be incompatible, even if they both use the "cdecl" convention and do not call out to the underlying environment. Some compilers return simple data structures with a length of 2 registers or less in the register pair EAX:EDX, and larger structures and class objects requiring special treatment by the exception handler (e.g., a defined constructor, destructor, or assignment) are returned in memory. To pass "in memory", the caller allocates memory and passes a pointer to it as a hidden first parameter; the callee populates the memory and returns the pointer, popping the hidden pointer when returning.

In Linux/GCC, double/floating point values should be pushed on the stack via the x87 pseudo-stack. Like so:

        sub esp, 8      ; make room for the double
        fld [ebp + x]   ; load our double onto the floating point stack
        fstp [esp]      ; push our double onto the stack
        call funct
        add esp, 8

Using this method ensures it is pushed on the stack in the correct format.

The cdecl calling convention is usually the default calling convention for x86 C compilers, although many compilers provide options to automatically change the calling conventions used. To manually define a function to be cdecl, some support the following syntax:

void _cdecl funct();

The _cdecl modifier must be included in the function prototype, and in the function declaration to override any other settings that might be in place.


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

x86 calling conventions 的相关文章

  • cassandra ssdb mongodb

    IM系统 数据量大了mongodb性能有瓶颈 cassandra ssdb 配合使用来搞IM 写扩散 其实是双写 历史消息走cassandra ssdb保留7天的离线消息 cassandra ssdb mongodb
  • 简单的解压缩算法(华为od考试)

    题目描述 现需要实现一种算法 能将一组压缩字符串还原成原始字符串 还原规则如下 1 字符后面加数字N 表示重复字符N次 例如 压缩内容为A3 表示原始字符串为AAA 2 花括号中的字符串加数字N 表示花括号中的字符重复N次 例如压缩内容为
  • 12面魔方公式图解法_【高级篇】(三)三阶魔方CFOP高级玩法之——F2L

    一 F2L这一步要干什么 1 先了解一下 棱角对 和 槽位 的概念 棱角对 即由一个棱块和一个角块构成 是F2L的基本单元 共四组 槽位 给 棱角对 预留的位置 即 棱角对 最后需要插入的地方 棱角对 和 槽位 的概念 2 棱角对 是如何插
  • 在 Android Studio 2.2 中愉快地使用 C/C++

    使用 Android studio 你可以将 C 和 C 代码编译成 native library 然后打包到你的 APK 中 你的 Java 代码可以通过 Java Native Interface JNI 调用 native libra
  • Lite Git (II) - Initialize

    Lite Git II Initialize 前言 本专栏名为Lite Git 主要想与Pro Git对应 后者为Git官方指南 有兴趣 或者想了解更多细节的同学 请移步官网下载PDF版 本专栏主要为了让初出茅庐的同学更快 更合理地掌握Gi
  • RxJS——异步数据流的响应式编程库(适合新手入门)

    文章目录 RxJS概述 Redux VS RxJS RxJS核心概念解析 热观察和冷观察 merge combine合流 RXJS6 的变化 RxJS概述 RxJS 全称 Reactive Extensions for JavaScript
  • 产线发现扣上电池就开机问题分析

    作者 AirCity 2020 3 1 Aircity007 sina com 本文所有权归作者Aircity所有 现象 某SDM630平台手机项目首件 扣上电池 没有按Power键 主板立即开机 问题分析 初步排查开机信号 VBUS信号
  • 前端面试常问题(持续整理中)

    1 js的运行机制 js主要用途是和用户互动 用来操作DOM 所以js是单线程的 避免了同时操作同一个DOM的矛盾问题 2 arguments对象是什么 它是一个类数组对象 它有length属性 但是没有数组的方法 它是如何转化成数组的呢
  • linux操作系统安装时间怎么看,Linux操作系统版本要怎么查看

    Linux操作系统版本要怎么查看 Linux可安装在各种计算机硬件设备中 比如手机 平板电脑 路由器 视频游戏控制台 台式计算机 大型机和超级计算机 下面是小编收集Linux操作系统版本 希望大家认真阅读 1 查看内核版本命令 chen m
  • HUE+LDAP+HIVE,报错:PLAIN auth failed: Error validating LDAP user

    我已经为hue集成了ldap 本次为hive集成ldap认证之后 登录hue后 在hive editor中执行sql语句报如下错误 Bad status 3 PLAIN auth failed Error validating LDAP u

随机推荐

  • Web服务(04)——LAMP的简介与搭建+DISCUZ论坛

    文章目录 LAMP的简介与搭建 DISCUZ论坛 前言 一 LAMP的简介 二 Apache服务 三 LAMP服务的搭建 1 编译安装apache服务 2 编译安装MYSQL服务 3 编译安装PHP服务 四 搭建DISCUZ论坛 总结 LA
  • Datax 的基本操作

    1 Datax 简介 DataX 是一个异构数据源离线同步工具 致力于实现包括关系型数据库 MySQL Oracle等 HDFS Hive ODPS HBase FTP等各种异构数据源之间稳定高效的数据同步功能 开源地址 https git
  • 【LeetCode训练营 189】轮转数组详解

    博客内容 LeetCode训练营 189 轮转数组详解 作 者 陈大大陈 个人简介 一个正在努力学技术的准前端 专注基础和实战分享 欢迎私信 欢迎大家 这里是CSDN 我总结知识和写笔记的地方 喜欢的话请三连 有问题请私信 目录 暴力法 辅
  • 一文看尽深度学习中的15种损失函数

    转自 https zhuanlan zhihu com p 377799012 在机器学习中 损失函数是代价函数的一部分 而代价函数则是目标函数的一种类型 1 Loss function 即损失函数 用于定义单个训练样本与真实值之间的误差
  • 解决PL/SQL 8 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务的问题

    今天晚上貌似遇到的ORACLE11G问题特别多 不过还好 几经尝试 都在网上找到了答案并解决了这些问题 留个备份 PL SQL Developer工具在连oracle11g的时候 碰到了这个问题 ORA 12514 TNS 监听程序当前无法
  • 【C语言数据结构】带头节点与不带头节点的单链表头插法对比

    前言 近期在学习STM32代码框架的过程中 老师使用链表来注册设备 发现使用了不带头节点的单链表 注册时使用头插法 之前在本专题整理学习过带头节点的单链表 因此本文整理对比一下两种方式的头插法区别 具体实现在次 重点在于用以理解两种思路 以
  • Apikit 自学日记:API 异常监控-创建 API 监控

    如何在apikit中 创建 API 监控呢 创建并开启监控API 一 手动创建监控API Eolink API 网络监控平台支持从 Eolink API Management API管理产品 中导入API信息 或者手动创建监控API 进入A
  • (杭电多校)2023“钉耙编程”中国大学生算法设计超级联赛(3)

    1005 Out of Control 先将序列a升序 然后离散化 比如说序列a为1000 1000 500 200 10 然后升序后为10 200 500 1000 1000 映射到从1开始的数 为1 2 3 4 4 此即为前缀最大值序列
  • Flask 学习-70.Flask-RESTX 注册接口实例

    前言 注册接口主要是密码需要加密 用到werkzeug security 模块的2个方法generate password hash check password hash 数据库操作用到Flask SQLAlchemy 相关的基础配置就不
  • 如何利用路由器连接wifi并将wifi网络分发出去

    有时候 我们的wifi网络设置了最大连接数 该数字小于我们的需求数 我们可以考虑利用路由器来进一步分发wifi网络 下面以tp link的产品为例介绍如何分发网络 1 开启路由器 让其发出广播信号 2 使用电脑无线功能连接该路由器 登录路由
  • Vue项目this.$router.push()找不到push问题

    在使用Vue cli开发项目时 我喜欢使用Es6语法 但在使用语法跳路由时却会受到报错 因为Es6的箭头函数的this指向的是最近的一个this 而不是全局的 所以会造成找不到路由方法的问题 解决方法 使用传统语法 XXX function
  • 7-4 多态练习-计算面积

    定义三个类 父类 抽象类 GeometricObject代表几何形状 子类Circle代表圆形 子类Rectangle代表矩形 具体属性和方法如下 父类 抽象类 GeometricObject 属性 private String color
  • 华硕主板BIOS设置虚拟化技(virtualization technology)

    华硕主板BIOS设置虚拟化技 virtualization technology 背景 环境介绍 win安装docker介绍 bios虚拟技术是否开启检查 虚拟技术开启 打开bios设置 虚拟技术设置 参考文献 背景 从2013开始 doc
  • A,NS,cname,forward,txt,aaaa记录讲解

    最近刚好处理DNS问题比较多 对DNS复习一下基础知识 在DNS域名解析中 记录存在很多种 例如主要的A记录 NS记录 CNAME记录 FORWARD记录 还有X记录等等 现在就对它们的区别与联系做总结 1 A记录 A记录又称IP指向 用户
  • 前端面试题总结带答案(持续更新)

    Vue面试题 1 什么是 vue 生命周期 Vue 实例从创建到销毁的过程 就是生命周期 也就是从开始创建 初始化数据 编译模板 挂载Dom 渲染 更新 渲染 卸载等一系列过程 我们称这是 Vue 的生命周期 它可以总共分为8个阶段 创建前
  • 音乐铃声解析提取API接口

    接口地址 https api hackeus cn api kgring 请求协议 HTTP HTTPS 请求方式 GET POST 返回格式 JSON 请求示例 https api hackeus cn api kgring api ke
  • intellij IDEA中我一运行程序,就提示我edit configuration

    因为你直接使用open来打开别人的项目 所以有问题 正确方法 重新打开intellij界面 使用import来导入别人的项目
  • [UnityShader入门精要读书笔记]06.顶点/片元着色器基本结构

    Unity Shader基本结构包含Shader Properties SubShader FallBack等语义块 结构如下 Shader MyShaderName Properties 属性 SubShader 针对显卡A的SubSha
  • GoWeb——处理XML文件

    目录 处理XML文件 1 解析XML文件 2 生成XML文件 处理XML文件 XML eXtensible Markup Language 可扩展标记语言 是一种数据表示格式 可以描述非常复杂的数据结构 常用于传输和存储数据 1 解析XML
  • x86 calling conventions

    原文地址 http en wikipedia org wiki X86 calling conventions x86 calling conventions From Wikipedia the free encyclopedia Thi