帮忙看下这段汇编代码,该怎么处理
帮忙看下这段汇编代码
现学现卖,实在是无能为力啊.麻烦各位大牛帮帮忙.
这段代码凡是汇编的地方几乎都有错.
gcc -g spinlock.c main.c -lpthread -I.
spinlock.c: Assembler messages:
spinlock.c:30: Warning: using `%dx' instead of `%edx' due to `w' suffix
spinlock.c:41: Warning: using `%ax' instead of `%eax' due to `w' suffix
spinlock.c:43: Error: operand size mismatch for `xadd'
spinlock.c:68: Warning: using `%ax' instead of `%eax' due to `w' suffix
spinlock.c:72: Error: operand size mismatch for `add'
spinlock.c:87: Error: operand size mismatch for `xadd'
spinlock.c:90: Error: no such instruction: `mv %eax,$-1'
spinlock.c:91: Error: no such instruction: `mv -24(%ebp),%eax'
spinlock.c:125: Error: no such instruction: `mvw %eax,%eax'
spinlock.c:126: Error: no such instruction: `mvw (%edx),%eax'
各位大神指点则个.小的不胜感激啊.
小的可能不在线等哈.不过看到的第一时间,一定回复.
------解决思路----------------------
看cmpw指令应该是PowerPc的指令集,对这个不太了解。
Warning: using `%dx' instead of `%edx' due to `w' suffix 这个貌似应该是30行开始的。
cmpw 指令 比较的应该是32bit的数据,看这个警告,应该是两个操作数有问题。
:"=m" (plock->pid)
:"q" (pid)
第一个冒号是输出操作数,第二个冒号是输入操作数。在这段内联汇编里应该都是做输入才对。
30行和40行两段 中间又没C代码,合并一起会比较好。
参考链接:
内联汇编:http://blog.****.net/liuqiaoyu080512/article/details/8457528
GCC:https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Machine-Constraints.html#Machine-Constraints
PPC汇编入门:http://blog.****.net/skywind/article/details/6347684
仅供参考。
------解决思路----------------------
spinlock.c:43: Error: operand size mismatch for `xadd'
spinlock.c:72: Error: operand size mismatch for `add'
spinlock.c:87: Error: operand size mismatch for `xadd'
这几个是使用xadd,add指令与后面的操作符的运算位数不对应。
很久没用了,忘了具体指令。xaddw 是16位计算,其他的忘了,你对应一下。
spinlock.c:90: Error: no such instruction: `mv %eax,$-1'
spinlock.c:91: Error: no such instruction: `mv -24(%ebp),%eax'
spinlock.c:125: Error: no such instruction: `mvw %eax,%eax'
spinlock.c:126: Error: no such instruction: `mvw (%edx),%eax'
这几个应该是指令使用错误,看看指令有没有写错的。
PS:
__asm__ __volatile__(
"cmpw %1, %0\n"
"jne 1f\n"
"call abort\n"
"1:"
:"=m" (plock->pid)
:"q" (pid)
:"memory", "cc"
);
__asm__ __volatile__(
"1:\n"
"cmpw %1, %0\n"
"jne 1b\n"
"lock xaddw %1, %2\n"
:"=q" (unlock), "=m" (plock->lock)
:"q" (lock)
:"memory", "cc"
);
这两个完全可以放一起写。。。
现学现卖,实在是无能为力啊.麻烦各位大牛帮帮忙.
#include <spinlock.h>
int spinlock_init(PSPINLOCK plock)
{
if (NULL == plock)
{
return RET_FAILED;
}
plock->lock = LOCK_UNLOCKED;
plock->pid = THREAD_DEFAULT;
return RET_SUCCESS;
}
int spinlock_wait(PSPINLOCK plock)
{
int ret = RET_SUCCESS;
lock_t lock = LOCK_LOCKED;
lock_t unlock = LOCK_UNLOCKED;
pthread_t pid = pthread_self();
if (THREAD_INVALID == plock->pid)
{
ret = RET_FAILED;
return ret;
}
//判断是否已经占用了这个锁,如果是,则直接退出,不是则进行下一步
__asm__ __volatile__(
"cmpw %1, %0\n"
"jne 1f\n"
"call abort\n"
"1:"
:"=m" (plock->pid)
:"q" (pid)
:"memory", "cc"
);
__asm__ __volatile__(
"1:\n"
"cmpw %1, %0\n"
"jne 1b\n"
"lock xaddw %1, %2\n"
:"=q" (unlock), "=m" (plock->lock)
:"q" (lock)
:"memory", "cc"
);
plock->pid = pid;
return RET_SUCCESS;
}
int spinlock_trywait(PSPINLOCK plock)
{
int ret = RET_SUCCESS;
pthread_t pid = pthread_self();
lock_t lock = LOCK_LOCKED;
lock_t unlock = LOCK_UNLOCKED;
if (THREAD_INVALID == plock->pid)
{
ret = RET_FAILED;
return ret;
}
__asm__ __volatile__(
"cmpw %1, %0\n"
"je 2f\n"
"jne 1f\n"
"1:\n"
"addw %2, %3\n"
"2:\n"
: "=q" (pid), "=m" (plock->pid),"=m" (ret)
: "ir" (RET_FAILED)
:"memory", "cc"
);
if (RET_FAILED == ret)
{
return ret;
}
__asm__ __volatile__(
"cmp %1, %0\n"
"jne 1f\n"
"lock xaddw %1, %3\n"
"jmp 2f\n"
"1:\n"
"mv %%eax, %4\n"
"mv %2, %%eax\n"
"2:\n"
: "=q" (unlock), "=m" (plock->lock), "=m" (ret)
: "q" (lock), "ir" (RET_FAILED)
: "memory", "cc"
);
if (RET_FAILED == ret)
{
return ret;
}
else
{
plock->pid = pid;
return ret;
}
}
int spinlock_post(PSPINLOCK plock)
{
pthread_t pid = pthread_self();
lock_t unlock = LOCK_UNLOCKED;
lock_t lock = LOCK_LOCKED;
if (pid != plock->pid)
{
return RET_SUCCESS;
}
__asm__ __volatile__(
"cmp %1, %0\n"
"je 1f\n"
"call abort\n"
"1:\n"
"mvw %%eax, %2\n"
"mvw %1, %%eax\n"
:"=q"(lock), "=m" (plock->lock)
:"p" (unlock)
:"memory", "cc"
);
return RET_SUCCESS;
}
int spinlock_destroy(PSPINLOCK plock)
{
if (THREAD_DEFAULT == plock->pid && LOCK_UNLOCKED == plock->lock)
{
plock->pid = THREAD_INVALID;
return RET_SUCCESS;
}
return RET_FAILED;
}
这段代码凡是汇编的地方几乎都有错.
gcc -g spinlock.c main.c -lpthread -I.
spinlock.c: Assembler messages:
spinlock.c:30: Warning: using `%dx' instead of `%edx' due to `w' suffix
spinlock.c:41: Warning: using `%ax' instead of `%eax' due to `w' suffix
spinlock.c:43: Error: operand size mismatch for `xadd'
spinlock.c:68: Warning: using `%ax' instead of `%eax' due to `w' suffix
spinlock.c:72: Error: operand size mismatch for `add'
spinlock.c:87: Error: operand size mismatch for `xadd'
spinlock.c:90: Error: no such instruction: `mv %eax,$-1'
spinlock.c:91: Error: no such instruction: `mv -24(%ebp),%eax'
spinlock.c:125: Error: no such instruction: `mvw %eax,%eax'
spinlock.c:126: Error: no such instruction: `mvw (%edx),%eax'
各位大神指点则个.小的不胜感激啊.
小的可能不在线等哈.不过看到的第一时间,一定回复.
------解决思路----------------------
看cmpw指令应该是PowerPc的指令集,对这个不太了解。
Warning: using `%dx' instead of `%edx' due to `w' suffix 这个貌似应该是30行开始的。
cmpw 指令 比较的应该是32bit的数据,看这个警告,应该是两个操作数有问题。
:"=m" (plock->pid)
:"q" (pid)
第一个冒号是输出操作数,第二个冒号是输入操作数。在这段内联汇编里应该都是做输入才对。
30行和40行两段 中间又没C代码,合并一起会比较好。
参考链接:
内联汇编:http://blog.****.net/liuqiaoyu080512/article/details/8457528
GCC:https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Machine-Constraints.html#Machine-Constraints
PPC汇编入门:http://blog.****.net/skywind/article/details/6347684
仅供参考。
------解决思路----------------------
spinlock.c:43: Error: operand size mismatch for `xadd'
spinlock.c:72: Error: operand size mismatch for `add'
spinlock.c:87: Error: operand size mismatch for `xadd'
这几个是使用xadd,add指令与后面的操作符的运算位数不对应。
很久没用了,忘了具体指令。xaddw 是16位计算,其他的忘了,你对应一下。
spinlock.c:90: Error: no such instruction: `mv %eax,$-1'
spinlock.c:91: Error: no such instruction: `mv -24(%ebp),%eax'
spinlock.c:125: Error: no such instruction: `mvw %eax,%eax'
spinlock.c:126: Error: no such instruction: `mvw (%edx),%eax'
这几个应该是指令使用错误,看看指令有没有写错的。
PS:
__asm__ __volatile__(
"cmpw %1, %0\n"
"jne 1f\n"
"call abort\n"
"1:"
:"=m" (plock->pid)
:"q" (pid)
:"memory", "cc"
);
__asm__ __volatile__(
"1:\n"
"cmpw %1, %0\n"
"jne 1b\n"
"lock xaddw %1, %2\n"
:"=q" (unlock), "=m" (plock->lock)
:"q" (lock)
:"memory", "cc"
);
这两个完全可以放一起写。。。