rep、repe、repne、repz、repnz

rep :ecx=0结束
repe:ecx=0、ZF=1 ZF=0时重复
repne:ecx=0、ZF=0 ZF=1时不重复

串操作指令

STD:执行后DF=1 (负方向) set direction flag
CLD:执行后DF=0(正方向)

ds:edi
ss:esp
其他默认是ds
代码段cs

stos

stos es:edi 把eax的值中的四个字节写到edi中,如果DF=0,此时还会edi+4

mov ecx ,0x30
mov eax, 0xcccccccch
rep stos ds:edi
这些代码表示循环给edi这个地址开始cccccccc依次填充0x30次

scas

repe scas 扫描与指定字符不同的字符串(因为不同时会直接结束扫描)
repne scas 扫描与指定字符相同的字符串 (因为相同时会直接退出)

裸函数

基本结构:
image.png
image.png
image.png
左边:默认是cdecl

  1. #include<iostream>
  2. _declspec(naked) int MyStrlen(char* str)
  3. {
  4. _asm ret
  5. }
  6. int main()
  7. {
  8. char szString[] = "hello";
  9. MyStrlen(szString);
  10. std::cout << "hello";
  11. return 0;
  12. }

右边:改成stdcall

  1. #include<iostream>
  2. _declspec(naked) int _stdcall MyStrlen(char* str)
  3. {
  4. _asm ret
  5. }
  6. int main()
  7. {
  8. char szString[] = "hello";
  9. MyStrlen(szString);
  10. std::cout << "hello";
  11. return 0;
  12. }

image.png

用裸函数实现strlen、strcmp、memset、memcpy

参考汇编代码:

  1. .386
  2. .model FLAT,STDCALL
  3. option casemap:none
  4. include msvcrt.inc
  5. includelib msvcrt.lib
  6. .data
  7. szStringFormat db 10 dup(0)
  8. .code
  9. _myStrlen proto :dword
  10. _myMemset proto :DWORD,:BYTE,:DWORD
  11. _myStrcpy proto :DWORD,:DWORD
  12. _myStrcmp proto :DWORD,:DWORD
  13. _myMemset proc szDestString:DWORD,szFillByte:BYTE,dwlen:DWORD
  14. mov edi,szDestString
  15. mov al,szFillByte
  16. mov ecx,dwlen
  17. rep stosb
  18. ret
  19. _myMemset endp
  20. _myStrlen proc szSrcString:DWORD
  21. mov edi,[ebp+8]
  22. mov al,0
  23. mov ecx,0fffffffh
  24. repne scasb
  25. not ecx
  26. mov eax,ecx
  27. ret
  28. _myStrlen endp
  29. _myStrcpy proc szDestString:DWORD,szSrcString:DWORD
  30. mov eax,szSrcString
  31. push eax
  32. call _myStrlen
  33. mov ecx,eax
  34. mov edi,szDestString
  35. mov esi,szSrcString
  36. rep movsb
  37. ret
  38. _myStrcpy endp
  39. _myStrcmp proc szDestString:DWORD,szSrcString:DWORD
  40. mov ecx,0fffffffh
  41. mov esi,szSrcString
  42. mov edi,szDestString
  43. repne cmpsb
  44. not ecx
  45. mov eax,ecx
  46. ret
  47. _myStrcmp endp
  48. main proc
  49. LOCAL szStrl[20]:BYTE
  50. LOCAL dwlen:DWORD
  51. ;memset(szStrl,0xcc,dwlen)
  52. push 20
  53. push 0cch
  54. lea eax,szStrl
  55. push eax
  56. call _myMemset
  57. ;scanf("%S",szStrl)
  58. lea eax,szStrl
  59. push eax
  60. push offset szStringFormat
  61. call crt_scanf
  62. ;dwlen=strlen(strl)
  63. lea eax,szStrl
  64. push eax
  65. call _myStrlen
  66. ;strcpy(szStrl,)
  67. lea eax,szStrl
  68. push eax
  69. call _myStrcpy
  70. ;strcmp
  71. lea eax,szStrl
  72. push eax
  73. call _myStrcmp
  74. ret
  75. main endp
  76. entry:
  77. call main
  78. ret
  79. end entry

裸函数实现:

  1. #include<iostream>
  2. #include <string.h>
  3. #include <memory.h>
  4. //1. memset(char* dest,int val,int len)
  5. _declspec(naked) void MyMemset(char* dest, int val, int len)
  6. {
  7. _asm
  8. {
  9. push ebp
  10. mov ebp,esp
  11. push eax
  12. push ecx
  13. push edi
  14. mov eax,val
  15. mov ecx,len
  16. mov edi,dest
  17. rep stos edi
  18. pop eax
  19. pop ecx
  20. pop edi
  21. mov esp,ebp
  22. pop ebp
  23. }
  24. }
  25. //2. memcpy(char* dest, char* src, int len)
  26. _declspec(naked) void MyMemcpy(char* dest, char* src, int len)
  27. {
  28. _asm
  29. {
  30. push ebp
  31. mov ebp, esp
  32. push edi
  33. push esi
  34. push ecx
  35. mov edi,dest
  36. mov esi,src
  37. mov ecx,len
  38. rep movsb edi,esi
  39. pop ecx
  40. pop esi
  41. pop edi
  42. mov esp, ebp
  43. pop ebp
  44. }
  45. }
  46. //3. strcmp(char* dest,char* src)
  47. _declspec(naked) void MyStrcmp(char* dest, char* src)
  48. {
  49. _asm
  50. {
  51. push ebp
  52. mov ebp, esp
  53. push edi
  54. push esi
  55. mov edi,dest
  56. mov esi,src
  57. cmps edi,esi
  58. pop esi
  59. pop edi
  60. mov esp, ebp
  61. pop ebp
  62. }
  63. }
  64. //4. int strlen(char* str)
  65. _declspec(naked) int MyStrlen(char* str)
  66. {
  67. _asm
  68. {
  69. push ebp
  70. mov ebp, esp
  71. mov esp, ebp
  72. pop ebp
  73. }
  74. }
  75. int main()
  76. {
  77. char szString[] = "hello";
  78. MyStrlen(szString);
  79. std::cout << "hello";
  80. return 0;
  81. }