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

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

Post by m0skit0 » Fri Jul 15, 2011 7:21 pm

Strangelove wrote:but modern compilers usually skip that step and instead of generating assembly it just generates machine-code directly.
What do you mean by "moden" compilers? Which programming language are you referring to? Can you please name a compiler that does so?
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

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 7:45 pm

Err... what I can claim to know. GCC does at least.

It's just a simple optimization. Since assembly language reflects binary code so directly, it makes a lot of sense to just skip the whole assembly step altogether and the compiler generates binary code directly without going through the step of translating assembly to binary code.

Traditionally making programs did indeed involve the assembly step you mentioned.
Advertising
"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

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 » Fri Jul 15, 2011 8:25 pm

Strangelove wrote:what I can claim to know. GCC does at least.
I think not. GCC generates assembly code first. Not showing you the assembly doesn't mean it does not generate it. The -S switch actually makes gcc stop when assembly is generated. It does not modify GCC behavior.
GNU wrote:If you only want some of the stages of compilation, you can use -x (or filename suffixes) to tell gcc where to start, and one of the options -c, -S, or -E to say where gcc is to stop.
http://gcc.gnu.org/onlinedocs/gcc/Overa ... ll-Options
Strangelove wrote:Since assembly language reflects binary code so directly, it makes a lot of sense to just skip the whole assembly step altogether and the compiler generates binary code directly without going through the step of translating assembly to binary code.
You're missing a point here. GCC does not generate any binary code by itself. GAS does. You would need to incorporate GAS into GCC, which is just useless and less flexible. Why would anyone do that? What are the benefits? Remove GAS and try compiling with GCC...

You're also missing GNU (and UNIX) philosophy: do not make big complicated tools that pretend doing everything. Make small tools that chained together can solve complicated problems. This is a typical programmer's thinking: divide and win. Why incorporate GAS into GCC when you already have GAS? Same goes with some other programming languages that are actually translated into C before being compiled.

Cheers
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

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 » Sat Jul 16, 2011 4:59 am

However, many modern compilers will skip the assembly language and create the machine code directly.
Source: http://en.wikibooks.org/wiki/X86_Assemb ... _Languages

... not so sure about that... how can it skip assembly language...
Image

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

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

Post by Strangelove » Sat Jul 16, 2011 10:07 am

Thanks for link asgard. Looks like I remembered wrong about gcc. At least I can't find any hints that it uses this kind of optimization. But as the article says some compilers do I'm just to lazy to go digging up which ones.

I did take a look at GCC though and what tool optimisations it uses. It still has a standalone preprocessor, and it does do this step but the preprocessor used is integrated. source: http://gcc.gnu.org/onlinedocs/gnat_ugn_ ... ssing.html

There used to be a GASP program too (GNU assembler preprocessor) but I can't find it on my system. It served basically the same function as CPP did for the C compiler. Looking at the info file for GAS it says it uses an integrated preprocessor, but that you can use CPP if you need to do more fancy stuff.

As for the other steps, there are two tricks I've used in the past to speed up compiling with GCC. The first is to instruct GCC to use a PIPE instead of an intermediate file to send data between the various stages. You can also instruct GCC to use a filesystem based in RAM, it's called tmpfs unless they changed the name.

I suppose it makes sense for gcc not to skip the assembly step. gcc supports a lot of architectures, so their reason for using a separate assembler is to keep GCC portable. it makes more sense for compilers that only support one processor architecture. since code-generation is the last step performed by the compiler it might as well emit binary code directly instead of assembler. m0skit0 said the assembler would have to be integrated, this isn't right. there's would be nothing to assemble (!). a code-generator built this way does the transition from intermediate code to either binary code directly (or to assembly code).

On GCC making a program would go something like:

Program: C compiler
- PreProcess
- Compile: Translate C code to intermediate code (machine independent)
- Compile: Analyze and optimize Intermediate code
- Compile: Code generation, emit assembly code (machine dependent)
Program: Assembler
- Assemble: translate assembly to machine code
Program: Linker
- Linking: Combine machine code snippets into a complete program.

Another compiler suite could do this:

Program: Preprocessor
- Preprocess
Program: C compiler
- Compile: Translate C code to intermediate code (machine independent)
- Compile: Analyze and optimize Intermediate code
- Compile: Code generation, emit assembly code or binary code (machine dependent)
Program: Linker
- Linking: Combine machine code snippets into a complete program.

And this compiler suite could include a standalone assembler too.
Last edited by Strangelove on Sat Jul 16, 2011 1:38 pm, edited 4 times in total.
"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

Terbaddo
Posts: 116
Joined: Mon Feb 14, 2011 3:29 pm
Location: Paris, France
Contact:

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

Post by Terbaddo » Sat Jul 16, 2011 11:38 am

Clang doesn't generate machine code itself ?
That is not Unix. That is OSX a proprietary branch of BSD.
MixShark NGPToolChain Dev Repository Homebrew World

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

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

Post by Strangelove » Sat Jul 16, 2011 2:21 pm

Terbaddo wrote:Clang doesn't generate machine code itself ?
Hard to tell, the documentation is confusing and written for compiler geeks. But I get the impression that their code-generator is capable of this.

I also noticed that they include an assembler in their code generator to support inline assembler. Seems counter-productive to me, it would be less bloat to just chop out the assembler code and let a separate assembler handle cases like this. I don't imagine normal programs use much inline assembly.
"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

User avatar
pspfanMOHH
Posts: 660
Joined: Sat Jun 11, 2011 9:16 pm
Location: Grand Line, New World

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

Post by pspfanMOHH » Sun Jul 07, 2013 5:11 pm

irfanhb7 wrote:This is exactly what my teacher told but you explained it a much better way.
He could be you teacher in real. 8-)

Post Reply

Return to “Programming and Security”