C调用约定 [英] C calling conventions

查看:94
本文介绍了C调用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,


最近我一步一步地读了一本名为汇编语言的书.JO

Duntemann。


在linux编写的章节中,他有一段名为C调用约定的
,他描述如下


1 )

a函数必须保留ebx,esp,ebp,esi和edi 32位

寄存器的值。即虽然它可以使用那些寄存器,当它返回

控制它的调用者时,这些寄存器的值必须相同

它们在函数之前的值被称为


2)


所有其他GP寄存器的内容可以随意更改(在linux中

这显然不包括分段寄存器,因为它是一个受保护模式操作系统的



3)

一个程序返回值在EAX中返回。如果它的值是32

位或更小。通过EDX和EAX返回64位整数值,其中

是EAX中的低32位。浮点返回值通过

返回浮点stack.strings结构和大于

位的其他项通过引用返回。即一个函数返回指向

它们在EAX中的指针。


4)

传递给程序的参数被推送到逆序,

即给定C函数(foo,bar,bas); bas首先被推入堆栈,

bar被推到堆栈second.foo最后被推到堆栈上。


5)

过程不会从堆栈中删除参数。程序返回后调用者必须

这样做。


通过弹出程序关闭(更常见的是因为它是

通常更快)


通过向堆栈指针添加偏移量ESP


我的问题是这些语句的正确程度。


请注意,我绝不是专家C程序员。我只想要

对这些事项有一个很好的理解

Hi folks,

recently i read the book named assembly language step by step by Jeff
Duntemann.

in the chapter coding for linux, he has got a paragraph

named C calling conventions, which he describes as follows

1)
a function must preserve the values of ebx,esp,ebp,esi and edi 32 bit
registers. i.e although it may use those registers, when it returns
control to it''s caller,the values of those registers must be the same
values they had before the function was called

2)

the contents all other G.P registers may be altered at will(in linux
this pointedly does not include the segmented registers, because it
being a protected mode operating system)

3)
A procedures return value is returned in EAX. if it''s value is 32
bit or smaller.64 bit integer values are returned via EDX and EAX, with
the low 32 bits in EAX. floating point return values are returned via
floating point stack.strings structures and other items larger than
bits are returned by reference. i.e a function returns a pointer to
them in EAX.

4)
parameters passed to the procedure are pushed in the reverse order,
i.e given the C function(foo,bar,bas); bas pushed onto the stack first,
bar pushed onto the stack second.foo pushed onto the stack last.

5)
procedures do not remove parameters from the stack. the caller must
do that after the procedure returns.

either by popping the procedures Off(more commonly since it is
usually faster)

by adding an offset to the stack pointer ESP

well my question is how far these statements are correct.

please note that i am by no means an expert C programmer.i just want
gain a good understanding on these matters

推荐答案



On 21/06/2005 21:50, aa ***** @ gmail.com 写道:

On 21/06/2005 21:50, aa*****@gmail.com wrote:
嗨伙计们,


[...]
我的问题是这些陈述在多大程度上是正确的。


100%偏离主题。 C语言没有讨论这样的实现

问题。


这些语句似乎适用于Intel i386 ABI(它也是OS

依赖)。我没有检查它们对于任何特定的

一个是对还是错,你应该在你的书中读到它应用的系统,然后得到

一些信息在网络上的那个系统上。

你可以从 http://downloads.openwatcom.org/ftp/devel/docs/

如果你学习装配,英特尔奔腾手册可能对你有所帮助
该机器上的
语言:

http://developer.intel.com/design/Pe...umentation.htm

请注意,我绝不是专家C程序员。我只是想要
Hi folks,

[...]
well my question is how far these statements are correct.
100% off-topic here. The C language doesn''t discuss such implementation
issues.

These statements seem to apply to the Intel i386 ABI (it is also OS
dependant). I didn''t check if they are right or wrong for any particular
one, you should read in your book to which system it applies, and then get
some information on that system on the web.
You can start at http://downloads.openwatcom.org/ftp/devel/docs/

Intel Pentium manuals will probably help you if you learn the assembly
language on that machine:

http://developer.intel.com/design/Pe...umentation.htm

please note that i am by no means an expert C programmer.i just want
gain a good understanding on these matters




您还应该获得C标准的副本;-)


你可能感兴趣的是POSIX标准:

http://www.opengroup.org/onlinepubs/000095399/


不直接与您的问题相关,但即使您在

汇编中编程,您可能会对常见的C函数感兴趣。



You should also get a copy of the C standard then ;-)

Maybe of interest to you is the POSIX standard:

http://www.opengroup.org/onlinepubs/000095399/

Not directly related with your question, but even if you program in
assembly, you may be interested by common C functions.


< a href =mailto:aa ***** @ gmail.com> aa ***** @ gmail.com 写道:
最近我读了一本名为汇编语言的书一步一步由Jeff
Duntemann。

在编写linux的章节中,他有一段名为C调用约定的段落,他描述如下

1)
一个函数必须保留ebx,esp,ebp,esi和edi 32位
寄存器的值。即使它可以使用那些寄存器,当它向它的调用者返回
控制时,这些寄存器的值必须与函数被称为
之前的值相同[snip]好吧,我的问题是这些陈述是多么正确。
recently i read the book named assembly language step by step by Jeff
Duntemann.

in the chapter coding for linux, he has got a paragraph

named C calling conventions, which he describes as follows

1)
a function must preserve the values of ebx,esp,ebp,esi and edi 32 bit
registers. i.e although it may use those registers, when it returns
control to it''s caller,the values of those registers must be the same
values they had before the function was called [snip] well my question is how far these statements are correct.




看起来它特定于x86处理器,可能还有Linux

系统。 C语言本身没有提到关于调用

约定的内容。就语言而言,参数可以在堆栈,寄存器或载体鸽上传递。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/ ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



That looks like it''s specific to x86 processors, and possibly to Linux
systems. The C language itself says nothing about calling
conventions. As far as the language is concerned, parameters can be
passed on the stack, in registers, or by carrier pigeon.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


-----开始PGP签名的消息-----

哈希:SHA1

aa*****@gmail.com 写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

aa*****@gmail.com writes:
最近我一步一步地读了一本名为汇编语言的书杰夫·丹特曼。

在编写linux的章节中,他有一段命名

C调用约定,他描述如下


[...]

我的问题是这些陈述的正确程度。
<请注意,我绝不是专家C程序员。我只是希望
对这些事项有一个很好的理解
recently i read the book named assembly language step by step by
Jeff Duntemann.

in the chapter coding for linux, he has got a paragraph

named C calling conventions, which he describes as follows
[...]
well my question is how far these statements are correct.

please note that i am by no means an expert C programmer.i just want
gain a good understanding on these matters




你最好问在GNU / Linux新闻组中,例如

comp.os.linux.development.system或i386汇编程序组。所有

这是特定于实现和平台的。


另请注意调用约定。也取决于

CPU类型。例如,我正在写这个系统

(powerpc-unknown-linux-gnu)几乎肯定不会使用上面的

约定,但它是一个常规的GNU / Linux系统在各方面都有用。


在用GNU / Linux编写C语言应用程序的六年时间里,我从来没有需要触摸过
汇编程序(虽然我曾经需要查看不同GCC版本的

assember输出来查找GCC错误)。除非你使用Altivec / SIMD指令编写内核内部或高度优化的速度关键的b $ b例程,否则我看不到任何好处。

问候,

Roger


- -

Roger Leigh

在GNU上打印/ Linux呢? http://gimp-print.sourceforge.net/

Debian GNU / Linux http://www.debian.org/

GPG公钥:0x25BFB848。请签名并加密您的邮件。

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.1(GNU / Linux)
评论:由Mailcrypt处理3.5.8< http://mailcrypt.sourceforge.net/>


iD8DBQFCuHYjVcFcaSW / uEgRApbBAKCmoYJzgqjuf79szaYJqt2 + ofDDwQCgmcMa

kWXEFtdEHRFPogyVEeK83a8 =

= N7到

-----结束PGP签名-----



You are better asking in a GNU/Linux newsgroup, such as
comp.os.linux.development.system, or an i386 assembler group. All
this is implementation- and platform-specific.

Also note that the "calling conventions" also vary depending on the
CPU type. For example, the system I''m writing this on
(powerpc-unknown-linux-gnu) almost certainly does not use the above
conventions, but is a regular GNU/Linux system in all respects.

In six years of writing applications in C for GNU/Linux, I''ve never
once needed to touch assembler (though I once needed to check the
assember output of different GCC versions to find a GCC bug). Unless
you are writing kernel internals or highly-optimised speed-critical
routines using Altivec/SIMD instructions, I can''t see any benefit.
Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCuHYjVcFcaSW/uEgRApbBAKCmoYJzgqjuf79szaYJqt2+ofpDwQCgmcMa
kWXEFtdEHRFPogyVEeK83a8=
=N7to
-----END PGP SIGNATURE-----


这篇关于C调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆