开发者

Microsoft inline assembly and references or Why does BYTE PTR [ByteRef] not work in this situation?

Okay so I have a C++ fu开发者_运维技巧nction in which I am trying to use inline assembly

void ToggleBit(unsigned char &Byte, unsigned int Bit)
{
    /* In C:
    *    Byte ^= (1<<Bit);
    */
    __asm
    {
        push edx
        push ecx
        mov  ecx, Bit
        xor  edx, edx
        mov  edx, 1
        sal  dl, cl
        xor  BYTE PTR [Byte], dl
        pop  ecx
        pop  edx
    }
}

This should work, right? Since Byte is a reference (which is essentially a constant pointer), it must be dereferenced to access the data... but it didn't work!

Upon debugging the following code:

mov  edx, Byte           
;edx = 0x0040f9d3
mov  bl, BYTE PTR [Byte]
;bl = 0xd3

I don't understand why this would happen at all.


As you say, a reference is the same as a pointer in assembly. To access the reference/pointer, you must first read the pointer value, and then dereference it:

mov ecx, Byte      ; Or  mov ecx, [Byte]  which is the same thing
xor [ecx], dl

When you access the value at BYTE PTR [Byte], it accesses the first byte of the pointer value (the pointed-to address) instead of the pointed-to value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜