Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (II)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Tutorial] Introduction to programming using C (II)

Post by m0skit0 » Sun May 15, 2011 12:31 pm

Back to index
<< Prev Next >>

Compiling process

Before heading into C syntax and programming, it's interesting to know how a C program is compiled. There are several programs involved on the process, so knowing them, knowing what they do and how to whole process fits together is very important to anyone interested in programming.

First thing to keep in mind is that the computer only understands a sequence of zeroes and ones called machine code. So a computer never understands C, Java or even assembly. All programming languagues need translation to machine code. For C, this is done using the C compiler suite.

1. Preprocessing

The preprocessor is a tool that formats the C code for the compiler. It processes the preprocessor directives, like #include or #define. Each of these directives tells the preprocessor to do something related to replacing, deleting or including chunks of C code. This involves no compiling C yet.

2. Compiling

Once the preprocessor has finished preparing the source code, the compiler transforms the C code into assembly. The compiler applies several optimizations to the code to reduce memory consumption and enhance speed. Thus the compiler generates .s files with the assembly.

3. Assembling

Once the compiler has translated all the C code into assembly, the assembler takes the assembly code and assembles it into what is called object code (.o files). This is effective machine code that the machine can understand, but it still needs external symbols (e.g. printf) to be resolved, and has no executable structure.

4. Linking

Once the object code is generated by the assembler, the linker resolves all external symbols linking the libraries (external code) needed.

5. Executable format

Once linking has been finished, the machine code is structured according to the target executable. This process and tool used differs for each OS, but most common executable formats are Microsoft's Win32 EXE and the Executable and Linkable Format (ELF) (used by most POSIX systems and also all PlayStation consoles). After this step is finished the program is ready to execute on the target architecture.

<< Prev Next >>
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (II)

Post by irfanhb7 » Sun May 15, 2011 6:06 pm

This is exactly what my teacher told but you explained it a much better way.
Advertising
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (II)

Post by ASKidwai » Mon May 16, 2011 3:45 am

I see but I don't understand that when we make our own executable, we have to write ./programname but when we use someone else's program (say for example, pspsh) we just have to type pspsh
Image
Image
Image
Image

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: [Tutorial] Introduction to programming using C (II)

Post by JJS » Mon May 16, 2011 5:52 am

ASKidwai wrote: ./programname
That is just because of the default searching order for files on a usual Linux installation (specified by the PATH environment variable). The current directory is not searched for the file, therefore you have to explicitly state that the shell must look in the current directory (denoted by "."). This is different on Windows, where the current directory always comes first in the search order. If you want the Windows behaviour on Linux, you can add . to your PATH variable (before someone jumps on this, I know that this is a security "feature" so only do it if you are feeling brave ;) )


vvv It is a system setting. Those applications just happen to be in folders that are specified in the PATH variable (type export to see all variables).

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (II)

Post by ASKidwai » Mon May 16, 2011 5:55 am

Oh, OK. And all the pre-made applications already have the path specified then?
Image
Image
Image
Image

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (II)

Post by m0skit0 » Mon May 16, 2011 11:03 am

They're installed on the default paths. Let's not get too much off-topic...
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

ASKidwai
Posts: 937
Joined: Mon Jan 10, 2011 7:42 am
Location: 'Ere and There
Contact:

Re: [Tutorial] Introduction to programming using C (II)

Post by ASKidwai » Mon May 16, 2011 11:29 am

m0skit0 wrote:They're installed on the default paths. Let's not get too much off-topic...
OK. Sorry for hijacking your thread(s)
Image
Image
Image
Image

asgard20032
Posts: 186
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: [Tutorial] Introduction to programming using C (II)

Post by asgard20032 » Fri Jul 15, 2011 3:22 pm

m0skit0 wrote: 2. Compiling

Once the preprocessor has finished preparing the source code, the compiler transforms the C code into assembly. The compiler applies several optimizations to the code to reduce memory consumption and enhance speed. Thus the compiler generates .s files with the assembly.

3. Assembling

Once the compiler has translated all the C code into assembly, the assembler takes the assembly code and assembles it into what is called object code (.o files). This is effective machine code that the machine can understand, but it still needs external symbols (e.g. printf) to be resolved, and has no executable structure.
Just wanted to know if its possible to see the generated assembly.
I use GCC under linux, or Mingwe under windows.

It would be fun to create basic program, see how it is in assembly, for learning purpose, since tutorial for assembly is not alway easy to understand.
Image

User avatar
codestation
Big Beholder
Posts: 1660
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: [Tutorial] Introduction to programming using C (II)

Post by codestation » Fri Jul 15, 2011 3:27 pm

asgard20032 wrote: Just wanted to know if its possible to see the generated assembly.
I use GCC under linux, or Mingwe under windows.

It would be fun to create basic program, see how it is in assembly, for learning purpose, since tutorial for assembly is not alway easy to understand.
Use the -S switch for that:

Code: Select all

gcc -S sourcefile.c
Plugin list
Working on: QPSNProxy, QCMA - Open source content manager for the PS Vita
Playing: Error: ENOTIME
Repositories: github, google code
Just feel the code..

Strangelove
Posts: 286
Joined: Thu Nov 25, 2010 6:32 pm

Re: [Tutorial] Introduction to programming using C (II)

Post by Strangelove » Fri Jul 15, 2011 6:32 pm

:) generating assembly from C is a neat trick.

often used by people to hand-optimize code to tweak the last sweatdrop out of a CPU.
popular on the assembly compo in finland.

but modern compilers usually skip that step and instead of generating assembly it just generates machine-code directly.
It's a 1:1 translation so it makes a lot of sense.
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard

Post Reply

Return to “Programming and Security”