nasm astrncat_s函数 x86

xxx.asm:

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

section .text
  global dllmain
  export astrncat_s

dllmain:
  mov eax,1
  ret 12

;------------------------------------------------;
;	将字符追加到字符串
;------------------------------------------------;
astrncat_s:
  push ebp
  mov ebp,esp
  sub esp,8
  mov [ebp-4],ebx

  mov ecx,[p1]	; char *strDest
  mov eax,[p2]	; size_t numberOfElements
  mov edx,[p3]	; const char *strSource
  mov ebx,[p4]	; size_t count
 
  ; get strDest end
  .for1:
  test eax,eax
  jz .return
  cmp byte [ecx],0
  je .eachCopy
  inc ecx
  dec eax
  jmp .for1
  
  .eachCopy:
  test ebx,ebx
  jz .return
  test eax,eax
  jz .return
  mov [ebp-8],ebx
  
  ; copy
  mov bl,byte [edx]
  test bl,bl
  je .return
  mov byte [ecx],bl
  
  ; next
  mov ebx,[ebp-8]
  inc ecx
  inc edx
  dec eax
  dec ebx
  jmp .eachCopy
   
  .return:
  xor eax,eax
  mov ebx,[ebp+4]
  add esp,8
  mov esp,ebp
  pop ebp
  ret 16

c++:

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

typedef int (CALLBACK* astrncat_s_t)(char* strDest, size_t numberOfElements, const char* strSource, size_t count); 

astrncat_s_t astrncat_s;

int main()
{
  HMODULE myDLL = LoadLibraryA("xxx.dll");
  astrncat_s = (astrncat_s_t)GetProcAddress(myDLL, "astrncat_s");
  
  char s1[10] = "ab";
  const char* s2 = "cde";
  strncat_s(s1, sizeof(s1), s2, 2);
  printf("%s\n", s1); // abcd

  //----------------------------------------------------------------------

  char s3[10] = "ab";
  const char* s4 = "cde";
  astrncat_s(s3, sizeof(s3), s4, 2);
  printf("%s\n", s3); // abcd
  return 0;
}
posted @ 2020-09-27 23:00  Ajanuw  阅读(141)  评论(0编辑  收藏  举报