Assembly 如何修改条件跳转的x86程序集标志?
在我的过程_Str_compare中,虽然它正确返回-1、0或1,但我不明白为什么从另一个汇编过程调用这个_Str_compare方法时,条件语句不能正常工作,例如下面示例中的jbe:Assembly 如何修改条件跳转的x86程序集标志?,assembly,x86,flags,conditional-statements,intel-syntax,Assembly,X86,Flags,Conditional Statements,Intel Syntax,在我的过程_Str_compare中,虽然它正确返回-1、0或1,但我不明白为什么从另一个汇编过程调用这个_Str_compare方法时,条件语句不能正常工作,例如下面示例中的jbe: _Str_compare proc ; (byte * str1, byte * str2, DWORD str1_len, DWORD str2_len) :: Returns -1, 0, or 1 for str1 is lexiographically before, equivalent to, or
_Str_compare proc ; (byte * str1, byte * str2, DWORD str1_len, DWORD str2_len) :: Returns -1, 0, or 1 for str1 is lexiographically before, equivalent to, or after str2, respectively. Also modifies the carry and zero flags so that cmp can be used directly following invokation of this method.
; init
push ebp
mov ebp, esp
push edi
push esi
push ecx
push edx
xor esi, esi
xor edi, edi
xor ecx, ecx
xor edx, edx
add esi, DWORD PTR [ebp + 8] ; esi = str1
add edi, DWORD PTR [ebp + 12] ; edi = str2
mov edx, DWORD PTR [ebp + 16]
cmp edx, DWORD PTR [ebp + 20]
jae IFBLOCK1
add ecx, DWORD PTR [ebp + 16]
IFBLOCK1:
add ecx, DWORD PTR [ebp + 20]
add edx, ecx ; edx is a buffer for holding ecx's value after looping through the strings
; code
cld ; traverse strings from beginning to end
repe cmpsb
cmp esi, edx
jne IFBLOCK2
mov edx, DWORD PTR [ebp + 16]
cmp edx, DWORD PTR [ebp + 20]
je op2
jmp op1
IFBLOCK2:
mov edx, DWORD PTR [esi - 1]
cmp edx, DWORD PTR [edi - 1]
jb op1
je op2
ja op3
op1:
lahf
or ax, 01h ; set the carry flag
and ax, 0FFBFh ; clear the zero flag
sahf
xor eax, eax
dec eax
jmp finish
op2:
lahf
and ax, 0FFFEh ; clear the carry flag
or ax, 040h ; set the zero flag
sahf
xor eax, eax
jmp finish
op3:
lahf
and ax, 0FFBEh ; clear both the carry and zero flags
sahf
xor eax, eax
inc eax
finish: ; clean and exit method
pop edx
pop ecx
pop esi
pop edi
add ebp, 4
pop ebp
ret
_Str_compare endp
ADD
指令(与许多其他算术/逻辑指令一样)会影响EFLAFS
,这就是为什么JBE
不会作用于\u Str\u compare
返回的内容,而是作用于ADD ESP,16
结果的其他内容。我猜同样的问题也存在于addebp,4
和XOR EAX,EAX
请仔细检查有关受影响标志的说明。在AMD的CPU手册中,在第3卷的末尾有一个很好的摘要,其中列出了所有标志修改指令
sampleProc proc
push 6
push 3
push sixLetteredStringAddress
push threeLetteredStringAddress
call _Str_compare
add esp, 16
jbe IF_STATEMENT_1
inc eax ; dummy operation
IF_STATEMENT_1:
ret
sampleProc endp