nasm astrchr函数 x86
xxx.asm:
%define p1 ebp+8
%define p2 ebp+12
%define p3 ebp+16
section .text
global dllmain
export astrchr
dllmain:
mov eax,1
ret 12
astrchr:
push ebp
mov ebp,esp
mov eax,[p1] ; char ptr
mov ecx,[p2] ; char
.for:
;-------------------------------------------;
; 找到后返回第一次出现的指针
;-------------------------------------------;
cmp [eax],cl
je .return
inc eax
;-------------------------------------------;
; 如果找不到该字符,则该函数返回空指针
;-------------------------------------------;
cmp byte [eax],0
je .error
jmp .for
.error:
xor eax,eax
.return:
mov esp,ebp
pop ebp
ret 8
c++:
#include <iostream>
#include <Windows.h>
typedef char*(CALLBACK* astrchr_t)(const char* str, int character);
astrchr_t astrchr;
int main()
{
HMODULE myDLL = LoadLibraryA("xxx.dll");
astrchr = (astrchr_t)GetProcAddress(myDLL, "astrchr");
char str[] = "hello world";
char* pch = strchr(str, 'l');
printf("%s\n", pch); // llo world
char* pch2 = astrchr(str, 'l');
printf("%s\n", pch2); // llo world
//==============================//
char* pch3 = strchr(str, 'b');
printf("%s\n", pch3); // (null)
char* pch4 = astrchr(str, 'b');
printf("%s\n", pch4); // (null)
return 0;
}