汇编从C使用标签 [英] Use label in assembly from C

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

问题描述

我只是需要一种方法来例如加载标签的地址MyLabel:在如'src.asm'到例如可变src.c。我使用gcc和NASM组装这些文件(这些文件将一起被链接)。我怎样才能加载标签的地址?

I simply need a way to load the address of a label e.g. MyLabel: in e.g. 'src.asm' into a variable in e.g. 'src.c'. (These files will be linked together) I am using gcc and nasm to assemble these files. How can I load the label address?

推荐答案

有两个步骤来此。首先,你必须在标签从组件文件中使用全球指令导出为全球性的。

There are two steps to this. First, you must export the label as global from the assembly file using the global directive.

global MyLabel

接下来,您必须使用它声明的标签外部的C.你可以做到这一点无论是在code,或头。

Next, you must declare the label as external in C. You can do this either in the code using it, or in a header.

extern void* MyLabel;

最后,你在C标签的地址,你得到任何变量的地址是一样的。

Finally, you get the address of the label in C the same way you get the address of any variable.

doSomethingWith( &MyLabel );

请注意,一些编译器添加下划线到C变量和函数名的开始。例如,GCC这是否在Mac OS X上,而不是Linux操作系统。我不知道其他平台/编译器。为了安全起见,你可以添加一个 ASM 声明的变量声明,告诉GCC什么变量集名称为

Note that some compilers add an underscore to the beginning of C variable and function names. For example, GCC does this on Mac OS X, but not Linux. I don't know about other platforms/compilers. To be on the safe side, you can add an asm statement to the variable declaration to tell GCC what the assembly name for the variable is.

extern void* MyLabel asm("MyLabel");

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

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