/*
mov 7EFEFEFF edx
h3 h2 h1 h0
01111110 11111110 11111110 1111111-->
4个0代表4个判断标志 h3 h2 h1 h0
//eax 包含4个字节 每个字节 0-7包含8位
b3(0-7) b2(0-7) b1(0-7) b0(0-7)
xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
其中hi 和bi基本同一个位数的值 但是代表着不同的变量
add edx ,eax
b(i) = 0
b0 = 00000000 -> h0 = b1(0)
b1 = 00000000 -> h1 = b2(0)
b2 = 00000000 -> h2 = b3(0)
b3 = 00000000 -> h3 = b3(7)
b(i) !=0 时候 会产生进位
b0 ! = 0000000 ->h0 = b1(0)+1
b1 ! = 0000000 ->h1 = b2(0)+1
b2 ! = 0000000 ->h2 = b3(0)+1
b3 ! = 0000000 ->h3 = b3(7)+1
xor eax,edx
eax
b3(0-7) b2(0-7) b1(0-7) b0(0-7)
b3(7)xxxxxx b3(0) xxxxxxxb2(0) xxxxxxxb1(0) xxxxxxxx
edx
h(3)111111h(2) 1111111h(1) 1111111h(0) 1111111
b1(0) = b1(0) xor h(0)
b2(0) = b2(0) xor h(1)
b3(0) = b3(0) xor h(2)
b3(7) = b3(7) xor h(3)
b(i) = 0
=>
b1(0) = b1(0) xor b1(0) = 0
b2(0) = b2(0) xor b2(0) = 0
b3(0) = b3(0) xor b3(0) = 0
b3(7) = b3(7) xor b3(7) = 0
b(i) != 0
bi(0) = 0 或者 =1
b1(0) = b1(0) xor (b1(0)+1)
b2(0) = b2(0) xor (b2(0)+1)
b3(0) = b3(0) xor (b3(0)+1)
b3(7) = b3(7) xor (b3(7)+1)
b1(0) = b1(0) xor (b1(0)+1)
b1(0) = 0
=> b1(0) = 0 xor(0+1) = 0 xor 1 = 1
b1(0) = 1
=> b1(0) = 1 xor(1+1) = 1 xor 0 = 1
b1(0) = 1
同样
b2(0) = 1
b3(0) = 1
b3(7) = 1
可以看出他们具有结构上的一致性 这样比较容易构造代码
xor eax,0FFFFFFFFh
b1(0) = not b1(0)
b2(0) = not b2(0)
b3(0) = not b3(0)
b3(7) = not b3(7)
bi = 0 => bi = 1
bi = 1 => bi = 0
现在从头到这里看下数据
传入的数据
b0 = 0 -> b1(0) = 1
b1 = 0 -> b2(0) = 1
b2 = 0 -> b3(0) = 1
b3= 0 -> b3(7) = 1
b0 != 0 -> b1(0) = 0
b1 != 0 -> b2(0) = 0
b2 != 0 -> b3(0) = 0
b3 != 0 -> b3(7) = 0
test eax,81010100h
//81010100h
1000 0001 0000 0001 0000 0001 0000 0000
这个很显然在测试b(i)的值
je loop; // ZF = 1
需要0xxxxxxx0 xxxxxxxx0 xxxxxxxx0 xxxxxxxxx
也就是4个b(i)全部为 0 再次读取4个字节
bi(0)全为0的条件是b0 b1 b2 b3 都不为0
如果有个 1会走下面表示含有0
je/jz 是 当运算结果为0时则跳,也就是当ZF标志为1时跳转。
ZF标志为1,说明上一次运算(不管是算术运算还是逻辑运算)结果为0,
而ZF为0,说明运算结果非0。
//test eax, 100b; b后缀意为二进制
// jnz ******; 如果eax右数第三个位为1,jnz将会跳转
*/
bool c0(int d)
{_asm
{
main_loop:
lea ecx,d
mov edx,7EFEFEFFh
mov eax,dword ptr [ecx]
add edx,eax
xor eax,edx
xor eax,0FFFFFFFFh
test eax,81010100h
mov edx,[ecx]
je short main_loop_out
//; found zero byte in the loop
//; main_loop_end:
test dl,dl ; is it byte 0
je short byte_0
test dh,dh ; is it byte 1
je short byte_1
test edx,00ff0000h ; is it byte 2
je short byte_2
test edx,0ff000000h ; is it byte 3
je short byte_3
jmp short main_loop ; taken if bits 24-30 are clear and bit
; 31 is set
byte_3:
ret
byte_2:
ret
byte_1:
ret
byte_0:
ret
main_loop_out:
//不包含0字节
ret
}
};
这个函数是比较复杂的。他可以一次copy4个字节。c的代码是1次1个。关键 就是这个检查0 byte的算法。