将某些程序集转换为 VB.NET - SHR 运算符的工作方式不同吗?

发布于 2024-09-09 00:44:37 字数 410 浏览 4 评论 0原文

好吧,这里有一个简单的问题,

我正在研究一些汇编,并将一些汇编例程转换回 VB.NET

现在,有一行特定的代码我遇到了麻烦,在汇编中,假设以下内容:

EBX = F0D04080

然后执行以下行

SHR EBX, 4

这给了我以下内容:

EBX = 0F0D0408

现在,在 VB.NET 中,我执行以下操作

variable = variable >> 4

哪个应该给我相同的...但它略有不同,而不是值 0F0D0408 我得到 FF0D0408

那么这里发生了什么?

Well, a simple question here

I am studying some assembly, and converting some assembly routines back to VB.NET

Now, There is a specific line of code I am having trouble with, in assembly, assume the following:

EBX = F0D04080

Then the following line gets executed

SHR EBX, 4

Which gives me the following:

EBX = 0F0D0408

Now, in VB.NET, i do the following

variable = variable >> 4

Which SHOULD give me the same... But it differs a SLIGHT bit, instead of the value 0F0D0408 I get FF0D0408

So what is happening here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-09-16 00:44:37

>> 的文档中运算符

在算术右移中,移位超出最右边位位置的位将被丢弃,最左边(符号)位将传播到左侧空出的位位置。这意味着如果模式为负值,则空出的位置设置为 1;否则它们被设置为零。

如果您使用有符号数据类型,则 F0B04080 具有负号(开头的位 1),该负号将被复制到左侧空出的位置。

顺便说一句,这不是 VB.NET 特有的东西: variable >>> 4 被转换为 IL指令 shr​​,它是一个“算术移位”并保留符号,与 x86 汇编指令 SHR 不同,它是一个无符号移位。要在 x86 汇编器中进行算术移位,可以使用 SAR。

要在 VB.NET 中使用无符号移位,您需要使用无符号变量:

Dim variable As UInteger = &HF0D04080UI

F0D04080 末尾的 UI 类型字符 告诉 VB.NET 该文字是一个无符号整数(否则,它将被解释为负符号整数)并且赋值会导致编译时错误)。

From the documentation of the >> operator:

In an arithmetic right shift, the bits shifted beyond the rightmost bit position are discarded, and the leftmost (sign) bit is propagated into the bit positions vacated at the left. This means that if pattern has a negative value, the vacated positions are set to one; otherwise they are set to zero.

If you are using a signed data type, F0B04080 has a negative sign (bit 1 at the start), which is copied to the vacated positions on the left.

This is not something specific to VB.NET, by the way: variable >> 4 is translated to the IL instruction shr, which is an "arithmetic shift" and preserves the sign, in contrast to the x86 assembly instruction SHR, which is an unsigned shift. To do an arithmetic shift in x86 assembler, SAR can be used.

To use an unsigned shift in VB.NET, you need to use an unsigned variable:

Dim variable As UInteger = &HF0D04080UI

The UI type character at the end of F0D04080 tells VB.NET that the literal is an unsigned integer (otherwise, it would be interpreted as a negative signed integer and the assignment would result in a compile-time error).

难以启齿的温柔 2024-09-16 00:44:37

VB 的 >> 运算符执行算术移位,即移位符号位而不是 0。

variable = (variable >> shift_amt) And Not (Integer.MinValue >> (shift_amt - 1))

应该给你一个等价的值,即使它有点长。或者,您可以使用无符号整数(UInteger 或 UInt32),因为没有符号位需要移动。

VB's >> operator does an arithmetic shift, which shifts in the sign bit rather than 0's.

variable = (variable >> shift_amt) And Not (Integer.MinValue >> (shift_amt - 1))

should give you an equivalent value, even if it is a bit long. Alternatively, you could use an unsigned integer (UInteger or UInt32), as there's no sign bit to shift.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文