C 中的内联汇编:动态寄存器
我试图在我的 C 代码中内联一些汇编代码:
__asm { mov reg,val };
问题:我想动态定义寄存器和值。 我知道“val”可以是用C代码编写的变量,但我不知道如何动态选择寄存器(即根据用户输入决定寄存器“dh”或“dl”)。
有什么建议吗?
I'm trying to inline some assembly code in my C code:
__asm { mov reg,val };
The problem: I want to define the register and value dynamically.
I know the 'val' can be a variable written in the C code, but I don't know how can I choose the register dynamically (i.e decide according to user input- register 'dh' or 'dl').
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C 代码中使用枚举和 switch:
Use an enum and switch in the C-code:
嗯...这需要您在运行时修改代码。
__asm { }
构造全部发生在编译时,因此您以后无法影响其内容。当然,自修改代码并不完全是现代操作系统的设置目的做起来最简单,因此您将不得不跳过一些环节(缓存刷新、代码位于不可写的段中,等等)。
更新:当然,您也许可以使用slashmais的技术并在一组预编译版本之间
switch
,但我担心在该级别混合代码(想到了寄存器破坏)。Well ... That would require you to modify the code at run-time.
The
__asm { }
construct happens all at compile-time, so you can't affect its contents later.Of course, self-modifying code is not exactly what modern operating systems are set up to do most easy, so you're going to have to jump through a few hoops (cache flushing, code being in non-writable segments, and so on).
Update: Of course you might be able to use slashmais's technique and
switch
between a set of pre-compiled versions, but I'd be scared about mixing code at that level (register clobbering comes to mind).