zoukankan      html  css  js  c++  java
  • nasm astrncmp函数 x86

    xxx.asm:

    %define p1 ebp+8
    %define p2 ebp+12
    %define p3 ebp+16
    
    section .text
      global dllmain
      export astrncmp
    
    dllmain:
      mov eax,1
      ret 12
    
    ;------------------------------------------------;
    ;	比较最多两个字符串的指定字符数。
    ;------------------------------------------------;
    astrncmp:
      push ebp
      mov ebp,esp
      push ebx
    
      mov ecx,[p1]	; const char *string1
      mov eax,[p2]	; const char *string2
      mov edx,[p3]	; size_t count
      
      .for:
      test edx,edx
      jz .identical
      mov bl,[ecx]
      cmp bl,[eax]
      jc .less
      jne .greater
      inc ecx
      inc eax
      dec edx
      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:
      pop ebx
      mov esp,ebp
      pop ebp
      ret 12
    

    c++:

    #include <iostream>
    #include <Windows.h>
    
    typedef int (CALLBACK* astrncmp_t)(const char* string1, const char* string2, size_t count);
    
    astrncmp_t astrncmp;
    
    int main()
    {
      HMODULE myDLL = LoadLibraryA("xxx.dll");
      astrncmp = (astrncmp_t)GetProcAddress(myDLL, "astrncmp");
      
      printf("%d
    ", strncmp( "a1", "a2", 0));  // 0
      printf("%d
    ", astrncmp("a1", "a2", 0));  // 0
    
      printf("%d
    ", strncmp( "a1", "a2", 1));  // 0
      printf("%d
    ", astrncmp("a1", "a2", 1));  // 0
    
      printf("%d
    ", strncmp( "a1", "a2", 2));  // -1
      printf("%d
    ", astrncmp("a1", "a2", 2));  // -1
    
      return 0;
    }
    
  • 相关阅读:
    PAT2019顶级7-2:美丽的序列(线段树+DP)
    ZOJ2112 Dynamic Rank(可持久化线段树套树状数组)
    CF1353E K-periodic Garland(动态规划)
    CF1353D Constructing the array(优先队列)
    HDU2069 Coin Change(基础DP)
    surf(树状数组+DP)
    双倍快乐(回文树)
    ZOJ3591 Nim(博弈论)
    HDU6601 Keep On EveryThing But Triangle(可持久化线段树)
    HDU6599 I Love Palindrome String(回文树)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13742451.html
Copyright © 2011-2022 走看看