免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1781 | 回复: 2
打印 上一主题 下一主题

[C] 请教一个Linux下C语言内嵌汇编程序的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-10 12:04 |只看该作者 |倒序浏览
下面这个函数,必须要带锁,然后做一些比较赋值之类的操作,使用了lock命令:

  1. unsigned int func(unsigned int oval, unsigned int nval, unsigned int *area)
  2. {
  3.     int rv;

  4.     asm volatile(
  5.         "lock;"                               ///////////////问题 - -!
  6.         "cmpl %2, %3;"
  7.         "je .L2;"
  8.         "movl $0, %0;"
  9.         "jmp .L3;"
  10.         ".L2:;"
  11.         "movl $1, %0;"
  12.         "movl %2, %1;"
  13.         ".L3:;"
  14.         : "=$A" (rv), "=r" (*area)
  15.         : "r" (oval), "r" (nval)
  16.         : "%eax"
  17.         );

  18.         return rv;
  19. }

  20. int main() {
  21.     unsigned int oval = 1;
  22.     unsigned int nval = 2;
  23.     unsigned int area = 3;

  24.     func(oval, nval, &area);

  25.     return 0;
  26. }
复制代码


问题出在第一行汇编代码lock,删除这句,程序功能上是没问题的(至少我运行过后结果是正确的),但是加上这一句,就会报错,说是无效的指令: Illegal Instruction.

这是另外一个函数,我看不出使用上有什么区别,但是编译运行就没有问题:

  1. unsigned int atomic_add(unsigned int *area, int val)
  2. {
  3.     asm volatile(
  4.         "lock;"
  5.         "addl %1,%0"
  6.         : "+m" (*area)
  7.         : "ir" (val));
  8.         return *area;
  9. }


复制代码


不明白是为什么,请指教,谢谢。

[ 本帖最后由 lazywrite 于 2009-11-10 12:07 编辑 ]

论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
2 [报告]
发表于 2009-11-10 12:26 |只看该作者

回复 #1 lazywrite 的帖子

看看Intel手册吧
似乎lock不支持cmp

论坛徽章:
0
3 [报告]
发表于 2009-11-10 12:45 |只看该作者
原帖由 openspace 于 2009-11-10 12:26 发表
看看Intel手册吧
似乎lock不支持cmp



The LOCK prefix can be prepended only to the following instructions and only to those
forms of the instructions where the destination operand is a memory operand: ADD,
ADC, AND, BTC, BTR, BTS, CMPXCHG, CMPXCH8B, DEC, INC, NEG, NOT, OR, SBB,
SUB, XOR, XADD, and XCHG.


好像还真是这个原因,非常感谢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP