Machine-level representation of the C program-0

Source: Internet
Author: User

/*************************************** ******/
Text: cProgramMachine-level representation
Author: Arden Chao
Date: 2006-10-17
Email: arden1019@gmail.com
Version: 1.0.0
/*************************************** ******/

References:
China Power Press, 2004, ISBN 7-5083-2175-8, [US] Randal E. by Bryant David O 'allaron, translated by Edison Lee and Lei Yingchun.
C/C ++ deep exploration, edited by Yao xinyan, China Post and Telecommunications Press, 2002, ISBN 7-115-42515-x.
"GNU/Linux programming guide" translated by Zhang Hui, Tsinghua University Press, 2001, ISBN 7-302-05550-5/tp.3273 [us] Kurt wall.
"Advanced programming in UNIX environment", published by Mechanical Industry Press, 2000, translated by ISBN 7-111-07579-x [us] W. Richard Steven S, Yu jinyuan.

////////////////////////
// Preface and preparation
////////////////////////

Before encountering csapp, when encountering some strange problems in C language, I started to try the compilation from the compiler.Code. However, it is not a good choice to compile the assembly code to analyze problems from a large number of hard-to-read codes. So I thought, could you compile a reference manual like this, and compare it in C language and assembly. For example, how does one express the Assembly level of the switch statement? Then I can easily find the answer in this reference manual, without having to write another program for this purpose. However, at that time, I had no such ability or time to complete this task.

Soon after, I met csapp by chance. After reading a few simple pages, I found that this is what I was looking. As a result, Mr. Lei Yingchun's translation was bought from China-Pub. The third chapter mainly describes the compilation of C Programs and the expression of machine-level code. From the basic commands and addressing methods of assembly to the analysis of specific C language modules, the association between C language and assembly is clearly presented to me.

Today, I want to record some of my attempts in this region. I hope I can take responsibility for my understanding, and I also hope I can give me the same help as I did. So far, although Java and dot net are prevalent, I think that to become a top-notch programmer, we still need to be closer to the system itself. These analysis capabilities should at least be available.

This is the preface, and the following is the preparation.

If you want to share this immature text with me, you may need to understand the C language and the assembly language syntax. In college, you may need to compile things in the classroom and use them directly. When talking about the addressing method of XXX, don't avoid it. Even if you can't give me the answer right away, you should google it like me, and review it in a short time.
Also, you have to use a C compiler. We will use GCC as the compiler for this text. On Windows, you can install Dev-CPP to get an IDE environment, which includes the compiler we need. In Linux, the operating system should already contain the GCC environment. The MS compiler is not used because I have used vs In the IDE environment and Cl has been used several times, but it is better to find a familiar compiler. I don't know how much this document will develop. If we need to analyze the implementation of the compiler that day, our GCC will at leastSource codePut it in front of us, and we cannot determine the action of Cl. Of course, there are many c compilers, but I am not familiar with them :(.

The old habit is to give the simplest analysis method from a simple example:

We provide a simple C code and demonstrate how to get the assembly code.
1: Open Dev-CPP, enter the following test code, and save it as the asm01.c file.
2: start -- run -- cmd. In command line mode, switch the directory to the directory where asm01.c is located.
3: Enter the following command: gcc-O2-s asm01.c

Note:
1: In the GCC command, both O and s are in uppercase. O2 indicates level 2 optimization, and s indicates that only compilation is performed to generate. s assembly code files.
2: If the command is not found when you run GCC on the command line, you need to add the GCC directory to the path of the system environment variable, the GCC directory is in the bin directory of the dev-CPP directory. For example, on my machine, I added the following project C:/dev-CPP/bin IN THE PATH environment variable. Remember, use; separator.
3: My machine environment is PIII-800/win2000p SP4/dev-CPP

/// // C code /////////////////// ////
// File name: asm01.c
Int test (int)
{
Int B =;
Return B;
}

/// // ASM code /////////////////////
// File name: asm01.s

. File "asm01.c"
. Text
. P2align 4, 15
. Globl _ Test
. Def _ test;. SCL 2;. Type 32;. endef
_ Test:
Pushl % EBP
Movl % ESP, % EBP
Movl 8 (% EBP), % eax
Popl % EBP
RET

Write so much. Next time, I plan to review the compiler together.

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.