筆者在winDBG中反匯編一個小程序的main函數, 看到了如下的一段代碼:
0:000> uf .
monitor!main [c:\users\myalias\documents\visual studio 2005\projects\mytest\mytest\main.c @ 32]:
32 0042f780 55 push ebp
32 0042f781 8bec mov ebp,esp
32 0042f783 81eccc000000 sub esp,0CCh
32 0042f789 53 push ebx
32 0042f78a 56 push esi
32 0042f78b 57 push edi
32 0042f78c 8dbd34ffffff lea edi,[ebp-0CCh]
32 0042f792 b933000000 mov ecx,33h
32 0042f797 b8cccccccc mov eax,0CCCCCCCCh
32 0042f79c f3ab rep stos dword ptr es:[edi]
有點不明白高亮語句的作用, 在stackoverflow的一篇帖子中找到了答案.
; This puts the address of the stack frame bottom (lowest address) into edi...
lea edi,[ebp-0C0h]
; ...and then fill the stack frame with the uninitialised data value (ecx = number of
; dwords, eax = value to store)
mov ecx,30h
mov eax,0CCCCCCCCh
rep stos dword ptr es:[edi]
即, rep指令的目的是重復其上面的指令. ECX的值是重復的次數.
所以, 我列出的代碼的作用是將棧上從ebp-0xcc開始的位置向高地址方向的內存賦值0xCCCCCCCC,次數重復0x33(51)次. 注意0xCCCCCCCC代表着未被初始化.
把STOS和REP的功能弄清楚, 上面的答案就不難理解了.
=================
STOS指令的作用是將eax中的值拷貝到ES:EDI指向的地址. 如果設置了direction flag, 那么edi會在該指令執行后減小, 如果沒有設置direction flag, 那么edi的值會增加, 這是為了下一次的存儲做准備.
REP可以是任何字符傳指令(CMPS, LODS, MOVS, SCAS, STOS)的前綴. REP能夠引發其后的字符串指令被重復, 只要ecx的值不為0, 重復就會繼續. 每一次字符串指令執行后, ecx的值都會減小.
參考資料
===================
Can anyone help me interpret this simple disassembly from WinDbg?
STOS
http://www.cs.ubbcluj.ro/~dadi/ac/doc/ng1cf0a.html
REP