zoukankan      html  css  js  c++  java
  • 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
    ", r); // 123
    
      astrcpy_s(r, sizeof(r), "456");
      printf("%s
    ", r); // 456
    
      //===============================//
    
      strcpy_s(r, 2, "1");
      printf("%s
    ", r); // 1
    
      astrcpy_s(r, 2, "2");
      printf("%s
    ", r); // 2
      return 0;
    }
    
  • 相关阅读:
    HOWTO re
    数据类型
    字符串
    最大公约数
    this
    tip 2:找最小公倍数之Boost
    tip 1:一个简单的将int型转换成char的方法
    Item47
    成员函数模板
    item44:将与参数无关的代码抽离template
  • 原文地址:https://www.cnblogs.com/ajanuw/p/13723735.html
Copyright © 2011-2022 走看看