如何判断变量中的数据是C ++中的字还是字节? [英] How do I figure out if the data in a variable is a word or a byte in C++?

查看:184
本文介绍了如何判断变量中的数据是C ++中的字还是字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前提出的问题都错了,请允许我解释一下。

我正在努力为x86处理器编写一个声明。 x86指令集具有基于操作数大小的相同指令的不同操作码。我想知道的是,给定一个语句'mov eax,123'我如何找出操作数(字节,字,双字,qword ......)?我正在使用C ++作为我的实现语言。

希望这更有意义。

非常感谢。

I put my question all wrong earlier, allow me to explain.
I am struggling to write an assember for x86 processors. The x86 instruction set has different opcodes for the same instructions based on the operand sizes. What I want to know is that given a statement 'mov eax, 123' how do I figure out the the operands(byte, word, dword, qword...)? I'm using C++ as my implementation language.
Hope this makes a little more sense.
Thanks anyway.

推荐答案

如果你的意思是'我如何确定一个 int 值是否适合字节?'然后答案是肯定的,你可以这样做。一个简单的方法可能是往返:

If you meant 'How could I establish if an int value would fit in a byte?' then the answer is yes, you may do it. A simple approach could be the round-trip:
unsigned int a = 0xDEADBEEF; // see the nvr3 comment
bool fit_in_a_byte = (a == static_cast< unsigned char >(a));


好了现在它更有意义了。

编写汇编程序并不是一件容易的事,至少编码正确机器码。

在某些情况下,即使存在完整的指定指令,也可能有多个合法编码。

无论如何回到你的问题考虑在汇编那里可以是显式或隐式操作数和操作数大小。

如果是指令:

OK now it makes a little bit more sense.
To write an assembler is not such an easy task, at least to encode correct machine code.
In some cases even if a full specified instruction is present there could be more than one legal encoding.
Anyway going back to your question consider that in assembly there can be explicit or implicit operands and operands sizes.
In case of the instruction:
mov eax, 123



它不是完全限定的,但我们可以从显式操作数中获取隐式操作数大小。该指令在32位寄存器中移动,即显式操作数,一个值。该值必须大小相同,因此它是32位整数。在'C'语言中它将是'int'。

在几乎所有指令中,您都可以从显式操作数推断操作数大小。如果你不能让汇编程序使用元限定符来表明它。

I.e.


It is not fully qualified, but we can get the implicit operand size from the explicit operand. This instruction moves in a 32bits register, the explicit operand, a value. This value must be of same size, so it is a 32bits integer. In 'C' language it would be an 'int'.
In almost all instructions you can extrapolate the operands size from explicit operand. In cases where you can't the assembler use meta qualifiers to make it clear.
I.e.

movsx eax, byte ptr[var];



指令movsx加载一个带有不同大小符号扩展值的寄存器,在我们的特定情况下,目标是32位寄存器eax,但操作数是一个字节。



有些汇编程序允许不合格的类型,其中可能有多个操作数大小,但在这种情况下,必须在手册中明确说明汇编程序默认的操作数大小......


The instruction movsx load a register with a sign extended value of different size, in our specific case the destination is the 32bits register eax, but the operand is a byte.

Some assemblers allows unqualified types also where there could be more than one operand size, but in that case it must be clearly stated in in the manual to which operand size the assembler will default...


这篇关于如何判断变量中的数据是C ++中的字还是字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆