main函數的參數__網絡編程之參數學習

是否還記得ppt上的這張圖片呢?

這個程序可以帶參數,是不是和以前的程序不一樣呢?

下面我們就來實現,這可花了我半個小時的時間哦!!!

首先,我們和以前寫C語言程序一樣,新建一個工程,工程的名字當然是vc啦

然後選擇A simple application,然後點擊finish,完成工程的創建

開始寫代碼啦,雙擊打開vc.cpp文件

開始下面的代碼,應該能看懂吧,嘿嘿

 1 #include "stdafx.h"
 2 #include "stdio.h"
 3 
 4 int main(int argc, char* argv[])
 5 {
 6     if (argc==1)
 7     {
 8         printf("\n\n The content in argv[0] is :%s \n\n",argv[0]);
 9     }
10     if (argc==2)
11     {
12         printf("\n\n The command includes 1 parameter: %s \n\n",argv[1]);
13         printf("The content in argv[0] is :%s \n\n",argv[0]);
14     }
15 
16     if (argc>2)
17     {
18        printf("\n\n bad command !\n");
19     }
20  return 0;
21 }

 

這樣就OK了哦,很簡單吧!

 

 

===================下面是我的東西了,hoho~~~====================

反彙編,找到主函數

1 00401256  |.  52            push    edx
2 00401257  |.  A1 847C4200   mov     eax, dword ptr [__argv]
3 0040125C  |.  50            push    eax
4 0040125D  |.  8B0D 807C4200 mov     ecx, dword ptr [__argc]
5 00401263  |.  51            push    ecx
6 00401264  |.  E8 9CFDFFFF   call    00401005

運行到00401257看看情況

進入數據窗口查看003D0D90

看出來是什麼沒?就是argv[0]啦。

繼續運行到0040125D

只有一個參數,是不是,因爲我們沒有輸入

下面是主函數

 1 00401005  /$ /E9 06000000   jmp     main
 2 0040100A  |  |CC            int3
 3 0040100B  |  |CC            int3
 4 0040100C  |  |CC            int3
 5 0040100D  |  |CC            int3
 6 0040100E  |  |CC            int3
 7 0040100F  |  |CC            int3
 8 00401010 >|> \55            push    ebp
 9 00401011  |.  8BEC          mov     ebp, esp
10 00401013  |.  83EC 40       sub     esp, 40
11 00401016  |.  53            push    ebx
12 00401017  |.  56            push    esi
13 00401018  |.  57            push    edi
14 00401019  |.  8D7D C0       lea     edi, dword ptr [ebp-40]
15 0040101C  |.  B9 10000000   mov     ecx, 10
16 00401021  |.  B8 CCCCCCCC   mov     eax, CCCCCCCC
17 00401026  |.  F3:AB         rep     stos dword ptr es:[edi]
18 00401028  |.  837D 08 01    cmp     dword ptr [ebp+8], 1
19 0040102C  |.  75 13         jnz     short 00401041
20 0040102E  |.  8B45 0C       mov     eax, dword ptr [ebp+C]
21 00401031  |.  8B08          mov     ecx, dword ptr [eax]
22 00401033  |.  51            push    ecx                              ; /<%s>
23 00401034  |.  68 90204200   push    00422090                         ; |format = LF,LF," The content in argv[0] is :%s ",LF,LF,""
24 00401039  |.  E8 82000000   call    printf                           ; \printf
25 0040103E  |.  83C4 08       add     esp, 8
26 00401041  |>  837D 08 02    cmp     dword ptr [ebp+8], 2
27 00401045  |.  75 27         jnz     short 0040106E
28 00401047  |.  8B55 0C       mov     edx, dword ptr [ebp+C]
29 0040104A  |.  8B42 04       mov     eax, dword ptr [edx+4]
30 0040104D  |.  50            push    eax                              ; /<%s>
31 0040104E  |.  68 5C204200   push    0042205C                         ; |format = LF,LF," The command includes 1 parameter: %s ",LF,LF,""
32 00401053  |.  E8 68000000   call    printf                           ; \printf
33 00401058  |.  83C4 08       add     esp, 8
34 0040105B  |.  8B4D 0C       mov     ecx, dword ptr [ebp+C]
35 0040105E  |.  8B11          mov     edx, dword ptr [ecx]
36 00401060  |.  52            push    edx                              ; /<%s>
37 00401061  |.  68 34204200   push    00422034                         ; |format = "The content in argv[0] is :%s ",LF,LF,""
38 00401066  |.  E8 55000000   call    printf                           ; \printf
39 0040106B  |.  83C4 08       add     esp, 8
40 0040106E  |>  837D 08 02    cmp     dword ptr [ebp+8], 2
41 00401072  |.  7E 0D         jle     short 00401081
42 00401074  |.  68 1C204200   push    0042201C                         ; /format = LF,LF," bad command !",LF,""
43 00401079  |.  E8 42000000   call    printf                           ; \printf
44 0040107E  |.  83C4 04       add     esp, 4
45 00401081  |>  33C0          xor     eax, eax
46 00401083  |.  5F            pop     edi
47 00401084  |.  5E            pop     esi
48 00401085  |.  5B            pop     ebx                              ;  7FFD5000
49 00401086  |.  83C4 40       add     esp, 40
50 00401089  |.  3BEC          cmp     ebp, esp
51 0040108B  |.  E8 B0000000   call    _chkesp
52 00401090  |.  8BE5          mov     esp, ebp
53 00401092  |.  5D            pop     ebp
54 00401093  \.  C3            retn

很簡單,不解釋。

轉載於:https://www.cnblogs.com/tk091/archive/2012/09/28/2707694.html