zoukankan      html  css  js  c++  java
  • 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
    ", _stricmp("a", "a")); // 0
      printf("%d
    ", _stricmp("a", "A")); // 0
      printf("%d
    ", _stricmp("", ""));   // 0
      printf("%d
    ", _stricmp("a", "b")); // -1
      printf("%d
    ", _stricmp("b", "a")); // 1
    
      printf("//----------------------------------------
    ");
    
      printf("%d
    ", astricmp("aaaa", "AaA")); // 0
      printf("%d
    ", astricmp("a", "A")); // 0
      printf("%d
    ", astricmp("", ""));   // 0
      printf("%d
    ", astricmp("a", "b")); // -1
      printf("%d
    ", astricmp("b", "aaaaa")); // 1
      return 0;
    }
    
  • 相关阅读:
    剑指Offer(链表)-从尾到头打印链表
    Java数据结构与算法-链表
    剑指Offer(数组)-数组中重复的数字
    剑指Offer(数组)-二维数组的查找
    Java冒泡排序法实现
    springMVC全局异常配置
    CookieUtil工具类
    算法
    Java
    算法
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13735047.html
Copyright © 2011-2022 走看看