简体   繁体   English

内联汇编-使用printf以十进制显示寄存器吗?

[英]Inline Assembly - Display a register in decimal using printf?

I just had a really quick question that I saw someone mention something about in another question, but I didn't want to necro-post on it. 我刚刚提出了一个非常快速的问题,我看到有人在另一个问题中提到了一些话题,但是我不想对此进行死补。

I'm coding in inline assembly with c++, and need to display a register value in decimal. 我正在使用C ++进行内联汇编编码,并且需要以十进制显示寄存器值。 I was searching ways to do this, and saw someone mention "If you're using inline c, just call printf." 我正在寻找实现此目的的方法,并且看到有人提到“如果您使用内联c,请致电printf。” But they didn't go much further into explanation on it than that. 但是他们对此没有做更多的解释。

Is it possible the call printf can be used to get a register value in decimal format without needing to write a conversion section of the code? 是否可以使用调用printf来获取十进制格式的寄存器值,而无需编写代码的转换部分? And if so, how would that work? 如果是这样,那将如何工作? Say after some computations to a user entered integer, the value now lies in the AX register. 对用户输入的整数进行一些计算后,该值现在位于AX寄存器中。 Would I simply put call printf in the code after it? 我可以在代码之后简单地将调用printf放在代码中吗? Or does it print values from the stack? 还是从堆栈中打印值? Or is it maybe even possible to do something like: 或者甚至有可能做类似的事情:

AX printf

I apologize for my ignorance on this, our book does not cover inline assembly, and I'd like to avoid having to write a massive segment of code to convert if I can. 对此我的无知我深表歉意,我们的书没有涵盖内联汇编,并且我想避免必须写大量的代码来进行转换。 Plus I can't really seem to find answers on how exactly printf works. 另外,我似乎真的找不到关于printf的工作原理的答案。 Thank you for any help, I really appreciate it! 谢谢您的帮助,我非常感谢!

The easiest way to accomplish this is to use inline assembler to copy your register to some variable, and then print that variable. 完成此操作的最简单方法是使用内联汇编器将寄存器复制到某个变量,然后打印该变量。

short registerValue;
__asm mov registerValue, ax;
printf("ax: %hd", registerValue);

The exact assembler invocation will depend on your compiler and syntax; 确切的汇编程序调用将取决于您的编译器和语法。 the above likely won't work with a compiler other than cl. 以上可能不适用于cl以外的编译器。

If you want to actually call printf from assembler, you'll need to figure out it's calling convention and how that calling convention passes variadic function arguments. 如果要从汇编程序实际调用printf ,则需要弄清楚它的调用约定以及该调用约定如何传递可变函数参数。

Depending on the compiler, there may be predefined pseudo-symbols which directly access the registers. 根据编译器的不同,可能会有直接访问寄存器的预定义伪符号。 This was especially convenient with Turbo C and its descendants: 这对于Turbo C及其后代特别方便:

 _some_magic_function ();
 printf ("es:bx = %0x:%0x\n", _ES, _BX);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM