LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2011, 01:02 PM   #1
ashish anand
LQ Newbie
 
Registered: Oct 2009
Posts: 23

Rep: Reputation: 0
Unhappy Assembly Language


Well it might be a very basic question but I am still confused .

Whenever we write an architecture specific code we write it in assembly language(which gets converted into machine lang by assemblers).

But when we write a normal c code our compiler changes it into assembly code which is again architecture specific.And for different architectures we have different compilers changing into architecture specific code .So then what is the necessasity of writing in assembly language.Why cant all the code written in c language and let the compilers change them into assembly language.As for arm architecture there are arm gcc toolchain converting c code into assembly specific to arm architecture.

Please correct me if I am wrong as I am new to these topics.


Thanks in advance
 
Old 10-17-2011, 01:11 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
While todays compilers are pretty clever there's always hope that
some humans do better at optimising the way an algorithm may be
written/implemented in assembly/machine code. I will hazard the guess
(hope?) that there may be some such smart people in your company.


Cheers,
Tink


P.S.: Moved to programming for better exposure.
 
Old 10-17-2011, 01:26 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by ashish anand View Post
Whenever we write an architecture specific code we write it in assembly language(which gets converted into machine lang by assemblers).
Well, one legitimate reason to do this is if the assembly code is so low-level that it would be impossible to express in C.
 
Old 10-17-2011, 02:02 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by dugan View Post
Well, one legitimate reason to do this is if the assembly code is so low-level that it would be impossible to express in C.
This is probably the only good reason to use assembler code these days. Trying to out-optimize a modern C compiler is very difficult.
I don't think it is still the case that assembler code is generated as an intermediate step in modern compilers (gcc). It is still possible to generate assembler output (gcc's '-S' option), but it isn't normally generated.
--- rod.
 
Old 10-17-2011, 02:13 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Some operations are too architecture specific to represent in C.

Quote:
when we write a normal c code our compiler changes it into assembly code which is again architecture specific.
That covers operations you can represent in C that only become architecture specific as a result of translation to asm. It doesn't cover operations that are inherently architecture specific.

How do you request service from the OS? As a C programmer, you call some library function to request the OS service for you. But how does the C code inside that library function finally request the service? C can only call functions. In most OS's the actual request for service is something architecture specific that is not a function call.

How does that OS routine return control to your process when the requested service is done? That is also architecture specific beyond what can be represented in C.

Many other things a kernel and/or device drivers might need to do have no representation in C.

Typically those are all done in inline assembler within C source code. But that is still human written asm code, not asm code created by translation from C.

Quote:
Originally Posted by theNbomr View Post
Trying to out-optimize a modern C compiler is very difficult.
Yes it is difficult.

Within what can be represented in C, there are still rare cases where the compiler doesn't translate well enough. When you need to squeeze a little more performance out of the most performance critical loops, sometimes a human can do better.

Sometimes a project demands you do something difficult.

Last edited by johnsfine; 10-17-2011 at 02:19 PM.
 
1 members found this post helpful.
Old 10-17-2011, 08:05 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
It is actually true that the earliest "C" compilers generated assembly source-code that was actually assembled by a separate program. And, such "separate programs" do still exist. Some parts of Linux, such as the "trampoline" code that's used during the earliest stages of the boot process, are pure-assembly source code.

But, most of the time, assembly constructs are embedded within "C" programs using e.g. the asm { } directive. Assembly-level statements can be included within a subroutine, or the entire subroutine can be written in assembly-level statements, and the whole thing is processed directly by your friendly nighborhood "C" compiler.

So, when would you actually use this? The short answer is, "almost never." The proper answer is, "when you need to specify an actual sequence of processor-specific machine instructions."

The genius of the original Unix system, and the impetus for writing "C" in the first place (literally as "a language to write Unix in..."), was the recognition that nearly all of the code in a production operating system isn't machine-specific at all, and doesn't need to be written in assembly. (The much-maligned MULTICS system was a fat gob of assembler.)

As time has gone by, microprocessor designers now engineer their instruction-sets, not to be generated by people writing assembler code, but by language compilers. They teach language-compiler designers how to generate highly-optimized instruction sequences for this-or-that microprocessor model, and they often supply compilers of their own which can wring absolutely the most horsepower out of the chip. (They use those compilers to create benchmark programs so that their chips score as high as possible.)
 
1 members found this post helpful.
Old 10-18-2011, 07:32 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sundialsvcs View Post
(The much-maligned MULTICS system was a fat gob of assembler.)
I assume you never looked at any of the MULTICS source code.

In fact it was a fat gob of PL/I with very little asm.

CTSS, was coded in assembler. It was used with a higher level language named BCPL (one of several unrelated early languages with that name). But the OS itself was not coded in BCPL.

ITS was based on CTSS and was coded in a mixture of assembler and B (slightly more assembler than B). B was invented for the purpose of writing ITS.

MULTICS copied a lot from ITS, but added massive design bloat and switched to a much less efficient higher level language.

UNIX derived its name from MULTICS, but since MULTICS was a much longer project started before UNIX but not working until after UNIX, most of the design ideas for UNIX came directly from ITS, not through MULTICS.

C got its name and most of its concepts from B.
 
Old 10-18-2011, 08:36 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Very good questions. The answer is: don't write Assembly code if you can avoid it. Really: you have to have a very specific reason to use Assembly.
 
Old 10-18-2011, 08:56 AM   #9
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
Quote:
Originally Posted by NevemTeve View Post
Very good questions. The answer is: don't write Assembly code if you can avoid it. Really: you have to have a very specific reason to use Assembly.
A trivial example would be the /arch directory of the Linux Kernel, where arch-specific assembler routines are. You'll find code there for the low level parts of memory management and interrupts, among other stuff.
 
Old 10-18-2011, 10:53 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Assembly is pretty much required for performance-critical applications like video encoding and cryptography. Compilers can't take full advantage of advanced features in the processor (SSE2 and such). There's a lot of performance "cheats" available to hand-written assembly that the compiler can't take advantage because you as the programmer knows a bigger picture than the compiler. On a register-starved architecture like x86, merely doing better register allocation so you don't have to touch memory unless you absolutely have to produces ridiculously huge wins (many times over 50%). On a saner architecture like PPC, there's pretty much no special tricks you can pull that the compiler doesn't know about already.

Oh, and compiler intrinsics and the asm directive can get very messy very quickly. They're great when you only need a few special instructions, but not really feasible for anything at all complicated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
assembly language ramorous24 Programming 4 08-21-2011 11:31 PM
Is Assembly Language considered a Structured Language? theKbStockpiler Programming 4 01-30-2011 09:09 AM
Assembly Language Books JMJ_coder Programming 3 05-06-2008 04:04 PM
assembly language in linux herbertgnanaraja Programming 12 09-21-2006 03:52 AM
assembly language: ret>>=10 ashlesha Programming 2 09-13-2006 04:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration