Problems related to the pressure stack of C-language function call parameters

Source: Internet
Author: User

Order of arguments into the stack

Before the interview was asked this question, when the function call, the parameter in the order of the stack is left to right, or from right to left. It was not clear at the time, casually said from right to left. In fact, this answer is not entirely correct. Because, in fact, in the order of the stack, different architectures are not the same, for example, look at the following code:

#include <stdio.h>intTestintAintb) {printf("Address of a%x.\n", &a);printf("Address of B%x.\n", &b);if((unsigned int) &a > (unsigned int) {&b) {printf("Push argument from left to right...\n"); }Else{printf("Push argument from right to left...\n"); }return 0;}intMain () {test (1,2);return 0;}

The results of running on 64-bit Ubuntu systems are: left to right.

Address of a 1ec62c.
Address of B 1ec628.
Push Argument from ...

The result of 32-bit Ubuntu is: right to left

Address of a bfd03290.
Address of B bfd03294.
Push Argument from ...

First to explain why the above code can determine the order of the stack, first you need to understand that:

The C language of the GCC compiler stack is from high address to low address growth, that is, who first into the stack, whose address is large, mastered this, the test is not difficult to write code.

What's in the stack when the function is called

Take the argument from left to right into the stack as an example:

push arg0 -- High Addresspush arg1...push argnpush eippush ebp  -- Low address
When 32-bit systems and 64-bit system functions are called, is there a difference in the way parameters are stacked?

This problem was a problem in the near future, then silly, I have been only concerned about the 32-bit system parameters into the stack, always thought 64-bit system is the same, no difference, now summed up has two points:

  • 64-bit system first put the incoming parameters in the register, in the specific implementation of the function to put the value of the register into the stack, and then go to the stack to take parameters
  • The order of the parameters in the 64-bit system stack is left-to-right (because the register is passed through the value first)

Look at the disassembly below:

C code same as above Ubuntu +Bit disassembly:intMain () {804846D: -                      Push   %EBP 804846E: theE5 mov%esp,%EBP 8048470: theE4 f0                 and     $Xfffffff0,%esp 8048473: theEcTen                 Sub $0x10,%esp Test (1, 2); 8048476: C7 -  - Geneva  Geneva xx xxMovl $X2,0x4(%esp)804847D:xx  804847E:c7Geneva  -  on xx xx xxMovl $X1, (%esp)8048485: E88A FF FF call8048414<test>return 0;804848A:b8xx xx xx xxmov $x0,%eax}intTestintAintb) {8048414: -                      Push   %EBP 8048415: theE5 mov%esp,%EBP 8048417: theEc -                 Sub $0x18,%esp printf ("Address of a%x.\n", &a); 804841A:b8 -  - GenevaAbout MOV $x8048560,%eax 804841F:8D -Lea0x8(%EBP),%edx 8048422: the  Wu  - Genevamov%edx,0x4(%esp)8048426: the Geneva  -mov%eax,(%esp)8048429: E8 AFF FF FF call8048340<printf@plt>return 0;8048466: B8xx xx xx xxmov $x0,%eax}ubuntu -Bit disassembly:intMain () {40056E: -                      Push   %RBP  40056F: -  theE5 mov%RSP,%RBPTest1,2);400572: Be Geneva xx xx xxmov $X2,%esi  400577: BF on xx xx xxmov $X1,%edi  40057C:E8 AC FF FF FF CALLQ40052D <test>return 0;400581: B8xx xx xx xxmov $x0,%eax}intTestintAintb) {40052D: -                      Push   %RBP  40052E: -  theE5 mov%RSP,%RBP  400531: -  theEcTen              Sub $0x10,%rsp 400535:89 7d FC mov%edi,-0x4 (%RBP) 400538:89-F8 mov%esi,-0x8 (%RBP) printf ("Address of a%x.\n", &a);  40053B: - 8D $FC Lea-0x4(%RBP),%rax  40053F: -  theC6 mov%rax,%rsi  400542: BF -  .  + xxmov $x400614,%edi  400547: B8xx xx xx xxmov $x0,%eax  40054C:E8 BF FE FF FF CALLQ400410<printf@plt>return 0;400567: B8xx xx xx xxmov $x0,%eax}

Look at the 32-bit Ubuntu operating system, 8048476: It is really the parameters directly into the stack, 2 first into the stack, 1 after the stack.

8048476  c7 44 24 04 02 00 00    movl   $0x2,0x4(%esp)804847d  00 804847e  c7 04 24 01 00 00 00    movl   $0x1,(%esp)8048485  e8 8a ff ff ff          call   8048414 <test>

Looking at the 64-bit Ubuntu OS, 2 and 1 are not put into the stack at all, but are placed in the register ESI and EDI.

  40056F: -  theE5 mov%RSP,%RBPTest1,2);400572: Be Geneva xx xx xxmov $X2,%esi  400577: BF on xx xx xxmov $X1,%edi  40057C:E8 AC FF FF FF CALLQ40052D <test>

Then look at the implementation of the 64-bit system test, the EDI into the stack, and then the ESI into the stack, which is why the function looks like from left to right into the stack of reasons.

  40052d  55                      push   %rbp  40052e  48 89 e5                mov    %rsp,%rbp  400531  48 83 ec 10             sub    $0x10,%rsp  400535  89 7d fc                mov    %edi,-0x4(%rbp)  400538  89 75 f8                mov    %esi,-0x8(%rbp)

Problems related to the pressure stack of C-language function call parameters

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.