Assembly language basics 5-an example of comparison between source code and disassembly code

Source: Internet
Author: User

The following C ++CodeWhat will the corresponding assembly code look like?

 

 # Include  <Stdio. h>  Int  Addemup ( Int , Int ); Void  Main ( Void ){ Int  X = 5; Int  Y = 10; Int Z = 0; Z = Addemup ( X , Y ); Printf ( "Z = % I \ n" , Z );} Int  Addemup ( Int  A , Int  B ){ Int C = 0; C = A + B ; Return ( C );}

 

1: # Include  <Stdio. h> 2: 3: Int  Addemup ( Int , Int ); 4: 5: Void Main ( Void ) 6 :{ Addemup ! Main : 00401000 55 Push EBP ; Save base pointer 00401001 8bec MoV EBP , ESP ; Set Stack pointer 00401003 83ec0c Sub ESP , 0xc; Make room For  Locals  // In this section, we first save the stack bottom pointer (EBP) so that it can be restored later. // Then let the stack base point to the top of the stack and use the original stack top as the bottom of the new stack. The image point is that the stack is moved up to a certain distance from the original stack length. // Here, we can see that if you want to access the function parameters, the displacement relative to EBP is positive, // and the displacement of the local variable of the function is negative. 7: Int  X = 5; 8: Int  Y = 10; 9: Int  Z = 0; 00401006 C745fc05000000 mov DWORD PTR [ EBP -0x4], 0x5;Local x = 5 0040100d C745f80a000000 mov DWORD PTR [ EBP -0x8], 0xa; Local y = 10 00401014 C745f400000000 mov DWORD PTR [ EBP -0xc], 0x0; Local Z = 0 // The PTR command is used to modify attribute operators to explicitly specify the type attributes of variables, labels, or address expressions (only valid in the instruction ). // The type can be byte, word, DWORD, near, and far before PTR. // Here the operation is to extend the operand X5 into DWORD into the memory where the address is [ebp-0x4] 10: 11: Z =Addemup ( X , Y ); 0040101b 8b45f8 MoV eax ,[ EBP -0x8]; Load eax with Y 0040101e 50 Push eax ; Push y on Stack 0040101f 8b4dfc MoV ECx ,[ EBP -0x4]; Load ECx with x 00401022 51 Push ECx ; Push X on Stack 00401023 E81b000000 call addemup ! Addemup ; Call addemup 00401028 83c408 Add ESP , 0x8; Fixup Stack  For  ARGs 0040102b 8945f4 MoV [ EBP -0xc],Eax ; Z returned in eax  // Here is the call part of the addemup function. Press X and Y into the stack as the call parameters. // Two DWORD parameters are pushed in. Each DWORD occupies 4 bytes. Therefore, the stack top pointer moves 8 bytes up after the stack is pressed. // after the call is completed, it indicates that the ESP at the top of the stack is added back to 8 bytes, that is, the stack state is restored. // Note that the returned values of function calls are generally stored in eax, // so the copy of the eax value to the [ebp-0xc] is to give Z the function return result action. // It can be seen that after the function call is completed, the callback completes the restoration of the stack top with the parameter removed. 12: 13: Printf ( "Z = % I \ n" , Z ); 0040102e 8b55f4 MoV edX ,[ EBP -0xc]; Load edX with Z 00401031 52 Push edX ; Push Z on the stack 00401032 6830704000 Push 0x407030; Push PTR to "z = % I \ N" 00401037 E822000000 call addemup ! Printf ; Call printf 0040103c 83c408 Add ESP , 0x8;Fix Stack  For  ARGs  // Here is a call to the printf function, the process is the same as the above function call. // 1. Pressure the parameter stack first; 2. Save the current stack bottom position; 3. Call the function; 4. Restore the stack top position 14:} 0040103f 8be5 MoV ESP , EBP ; Restore stack PTR 00401041 5D Pop EBP ; Restore base PTR 00401042 C3 RET ; Return // Here is the main function. The function caller is responsible for adjusting the stack top pointer. The called function body must restore the stack top and stack bottom to the status before being called. 15: 16: Int  Addemup ( Int  A , Int  B ) 17 :{ Addemup ! Addemup : 00401043 55 Push EBP ; Save base pointer 00401044 8bec MoV EBP ,ESP ; Set Stack pointer 00401046 51 push ECx; Make room  For  Local  // 1. Save the stack bottom; 2. Set the stack bottom; 3. Modify the stack top to allocate space for local variables 18: Int  C = 0; 19: 20: C = A + B 00401047 C745fc00000000 mov DWORD PTR [EBP -0x4], 0x0; Local C = 0 0040104e 8b00008 MoV eax ,[ EBP + 0x8]; Set eax to local 00401051 03450c Add eax ,[ EBP + 0xc]; Eax = Eax + Local B 00401054 8945fc MoV [EBP -0x4], Eax ; Local C = A + B  // The eax is a accumulator. 21: Return ( C ); 00401057 8b45fc MoV eax ,[ EBP -0x4]; Set eax to Result c  // Eax is used to return values. 22:} 0040105a 8be5MoV ESP , EBP ; Restore stack PTR 0040105c 5D Pop EBP ; Restore base PTR 0040105d C3 RET ; Return  To caller  // Restore the stack top to restore the stack bottom 

 

Let's summarize the stack operation in the Process of function calling. The call Convention used in this example is cdecl, followedArticle.

The caller is responsible for the following:

Phase Action Details
Before calling (status 1) None The stack now has a status before calling the function. Suppose EBP = A; esp = B;
Preparations Push all parameters to stack As the stack is pressed, the top pointer of the stack moves to the lower address. ESP = B-X. (Status 2)
Call a function None See the table below.
Finishing work Modify stack top pointer Restore to status 1. ESP = B, EBP =

 

The caller is responsible for the following:

Phase Action Details
Initial Function (status 2) None The stack has a status. Suppose EBP = A; esp = B-X;
Initialization 1. Save the stack bottom; 2. Set the stack bottom; 3. Modify the stack top to allocate space for local variables. EBP = B-X; esp = B-x-y; Value A is in the stack. (Status 3)
Execution function body None  
Finishing work 1. Restore the stack top, 2. Restore the stack bottom Restore to status 2. ESP = B-X; EBP = A; (status 2)

Table note:

X is the space occupied by parameters.

Y is the space occupied by local variables.

 

It's a little troublesome. You have to think about it several times before you can remember it. However, I believe that the assembly code can be read more, and it will naturally become very familiar to me when I see more.

 

Exercise:

If (I = 0) goto label1;

C code:

Corresponding assembly code:

MoV eax, [EBP-4]; here we assume that I is the first local variable. If it is the first parameter, it should be [EBP + 8]

CMP eax, 0x0

JZ label1

 

Problem:

Why is the purpose of pushing ECx under the seventh line in the code "Make room for local? How to do it?

A: The comment is incorrect. The purpose of pushing ECx is to save the value of ECx.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.