nasm astrcat_s函数 x86

xxx.asm

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

section .text
  global dllmain
  export astrcat_s

dllmain:
  mov eax,1
  ret 12

astrcat_s:
  push ebp
  mov ebp,esp
  push ebx

  mov ecx,[p1]	; dst char ptr
  mov eax,[p2]	; src char ptr
  mov edx,[p3]	; dwDstSize
  
  ; if dwDstSize==0 return FALSE
  test edx,edx
  jz .error
  
  ; get dst char end
  .dstFor:
  cmp byte [ecx],0
  je .copyFor
  inc ecx
  dec edx
  test edx,edx
  jz .error
  jmp .dstFor
  
  .copyFor:
  cmp byte [eax],0
  je .success
  dec edx
  test edx,edx
  jz .error
  mov bl,byte [eax]
  mov byte [ecx],bl
  inc eax
  inc ecx
  jmp .copyFor
  
  .error:
  xor eax,eax
  jmp .return
  
  .success:
  mov eax,1
  jmp .return
  
  .return:
  pop ebx
  mov esp,ebp
  pop ebp
  ret 12

c++:

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

typedef BOOL(CALLBACK* astrcat_s_t)(char* dst, const char* src, size_t dwDstSize);

astrcat_s_t astrcat_s;

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

  const char* a = "hello";
  const char* b = " world";
  char dst[12] = { 0 };
  if (!astrcat_s(dst, a, sizeof(dst)))
  {
    printf("astrcat_s 1 error.\n");
    return 0;
  }

  if (!astrcat_s(dst, b, sizeof(dst)))
  {
    printf("astrcat_s 2 error.\n");
    return 0;
  }
  
  printf("%p\n", dst);
  printf("%s\n", dst); // hello world
  return 0;
}
posted @ 2020-09-23 19:38  Ajanuw  阅读(113)  评论(0编辑  收藏  举报