请问linux 下的C如何嵌入汇编代码?
问题点数:50、回复次数:15Top
1 楼mengliu(以德服人)回复于 2001-08-02 12:11:21 得分 45
linux中嵌入汇编两种,一种在c中,利用gcc的汇编模板。见linux kernel常用的例子。
另一种写汇编文件编译。gcc编译c函数名是在c函数名前加"_",再汇编中直接加"_".后在c中调用。Top
2 楼sandy_wu()回复于 2001-08-02 13:11:17 得分 0
谢谢MENGLIU, 我是菜LINUX菜鸟,linux kernel常用的例子在那?
我的代码如下该如何改?谢谢!
void fun(long inputVar, long *eax, long *ebx, long *ecx, long *edx)
{
long a, b, c, d;
asm (
"mov eax, inputVar \n\t"
........
"movl a, eax \n\t" "mov b, ebx" \n\t"
"mov c, ecx" \n\t"
"mov d, edx" \n\t"
);
*eax = a;
*ebx = b;
*ecx = c;
*edx = d;
}
why it is not correct?Top
3 楼sandy_wu()回复于 2001-08-02 13:12:33 得分 0
void fun(long inputVar, long *eax, long *ebx, long *ecx, long *edx)
{
long a, b, c, d;
asm (
"mov eax, inputVar \n\t"
........
"movl a, eax \n\t"
"mov b, ebx \n\t"
"mov c, ecx \n\t"
"mov d, edx" \n\t"
);
*eax = a;
*ebx = b;
*ecx = c;
*edx = d;
}
why it is not correct? Top
4 楼keiy()回复于 2001-08-02 13:24:28 得分 5
Linux下ASM 要用摩托罗拉格式,INtel格式不能用
如:
pushl %ebp
movl %esp,%ebp
subl $8,%esp
movl $0,-4(%ebp)
movl -4(%ebp),%eax
addl %eax,-8(%ebp)
Top
5 楼sandy_wu()回复于 2001-08-02 13:37:08 得分 0
to: keiy
那我的那段代码该如何改正? 主要是如何从C传入传出变量?Top
6 楼sandy_wu()回复于 2001-08-02 14:02:56 得分 0
请问那有摩托罗拉格式的资料?Top
7 楼sandy_wu()回复于 2001-08-02 15:08:17 得分 0
up
Top
8 楼mengliu(以德服人)回复于 2001-08-02 15:17:41 得分 0
to : sandy_wu(): 我也不是高手呀。]
“如何从C传入传出变量”,如果是用汇编写函数,参数在堆栈中,如果函数中有自定义变量将堆栈望下降,留出位置。
如果用模板则可以很容易作到。参见“info gcc” 中有详细介绍。
Top
9 楼sandy_wu()回复于 2001-08-03 15:16:45 得分 0
to mengliu:
但我却是超级菜鸟,你就给我详细点说明把,帮我把上面的列子写出来把,谢谢!Top
10 楼Netguy(老家伙)回复于 2001-08-03 15:29:49 得分 0
www.linuxassembly.orgTop
11 楼mengliu(以德服人)回复于 2001-08-03 17:41:57 得分 0
上面有了,
不过我学linux很多是通过info得到的。linux上的info有很多东西呀。在shell下,敲一个info就知到了Top
12 楼sandy_wu()回复于 2001-08-03 21:11:45 得分 0
我将代码改为如下编译连接都通过了但是运行会导致Segmentation fault,请问为何?
我如将"GetCpuidInfo"函数中:"__asm__("cpuid\n\t")去掉就不会导致错误,为何?
代码如下请高手门赐教:
int main(int argc, char* argv[])
{
long reg_eax, reg_ebx, reg_ecx, reg_edx;
printf("before ax is %x\n",reg_eax);
printf("before bx is %x\n",reg_ebx);
printf("before cx is %x\n",reg_ecx);
printf("before dx is %x\n",reg_edx);
GetCpuidInfo(1, ®_eax, ®_ebx, ®_ecx, ®_edx);
printf("after ax is %x\n",reg_eax);
printf("after bx is %x\n",reg_ebx);
printf("after cx is %x\n",reg_ecx);
printf("after dx is %x\n",reg_edx);
getchar();
}
void GetCpuidInfo(long cpuid_levels, long *eax, long *ebx, long *ecx, long *edx)
{
int a,b,c,d;
__asm__("movl 8(%ebp),%eax");
__asm__("cpuid\n\t");
__asm__("movl %eax,-4(%ebp)");
__asm__("movl %ebx,-8(%ebp)");
__asm__("movl %ecx,-12(%ebp)");
__asm__("movl %edx,-16(%ebp)");
*eax = a;
*ebx = b;
*ecx = c;
*edx = d;
}
after run those code, it will case Segmentation fault! ; the output message like:
----------------------------
before ax is bffffb5c
before bx is 78e530f
before cx is 400140d4
before dx is 8048180
after ax is 683
after bx is 2
after cx is 0
after dx is 383f9ff
Segmentation fault (core dumped)
---------------------------------
the result is right, but why it will cause Segmentation fault?
Top
13 楼mengliu(以德服人)回复于 2001-08-05 14:16:47 得分 0
在c程序中,bx,cx和dx都有特定的用处呀。如果干扰了,需要保护这些积存器。
我的是在gcc下编译的!!!
#include <stdio.h>
int GetCpuidInfo(long, long *, long *, long *, long *);
int main(int argc, char* argv[])
{
long reg_eax, reg_ebx, reg_ecx, reg_edx;
printf("before ax is %x\n",reg_eax);
printf("before bx is %x\n",reg_ebx);
printf("before cx is %x\n",reg_ecx);
printf("before dx is %x\n",reg_edx);
GetCpuidInfo(1, ®_eax, ®_ebx, ®_ecx, ®_edx);
printf("after ax is %x\n",reg_eax);
printf("after bx is %x\n",reg_ebx);
printf("after cx is %x\n",reg_ecx);
printf("after dx is %x\n",reg_edx);
}
int GetCpuidInfo(long cpuid_levels, long *eax, long *ebx, long *ecx, long *edx)
{
int a,b,c,d;
__asm__("pushl %%ebx\n\t":);
__asm__("pushl %%ecx\n\t":);
__asm__("pushl %%edx\n\t":);
__asm__("movl %0,%%eax"::"g"(cpuid_levels));
__asm__("cpuid\n\t");
__asm__("movl %%eax,%0\n\t":"=g"(a));
__asm__("movl %%ebx,%0\n\t":"=g"(b));
__asm__("movl %%ebx,%0\n\t":"=g"(c));
__asm__("movl %%ebx,%0\n\t":"=g"(d));
__asm__("popl %%edx\n\t":);
__asm__("popl %%ecx\n\t":);
__asm__("popl %%ebx\n\t":);
*eax = a;
*ebx = b;
*ecx = c;
*edx = d;
}
Top
14 楼mengliu(以德服人)回复于 2001-08-06 08:41:44 得分 0
to:sandy_wu()
对不起,“把上面的列子写出来把”我还以为是“把上面的条子写出来把”Top
15 楼sandy_wu()回复于 2001-08-06 09:55:18 得分 0
mengliu(梦灵): thank U very much !!!!!!!Top




