Ubuntu call Function Analysis

Source: Internet
Author: User
As the saying goes, the second line is like a mountain (well, I said ). Different industries have different industry rules, and the Ubuntu operating system mentioned today is also the case. Today, I just have a flash. The program is very simple, probably like this. # Define deintmain () {char * msg = "mnzn"; printf ("msgis % s", msg); return0;} is simple, but let me write assembly on the 64-bit x86-ubuntu machine is wrong, just

As the saying goes, the second line is like a mountain (well, I said ). Different industries have different industry rules, and the Ubuntu operating system mentioned today is also the case. Today, I just have a flash. The program is very simple, probably like this.

# Include
Int main ()
{
Char * msg = "mnzn ";
Printf ("msg is % s", msg );
Return 0;
}

Very simple, but let me in x86-ubuntu 64-bit machine on How to Write assembly is wrong, just suddenly opened, disassembly looked at, all the mysteries of the mind explained clearly.
 
1 | # include
2 |
3 | int main ()
4 | {
5 | int a = 10;
6 | char B = 'a ';
7 |
8 | printf ("the number is % d, char is % c \ n", a, B );
9 | return 0;
10 |}


Next let's take a look at the results of the disassembly.

File Edit Options Buffers Tools Help
1 |. section. rodata
2 |. LC0:
3 |. string "the number is % d, char is % c \ n"
4 |
5 |. text
6 |. globl _ start
7 | _ start:
8 | # init stack frame
9 | pushq % rbp
10 | movq % rsp, % rbp
11 |
12 | # get local var
13 | subq $16, % rsp
14 |
15 | # int a = 10
16 | movl $10,-8 (% rbp)
17 |
18 | # char B = 'A'
19 | movb $97,-1 (% rbp)
20 |
21 | # call printf
22 | movsbl-1 (% rbp), % edx
23 | movl-8 (% rbp), % esi
24 | movl $. LC0, % edi
25 | movl $0, % eax
26 | call printf
27 |
28 | # call exit
29 | movl $0, % edi
30 | xor % eax, % eax
31 | call exit

---- 31 |-cc-: --- F1 print. s ?? 1 ?? 24 (robot ER) -- L31--C17 -- Bot
End of buffer


Results from our lovely emacs

Analysis:
Initialize the stack frame at line 9 and 10
13 opening up space for behavior local variables
Assign values to local variables at 16, 19

Today, the main character is the function call that started with 22 rows. Here I made a too subjective mistake. I think that the operating system I use also uses the stack to pass parameters, the result is a segment error no matter how it is written. Alas, let's take a look. If there are three parameters, the first parameter uses edi, the second parameter uses esi, and the third parameter uses edx, and eax should be cleared to 0 as the return value.

I used the above method to call exit without any errors. In addition, even if eax is not cleared to 0, there is no error in the program, but there is no information at hand. (Author: Mu you)

Related Article

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.