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?
From the documentation of the >> operator:
If you are using a signed data type,
F0B04080has a negative sign (bit1at the start), which is copied to the vacated positions on the left.This is not something specific to VB.NET, by the way:
variable >> 4is translated to the IL instructionshr, which is an “arithmetic shift” and preserves the sign, in contrast to the x86 assembly instructionSHR, which is an unsigned shift. To do an arithmetic shift in x86 assembler,SARcan be used.To use an unsigned shift in VB.NET, you need to use an unsigned variable:
The
UItype character at the end ofF0D04080tells 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).