nasm astrcpy_s函数 x86

xxx.asm

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

section .text
  global dllmain
  export astrcpy_s

dllmain:
  mov eax,1
  ret 12

astrcpy_s:
  push ebp
  mov ebp,esp
  push ebx
  
  mov eax,[p1]	; dst char ptr
  mov ecx,[p2]	; dwDstSize
  mov edx,[p3]	; src char ptr
  
  .for:
  ;-------------------------------------;
  ; get src first char
  ;-------------------------------------;
  mov bl,[edx]
  
  ;-------------------------------------;
  ; 检查src是否结束
  ;-------------------------------------;
  test bl,bl
  jz .return
  
  ;-------------------------------------;
  ; copy
  ;-------------------------------------;
  mov [eax],bl
  
  ;-------------------------------------;
  ; 避免dst溢出
  ;-------------------------------------;
  dec ecx
  test ecx,ecx
  jz .return
  
  ;-------------------------------------;
  ; next
  ;-------------------------------------;
  inc eax
  inc edx
  jmp .for
  
  .return:
  pop ebx
  mov esp,ebp
  pop ebp
  ret 12

c++:

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

typedef void (CALLBACK* astrcpy_s_t)(char* dest, size_t dest_size, const char* src);

astrcpy_s_t astrcpy_s;

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

  char r[100];

  strcpy_s(r, sizeof(r), "123");
  printf("%s\n", r); // 123

  astrcpy_s(r, sizeof(r), "456");
  printf("%s\n", r); // 456

  //===============================//

  strcpy_s(r, 2, "1");
  printf("%s\n", r); // 1

  astrcpy_s(r, 2, "2");
  printf("%s\n", r); // 2
  return 0;
}
posted @ 2020-09-24 13:56  Ajanuw  阅读(114)  评论(0编辑  收藏  举报