部分C庫函式重寫以及反彙編分析之memset

2021-08-27 04:22:16 字數 4228 閱讀 1729

/*///

函式解釋:將s中前n個位元組替換為ch並返回s;   

memset:作用是在一段記憶體塊中填充某個給定的值,它是對較大的結構體或陣列進行清零操作的一種最快方法。

///*/

#include "stdafx.h"

void *pmemset(void *s , char ch , int n);

int main(int argc, char* argv)

void *pmemset(void *s , char ch , int n)

return s;

}

**很簡單,每天堅持,確實可以感受到明顯的進步,特備是反彙編分析能力。加油!!

release編譯,od載入分析如下:

在release編譯模式下,rep stos 配合填充,可以使dword 、byte快速初始化。

00401000  /$  83ec 14       sub esp,14                               ;  開闢14h(20)位元組大小的記憶體空間留個變數(陣列a[20])

00401003 |. 8d4424 00 lea eax,dword ptr ss:[esp] ; eax存放位址a

00401007 |. 50 push eax ; 位址a入棧

00401008 |. 68 30704000 push memset.00407030 ; ascii "%s

"0040100d |. e8 6e000000 call memset.00401080 ; printf

00401012 |. 6a 0a push 0a ; 引數3:10 入棧

00401014 |. 8d4c24 0c lea ecx,dword ptr ss:[esp+c] ; ecx存放位址a

00401018 |. 6a 61 push 61 ; 『a』入棧

0040101a |. 51 push ecx ; 位址a入棧

0040101b |. e8 20000000 call memset.00401040 ; memset()

00401020 |. 8d5424 14 lea edx,dword ptr ss:[esp+14]

00401024 |. 52 push edx

00401025 |. 68 30704000 push memset.00407030 ; ascii "%s

"0040102a |. e8 51000000 call memset.00401080

0040102f |. 33c0 xor eax,eax

00401031 |. 83c4 30 add esp,30

00401034 \. c3 retn

00401035 90 nop

00401036 90 nop

00401037 90 nop

00401038 90 nop

00401039 90 nop

0040103a 90 nop

0040103b 90 nop

0040103c 90 nop

0040103d 90 nop

0040103e 90 nop

0040103f 90 nop

00401040 /$ 8b4424 0c mov eax,dword ptr ss:[esp+c] ; memset入口位址 eax存int n=0ah

00401044 |. 8bc8 mov ecx,eax ; while(n--)

00401046 |. 48 dec eax

00401047 |. 85c9 test ecx,ecx

00401049 |. 74 30 je short memset.0040107b

0040104b |. 8d48 01 lea ecx,dword ptr ds:[eax+1] ; ecx=n-1

0040104e |. 8a4424 08 mov al,byte ptr ss:[esp+8] ; al存『a』

00401052 |. 53 push ebx

00401053 |. 8ad8 mov bl,al

00401055 |. 8afb mov bh,bl ; bh='a'

00401057 |. 56 push esi

00401058 |. 8b7424 0c mov esi,dword ptr ss:[esp+c] ; esi存放位址a

0040105c |. 8bc3 mov eax,ebx ; 'a'存入eax

0040105e |. 8bd1 mov edx,ecx ; edx=n-1

00401060 |. 57 push edi

00401061 |. c1e0 10 shl eax,10 ; eax邏輯左移10h位

00401064 |. 8bfe mov edi,esi ; edi存放位址a

00401066 |. 66:8bc3 mov ax,bx

00401069 |. c1e9 02 shr ecx,2 ; ecx右移2位 ecx為後面重複次數 ecx=10b

0040106c |. f3:ab rep stos dword ptr es:[edi] ; 將ax值重複存放到目的串符號位址(位址a),即填充,一次填充8個a

0040106e |. 8bca mov ecx,edx

00401070 |. 83e1 03 and ecx,3 ; ecx=2

00401073 |. f3:aa rep stos byte ptr es:[edi] ; 填充了2個a 此處是以byte填充的

00401075 |. 8bc6 mov eax,esi

00401077 |. 5f pop edi

00401078 |. 5e pop esi

00401079 |. 5b pop ebx

0040107a |. c3 retn

0040107b |> 8b4424 04 mov eax,dword ptr ss:[esp+4]

0040107f \. c3 retn

部分C庫函式重寫以及反彙編分析之memch

從buf所指記憶體區域的前count個位元組查詢字元ch。說明 當第一次遇到字元ch時停止查詢。如果成功,返回指向字元ch的指標 否則返回null。include stdafx.h include void pmesch void buf int ch int count int main int ...

C 反彙編 if語句分析

include void main std cout hello world 7?1 4 if b 1 std cout b equal 1 判斷語句有幾種 1.條件表示式 表示式1?表示式2 表示式3 2.if語句 3.switch語句 首先來分析if語句吧 debug版 0040117d 68 ...

C 遞迴函式反彙編

源 include using namespace std int sumrecursion int arr,int n return 0 int main int sum sumrecursion arr,5 cout sum sum endl 反彙編 重要部分已經注釋,可以看到.text 004...