相当于海湾合作委员会的裸体属性 [英] Equivalent for GCC's naked attribute

查看:139
本文介绍了相当于海湾合作委员会的裸体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有写在纯C的应用程序,与包含纯ASM某些功能混合。裸属性不是可用于x86(为什么?为什么?!),当序幕和尾声与堆栈搞乱我的asm函数不喜欢。是它在某种程度上可以创建能够从C code部分中引用一个纯汇编函数?我只是需要这样的ASM函数的地址。

I've got an application written in pure C, mixed with some functions that contain pure ASM. Naked attribute isn't available for x86 (why? why?!) and my asm functions don't like when prologue and epilogue is messing with the stack. Is it somehow possible to create a pure assembler function that can be referenced from C code parts? I simply need the address of such ASM function.

推荐答案

只需使用 ASM()功能块之外。 ASM()的参数只是被编译器忽略,直接到汇编通过。对于复杂的功能,一个单独的程序源文件是更好的选择,以避免尴尬的语法。

Just use asm() outside a function block. The argument of asm() is simply ignored by the compiler and passed directly on to the assembler. For complex functions a separate assembly source file is the better option to avoid the awkward syntax.

例如:

#include <stdio.h>

asm("_one:              \n\
        movl $1,%eax    \n\
        ret             \n\
");

int one();

int main() {
        printf("result: %d\n", one());
        return 0;
}

PS:请确保你了解你的平台的调用约定。很多时候,你不能只是复制/过去的集会code。

PS: Make sure you understand the calling conventions of your platform. Many times you can not just copy/past assembly code.

PPS:如果您关心性能,使用扩展的asm 代替。扩展的asm实质上内嵌汇编code到C / C ++ code和快很多,尤其是对短期汇编函数。对于较大的组件功能的单独汇编源文件是preferable,所以这个答案是真的,你需要一个函数指针的一个小汇编函数极少数情况下黑客攻击。

PPS: If you care about performance, use extended asm instead. Extended asm essentially inlines the assembly code into your C/C++ code and is much faster, especially for short assembly functions. For larger assembly functions a seperate assembly source file is preferable, so this answer is really a hack for the rare case that you need a function pointer to a small assembly function.

这篇关于相当于海湾合作委员会的裸体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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