nasm astrrchr函数 x86

xxx.asm

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

section .text
  global dllmain
  export astrrchr

dllmain:
  mov eax,1
  ret 12

;------------------------------------------------;
;	扫描字符串以查找字符的最后一次出现。
;------------------------------------------------;
astrrchr:
  push ebp
  mov ebp,esp
  push ebx

  mov ecx,[p1]	; const char *str
  mov eax,[p2]	; int c
  
  .for:
  mov dl,[ecx]
  test dl,dl
  jz .return
  cmp dl,al
  jne .next
  mov ebx,ecx
  .next:
  inc ecx
  jmp .for
  
  .return:
  mov eax,ebx
  pop ebx
  mov esp,ebp
  pop ebp
  ret 8

c++:

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

typedef int (CALLBACK* astrrchr_t)(const char* str, int c);

astrrchr_t astrrchr;

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

  printf("%s\n", strrchr( "hello world", "o"[0])); // orld
  printf("%s\n", astrrchr("hello world", "o"[0])); // orld

  printf("%s\n", strrchr( "hello world", "l"[0])); // ld
  printf("%s\n", astrrchr("hello world", "l"[0])); // ld
  return 0;
}
posted @ 2020-09-27 23:36  Ajanuw  阅读(114)  评论(0编辑  收藏  举报