nasm astrstr函数 x86

xxx.asm:

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

section .text
  global dllmain
  export astrstr

dllmain:
  mov eax,1
  ret 12

;-------------------------------------------------------------;
; 返回一个指针,该指针指向字符串中第一次出现的搜索字符串
;-------------------------------------------------------------;
astrstr:
  push ebp
  mov ebp,esp
  sub esp,4
  
  mov ecx,[p1]	; const char *str
  mov edx,[p2]	; const char *strSearch
  
  mov [ebp-4],ecx
  
  .for:
  mov ah,[edx]
  mov al,[ecx]
  
  ;--------------------------------------------------;
  ; strSearch 全部查找完
  ;--------------------------------------------------;
  test ah,ah
  jz .find
  
  ;--------------------------------------------------;
  ; str 全部查找完
  ;--------------------------------------------------;
  test al,al
  jz .notFind
  
  ;--------------------------------------------------;
  ; 如果相等进行下一个strSearch字符的判断
  ;--------------------------------------------------;
  cmp ah,al
  je .foarchNext
  
  ;--------------------------------------------------;
  ; 不相等进行下一个str字符的判断
  ; 如果strSearch指针变动,则恢复strSearch指针
  ;--------------------------------------------------;
  cmp edx,[p2]
  jne .reSearch
  inc ecx
  mov [ebp-4],ecx
  jmp .for
  
  .reSearch:
  mov edx,[p2]
  mov [ebp-4],ecx
  jmp .for
  
  .foarchNext:
  inc ecx
  inc edx
  jmp .for
  
  .notFind:
  xor eax,eax
  jmp .return
  
  .find:
  mov eax,[ebp-4]
  jmp .return
  
  .return:
  add esp,4
  mov esp,ebp
  pop ebp
  ret 8

c++:

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

typedef int (CALLBACK* astrstr_t)(const char* str, const char* strCharSet);
astrstr_t astrstr;

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

  const char* s1 = "hello world";
  const char* s2 = "ll";
  printf("%s\n", strstr( s1, s2)); // llo world
  printf("%s\n", astrstr(s1, s2)); // llo world
  return 0;
}
上一篇:在VS Code中调试nasm,使用golink连接


下一篇:如何理解当代中国----《十亿消费者》摘录(转)