GCC 内联汇编中的 C 数组?
我正在尝试在 C/x86_64 内联汇编中编写一个函数,但我运气不佳。
我们可以将其归结为:(
void f(const unsigned char *a, unsigned char *b, const unsigned char *c) {
asm __volatile__ ("movdqa %0, %%xmm0" : : "m"(a) : "%xmm0");
}
顺便说一句,该函数被正确调用;我正在尝试替换 C 代码,当我使用现在注释掉的 C 代码时,它工作得很好。)
这会崩溃:
异常类型:EXC_BAD_ACCESS (SIGSEGV)
异常代码:0x000000000000000d、0x0000000000000000
崩溃的线程:0 调度队列:com.apple.main-thread
我已经尝试了相当多的约束组合,但是(正如我所说),我没有太多运气。
目标是能够访问汇编中的所有三个函数参数; a
只读,b
读写,c
只读。
正如你所看到的,这三个都是 C 中的 char 数组;但是,a
和 b
都是 16 个字节长,并且可以分别存储在 XMM 寄存器中(这是我的目标之一)。c
是此类变量的数组,因此每个变量也可以存储在 XMM 寄存器中。
另外,我应该指出,我希望 GCC 不将东西加载到寄存器中(因为它似乎与“x”约束有关),而是将其留给了我。
如果有人能为我写出约束条件,我真的很感激(如果您愿意,请添加一个简短的解释。)
I'm trying to write a function in C/x86_64 inline assembly, but I'm not having much luck.
We can boil it all down to this:
void f(const unsigned char *a, unsigned char *b, const unsigned char *c) {
asm __volatile__ ("movdqa %0, %%xmm0" : : "m"(a) : "%xmm0");
}
(The function is called correctly, by the way; I'm trying to replace C code, and when I use the now-commented out C code, it works just fine.)
This crashes:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: 0x000000000000000d, 0x0000000000000000
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
I've tried quite a few combinations of constraints, but (as I said), I'm not having much luck.
The goal is to have access to all three of the function parameters in assembly; a
read-only, b
read-write and c
read-only.
As you can see, all three are char arrays in C; however, both a
and b
are 16 bytes long and can be stored in an XMM register each (which is one of my goals).c
is an array of such variables, so each of those can also be stored in an XMM register.
Also, I should point out, I'd prefer if GCC didn't load things into registers (as it appears to do with the "x" constraint), but rather it left that to me.
I'd really appriciate if someone could write the constraints for me (and if you feel like it, add a short explanation.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案(如问题评论中所发布)是在传递到 asm 块之前简单地取消引用指针。这样做使一切都按其应有的方式进行。
我只是将其发布为答案,因为我无法仅通过对问题的评论将其标记为已回答。 (另外,我回答的有点晚了,因为昨天它不允许我回答,因为我的代表很低。)
The solution (as posted in the comments to the question) was to simply dereference the pointer before passing in to the asm block. Doing so made everything work as it should.
I'm only posting this as an answer because I can't mark it as answered with only comments to the question. (Also, I'm answering it a bit late because it wouldn't let me yesterday, due to my low rep.)