nasm astrcmp函数 x86

xxx.asm:

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

section .text
  global dllmain
  export astrcmp

dllmain:
  mov eax,1
  ret 12

astrcmp:
  push ebp
  mov ebp,esp
  push ebx
  
  mov eax,[p1]	; char ptr 1
  mov ecx,[p2]	; char ptr 2
  
  .for:
  mov dl,[eax]
  mov bl,[ecx]
  cmp dl,bl
  jc .r1
  jne .r3
  test dl,bl
  jz .r2
  inc eax
  inc ecx
  jmp .for
  
  ;-----------------------------------------------------;
  ; <0 第一个不匹配的字符在ptr1中的值比在ptr2中的值低
  ;-----------------------------------------------------;
  .r1:
  xor eax,eax
  not eax
  jmp .return
  
  ;-----------------------------------------------------;
  ; 0 两个字符串的内容相等
  ;-----------------------------------------------------;
  .r2:
  xor eax,eax
  jmp .return
  
  ;-----------------------------------------------------;
  ; >0 第一个不匹配的字符在ptr1中的值大于在ptr2中的值
  ;-----------------------------------------------------;
  .r3:
  mov eax,1
  jmp .return
  
  .return:
  pop ebx
  mov esp,ebp
  pop ebp
  ret 8

c++:

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

typedef int(CALLBACK* astrcmp_t)(const char* str1, const char* str2);

astrcmp_t astrcmp;

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

  printf("%x\n",  astrcmp("12", "22")); // -1
  printf("%x\n",  astrcmp("12", "12")); // 0
  printf("%x\n",  astrcmp("22", "12")); // 1
  return 0;
}
posted @ 2020-09-24 13:14  Ajanuw  阅读(151)  评论(0编辑  收藏  举报