这是一个执行 XOR 操作的代码。代码就像 x.bits.b = x.bits.c ^ x.bits.a 我想要的是: x.bits.c = x.bits.a ^ x.bits.b
我试图将所有“cl”值更改为“bl”,但它不起作用。
#include "stdafx.h"
int main()
{
typedef union {
unsigned char BYTE;
struct {
unsigned char a : 1;
unsigned char b : 3;
unsigned char c : 4;
}bits;
}un_X;
un_X x;
x.BYTE = 0xFE;
x.bits.a = 1;
x.bits.b = 0;
x.bits.c = 2;
printf("\n");
printf("BYTE = %X\n", x.BYTE);
printf(" a = %d\n", x.bits.a);
printf(" b = %d\n", x.bits.b);
printf(" c = %d\n", x.bits.c);
_asm {
//unsigned char a = x.BYTE & 0x01;
mov al, x.BYTE
and al, 0x01
// unsigned char c = (x.BYTE & 0xF0) >> 4;
mov cl, x.BYTE
and cl, 0xF0
shr cl, 4
//unsigned char b = c ^ a;
xor al, cl
// x.BYTE &= 0xF1; // 1111 0001
mov dl, x.BYTE
and dl, 0xF1
// x.BYTE |= (b << 1);
shl al, 1
or dl, al
mov x.BYTE, dl
}
printf("\n");
printf("BYTE = %X\n", x.BYTE);
printf(" a = %d\n", x.bits.a);
printf(" b = %d\n", x.bits.b);
printf(" c = %d\n", x.bits.c);
getchar();
return 0;
}
我希望 a 和 b 是恒定的,而 c 是可变的。