我有以下代码test.s
:
call $8 + $8
运行nasm test.s
编译成功.我希望以下等效的Rust代码能够成功编译,但事实并非如此.
在test.rs
:
#![feature(asm)] fn main() { unsafe { asm! ( "call $0 + $0" : : "i" (8) : "memory" : "volatile" ) } }
产量rustc test.rs
:
test.rs:4:9: 10:11 error::1:12: error: invalid token in expression call $8 + $8 ^
Alexandros.. 6
这对我有用:
#![feature(asm)] fn main() { unsafe { asm!( "call ${0:c} + ${0:c}" : : "i"(8) : "memory" : "volatile" ) } }
这是LLVM参考中的相关文档.通过查看输出objdump
,我们可以验证我们的内联汇编已经发出:
0000000000005190 <_ZN4main20hc3048743ecd04f53eaaE>: 5190: e8 7b ae ff ff callq 10 <_ZN10sys_common11thread_info11THREAD_INFO5__KEY20h20efb688859d2c0dRhsE+0x10> 5195: c3 retq 5196: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 519d: 00 00 00
更新:这是从内联汇编直接调用函数的示例:
#![feature(asm)] fn called_even_if_mangled() { println!("Just a regular function minding its own business"); } fn main() { unsafe { asm!( "call ${0:c}" : : "i"(called_even_if_mangled) : "memory" : "volatile", "alignstack" // Omit "alignstack" and you'll get a segfault because of a // misaligned SSE load on some initialization code regarding // stdin. ) } }
但是你永远不应该做这样的事情,除非你有一个非常好的和令人信服的论据(例如,因为你正在写一个JIT).我不得不花一个小时调试一个神秘的段错误,直到我意识到我还必须放入alignstack
选项部分.
你被警告了.
这对我有用:
#![feature(asm)] fn main() { unsafe { asm!( "call ${0:c} + ${0:c}" : : "i"(8) : "memory" : "volatile" ) } }
这是LLVM参考中的相关文档.通过查看输出objdump
,我们可以验证我们的内联汇编已经发出:
0000000000005190 <_ZN4main20hc3048743ecd04f53eaaE>: 5190: e8 7b ae ff ff callq 10 <_ZN10sys_common11thread_info11THREAD_INFO5__KEY20h20efb688859d2c0dRhsE+0x10> 5195: c3 retq 5196: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 519d: 00 00 00
更新:这是从内联汇编直接调用函数的示例:
#![feature(asm)] fn called_even_if_mangled() { println!("Just a regular function minding its own business"); } fn main() { unsafe { asm!( "call ${0:c}" : : "i"(called_even_if_mangled) : "memory" : "volatile", "alignstack" // Omit "alignstack" and you'll get a segfault because of a // misaligned SSE load on some initialization code regarding // stdin. ) } }
但是你永远不应该做这样的事情,除非你有一个非常好的和令人信服的论据(例如,因为你正在写一个JIT).我不得不花一个小时调试一个神秘的段错误,直到我意识到我还必须放入alignstack
选项部分.
你被警告了.