nasm astrrev函数 x86

xxx.asm

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

section .text
  global dllmain
  export astrrev

dllmain:
  mov eax,1
  ret 12

;------------------------------------------------;
;	反转字符串的字符。
;------------------------------------------------;
astrrev:
  push ebp
  mov ebp,esp

  mov ecx,[p1]	; char *str
  mov edx,[p1]
  
  ;------------------------------------------------;
  ;	get last
  ;------------------------------------------------;
  .getLast:
  mov ah,[ecx]
  test ah,ah
  jz .getLastBreak
  inc ecx
  jmp .getLast
  
  .getLastBreak:
  dec ecx
  
  .rev:
  cmp ecx,edx
  jc .return	; 偶数ecx会小于edx
  je .return	; 奇数会相等
  
  ;------------------------------------------------;
  ;	交换字节
  ;------------------------------------------------;
  mov ah,[ecx]
  mov al,[edx]
  mov [edx],ah
  mov [ecx],al
  
  ; next
  dec ecx
  inc edx
  jmp .rev
  
  .return:
  mov eax,[p1]	; 返回原始指针
  mov esp,ebp
  pop ebp
  ret 4

c++:

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

typedef char* (CALLBACK* astrrev_t)(char* str);

astrrev_t astrrev;

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

  char s[12] = "hello world";
  printf("%s, %s\n", _strrev(s), s); // dlrow olleh, dlrow olleh
  printf("%s, %s\n", astrrev(s), s); // hello world, hello world
  return 0;
}
上一篇:什么是NASM


下一篇:在VS Code中调试nasm,使用golink连接