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        扫描与指定字符相同的字符串  (因为相同时会直接退出)       
裸函数
基本结构:


左边:默认是cdecl
#include<iostream>_declspec(naked) int MyStrlen(char* str){_asm ret}int main(){char szString[] = "hello";MyStrlen(szString);std::cout << "hello";return 0;}
右边:改成stdcall
#include<iostream>_declspec(naked) int _stdcall MyStrlen(char* str){_asm ret}int main(){char szString[] = "hello";MyStrlen(szString);std::cout << "hello";return 0;}

用裸函数实现strlen、strcmp、memset、memcpy
参考汇编代码:
.386.model FLAT,STDCALLoption casemap:noneinclude msvcrt.incincludelib msvcrt.lib.dataszStringFormat db 10 dup(0).code_myStrlen proto :dword_myMemset proto :DWORD,:BYTE,:DWORD_myStrcpy proto :DWORD,:DWORD_myStrcmp proto :DWORD,:DWORD_myMemset proc szDestString:DWORD,szFillByte:BYTE,dwlen:DWORDmov edi,szDestStringmov al,szFillBytemov ecx,dwlenrep stosbret_myMemset endp_myStrlen proc szSrcString:DWORDmov edi,[ebp+8]mov al,0mov ecx,0fffffffhrepne scasbnot ecxmov eax,ecxret_myStrlen endp_myStrcpy proc szDestString:DWORD,szSrcString:DWORDmov eax,szSrcStringpush eaxcall _myStrlenmov ecx,eaxmov edi,szDestStringmov esi,szSrcStringrep movsbret_myStrcpy endp_myStrcmp proc szDestString:DWORD,szSrcString:DWORDmov ecx,0fffffffhmov esi,szSrcStringmov edi,szDestStringrepne cmpsbnot ecxmov eax,ecxret_myStrcmp endpmain procLOCAL szStrl[20]:BYTELOCAL dwlen:DWORD;memset(szStrl,0xcc,dwlen)push 20push 0cchlea eax,szStrlpush eaxcall _myMemset;scanf("%S",szStrl)lea eax,szStrlpush eaxpush offset szStringFormatcall crt_scanf;dwlen=strlen(strl)lea eax,szStrlpush eaxcall _myStrlen;strcpy(szStrl,)lea eax,szStrlpush eaxcall _myStrcpy;strcmplea eax,szStrlpush eaxcall _myStrcmpretmain endpentry:call mainretend entry
裸函数实现:
#include<iostream>#include <string.h>#include <memory.h>//1. memset(char* dest,int val,int len)_declspec(naked) void MyMemset(char* dest, int val, int len){_asm{push ebpmov ebp,esppush eaxpush ecxpush edimov eax,valmov ecx,lenmov edi,destrep stos edipop eaxpop ecxpop edimov esp,ebppop ebp}}//2. memcpy(char* dest, char* src, int len)_declspec(naked) void MyMemcpy(char* dest, char* src, int len){_asm{push ebpmov ebp, esppush edipush esipush ecxmov edi,destmov esi,srcmov ecx,lenrep movsb edi,esipop ecxpop esipop edimov esp, ebppop ebp}}//3. strcmp(char* dest,char* src)_declspec(naked) void MyStrcmp(char* dest, char* src){_asm{push ebpmov ebp, esppush edipush esimov edi,destmov esi,srccmps edi,esipop esipop edimov esp, ebppop ebp}}//4. int strlen(char* str)_declspec(naked) int MyStrlen(char* str){_asm{push ebpmov ebp, espmov esp, ebppop ebp}}int main(){char szString[] = "hello";MyStrlen(szString);std::cout << "hello";return 0;}
