asm volatile ("lock; xchg %0,%1" : "=r" (val), "=m" (*ptr) : "m" (*ptr) , "0" (val) );

danmao 2003-03-26 09:37:55
一段嵌入C++的汇编,看不懂。能具体说说吗?
...全文
2373 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
danmao 2003-03-26
  • 打赏
  • 举报
回复
thanks!

但是还是有点不太清楚:
1、
%0---------expressing *ptr variable
%1---------expressing val variable
传入的是ptr,%0为什么不表示ptr

2、"0" (val)什么意思?
3、这个语法("asm statements":spec1:spec2)各部分有什么要求?
hello_wyq 2003-03-26
  • 打赏
  • 举报
回复 2
asm ---expressing asm language
volatile----telling complier not to optimize my asm coding
lock-------asm instruction, expressing synchronous operation
xchg-------asm insturction, expressing to exchange the values in *ptr and val varible
%0---------expressing *ptr variable
%1---------expressing val variable
"r"(val)--------expressing to input val variable into common register in system
"m"(*ptr)------expressing that the data pointed by the pointer of ptr varable are stored in memory
"=r"(val) and "=m"(*ptr)----expressing that the data exchanged will be store val and *ptr variable ,
"m" expresses to use memory to store the data of *ptr variable
"r" expresssed to use common register to store the data of val variable
danmao 2003-03-26
  • 打赏
  • 举报
回复
忘了这是一段宏定义:

#define EXCHANGE(ptr,val) \
{ \
asm volatile ("lock; xchg %0,%1" : "=r" (val), "=m" (*ptr) : "m" (*ptr) , "0" (val) ); \
}
怎么没有人回答?
hello_wyq 2003-03-26
  • 打赏
  • 举报
回复
1)
sorry, I take a mistake
%0---------expressing ptr variable
%1---------expressing val variable
传入的是ptr,%0为什么不表示ptr
2)
when the assembler insturction has a read-write operand, you must logically split its function into two separate operands, one input operand and one write-only output operand.The connection between them is expressed by constrains which say they need to be in the same location when the instruction executes.Various optimizations or reloading could cause operand 0 to be in different register; GNU CC knows no reason not to do so. For example, the compiler might find a copy if the value of "val" in one register and use it for input operand , but generate the output operand in a different register(copying it afterward to "val"'s own address). Of cource, since the register for input operand is not event mentioned in the assembler code, the result will not work, but GNU CC cann't tell that.
sample
the following would not work:
asm volatile ("lock; xchg %0,%1" : "=r" (val), "=m" (*ptr) : "m" (*ptr) , "r" (val) );
3)
asm's structure in gcc:
asm ( assembler template
: output operands (optional)
: input operands (optional)
: list of clobbered registers
(optional)
);


good luck!

21,459

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧