gpt4 book ai didi

rust - 使用内联汇编读取 Rust 中 ebx 寄存器的值 ("rbx is used internally by LLVM and cannot be used as an operand for inline asm")

转载 作者:行者123 更新时间:2023-12-05 03:41:09 25 4
gpt4 key购买 nike

我正在编写 Rust 代码,需要获取当前存储在“ebx”寄存器 (x86) 中的值。

我的代码 [0] 如下所示:

#![feature(asm)]

fn main() {
let ebx: u32;
unsafe { asm!("", out("ebx") ebx) };
println!("current value of register ebx is: {}", ebx);
}

rustc 1.54.0-nightly 的错误代码是:

invalid register `ebx`: rbx is used internally by LLVM and cannot be used as an operand for inline asm

我发现的唯一解决方法是传统方式:

unsafe {
llvm_asm!("" : "={ebx}"(ebx) : :);
}

有没有办法用新的 asm! 宏实现所需的功能?

PS:由于我的研究,我发现了这个:https://github.com/rust-lang/rust/pull/84658 .但它仍然没有提供一个好的和简单的解决方案。

[0] https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8d0a322236cce1efbebedccc40ddbf71

最佳答案

您可以将其移动到另一个寄存器。

#![feature(asm)]

fn main() {
let ebx: u32;
unsafe { asm!("mov {:e}, ebx", out(reg) ebx) };
println!("current value of register ebx is: {}", ebx);
}

关于rust - 使用内联汇编读取 Rust 中 ebx 寄存器的值 ("rbx is used internally by LLVM and cannot be used as an operand for inline asm"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67871138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com