nasm astricmp函数 x86

xxx.asm:

%define p1 ebp+8
%define p2 ebp+12
%define p3 ebp+16

section .text
  global dllmain
  export astricmp

dllmain:
  mov eax,1
  ret 12

;-------------------------------------------;
; 对字符串(char)进行不区分大小写的比较。
;-------------------------------------------;
astricmp:
  push ebp
  mov ebp,esp
  sub esp,12
  
  %define pStr1 edx
  %define pStr2 ecx
  
  mov pStr1,[p1]	; char ptr1
  mov pStr2,[p2]	; char ptr2
  
  mov [ebp-12],ebx
  
  .for:
  mov [ebp-4],pStr1
  mov [ebp-8],pStr2
  
  mov bh,[pStr1]
  mov bl,[pStr2]
  
  mov al,bh
  push eax
  call _toLowerCase
  mov bh,al
  
  mov al,bl
  push eax
  call _toLowerCase
  mov bl,al
  
  test bh,bl
  jz .identical
  
  cmp bh,bl
  jc .less
  jne .greater  ; 不相等跳
  
  mov pStr1,[ebp-4]
  mov pStr2,[ebp-8]
  inc pStr1
  inc pStr2
  jmp .for
  
  
  ;-----------------------------------------------------;
  ; <0 string1 less than string2
  ;-----------------------------------------------------;
  .less:
  xor eax,eax
  not eax
  jmp .return
  
  
  ;-----------------------------------------------------;
  ; 0 string1 identical to string2
  ;-----------------------------------------------------;
  .identical:
  xor eax,eax
  jmp .return

  ;-----------------------------------------------------;
  ; >0 string1 greater than string2
  ;-----------------------------------------------------;
  .greater:
  mov eax,1
  jmp .return
  
  .return:
  mov ebx,[ebp-12]  
  add esp,12
  mov esp,ebp
  pop ebp
  ret 8
  
  
_toLowerCase:
  push ebp
  mov ebp,esp
  
  mov al,[p1]	; char
  ;----------------------------------------;
  ; 如果 < 0x41 return
  ;----------------------------------------;
  cmp al,41h
  jb .return
  
  ;----------------------------------------;
  ; 如果 > 0x5A return
  ;----------------------------------------;
  cmp al,5Ah
  ja .return
  
  add al,20h
  
  .return:
  mov esp,ebp
  pop ebp
  ret 4

c++:

#include <iostream>
#include <Windows.h>

typedef size_t (CALLBACK* astricmp_t)(const char* str1, const char* str2);

astricmp_t astricmp;

int main()
{
  HMODULE myDLL = LoadLibraryA("xxx.dll");
  astricmp = (astricmp_t)GetProcAddress(myDLL, "astricmp");

  printf("%d\n", _stricmp("a", "a")); // 0
  printf("%d\n", _stricmp("a", "A")); // 0
  printf("%d\n", _stricmp("", ""));   // 0
  printf("%d\n", _stricmp("a", "b")); // -1
  printf("%d\n", _stricmp("b", "a")); // 1

  printf("//----------------------------------------\n");

  printf("%d\n", astricmp("aaaa", "AaA")); // 0
  printf("%d\n", astricmp("a", "A")); // 0
  printf("%d\n", astricmp("", ""));   // 0
  printf("%d\n", astricmp("a", "b")); // -1
  printf("%d\n", astricmp("b", "aaaaa")); // 1
  return 0;
}
posted @ 2020-09-26 14:58  Ajanuw  阅读(150)  评论(0编辑  收藏  举报