LinuxQuestions.org
Help answer threads with 0 replies.
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 08-02-2014, 08:24 PM   #1
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Rep: Reputation: 19
Question what is difference between compiler and assembler?(


Hi Friends,
while reading book on operating system I came across terms Assembler and Compiler, I read about them and I ended up in bigger confusion every time I read about these two, many posts and explanation on internet state that Assembler/Compiler translates source code into object-core which can be then executed, if what i've understood here is correct then there are some questions I have for which I was not able to find any good explanation.

Questions/Doubts

*) If what I've understood is correct then why two different programs?(only difference I can see is assembler can convert only assembly language where as compiler can do more of such languages)

*) As per book I have there is hierarchy of how program is executed I will try to write it down in short.

[High Level Language Program] -> (compiler) -> [Assembly language program] -> (Assembler) -> [Object Module] + [Object Library Module] -> (Linker) -> [Executable Machine language Program] -> (Loader) -> [Memory]
( I have created similar digram and attached to this post)
well problem with hierarchy is if both assembler and compiler are doing about the same job then why is compiler pointing to Assembler, according to me any one of two should be there in picture.

why did author keep compiler on the top of hierarchy? why not assembler?

does it means that compiler converts source code into assembly code and passes it to assembler then assembler converts it further to machine code (binary)?

Although this post may not relate specifically to Linux but is related to operating system, I posted this questions here expecting there would be some one around who might have better under understanding about operating system and it's components in general.

Thank you
Attached Thumbnails
Click image for larger version

Name:	HLL_program_translation_hierarchy.jpeg
Views:	32
Size:	35.2 KB
ID:	16018  
 
Old 08-02-2014, 08:41 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Assembly is a type of programming language where there's a one-to-one relation between the commands and the underlying machine code instructions. Assembly is simply a human-readable representation of the actual CPU codes, and converting an assembly language program into executable code is a relatively simple matter of converting each assembly mnemonic into the corresponding binary code.

(Yes, this is somewhat simplified, as most assemblers can expand macros and convert labels into addresses, but the main point still stands.)

A compiler, on the other hand, converts program code written in a high-level language into something the CPU can execute. Unlike assembly, a high-level language can contain abstractions like variables and functions, which really don't exist at the assembly/machine code level.

When a compiler translates a program written in a language like C into machine code, it may convert it into an assembly program and let an assembler handle the final conversion into an executable file.

Last edited by Ser Olmy; 08-03-2014 at 05:55 AM.
 
Old 08-03-2014, 09:16 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-03-2014, 10:07 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I think this is the point when you should actually write and compile a program.
Code:
$ cat >hello.c <<DONE
#include <stdio.h>
int main (void) {
    puts ("Hello");
    return 0;
}
DONE
$ gcc -o hello.s -S hello.c # generate Assembly source (hello.s)
$ gcc -o hello.o -c hello.c # generate object module (hello.o)
$ gcc -o hello      hello.c # generate executable
 
Old 08-03-2014, 10:23 AM   #5
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,982

Rep: Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626Reputation: 3626
One of the most common assemblers is the Microsoft Assembly (macro assembly) MASM. On their site they quote this.
"The Microsoft Macro Assembler 8.0 (MASM) is a tool that consumes x86 assembly language programs and generates corresponding binaries. Assembly language programs built with MASM can be edited and debugged using Visual C++ 2005 Express Edition. This installation requires the Visual C++ 2005 Express Edition to be installed on the computer."
http://www.microsoft.com/en-us/downl...190a24fa6=True

This differs from a compiler where a different level of code is used as base.

If I were to have been in charge of Linux from the start, I'd have made it all in assembly.
 
Old 08-03-2014, 12:11 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> If I were to have been in charge of Linux from the start, I'd have made it all in assembly.

And so it would work on one platform instead of hundreds.
 
Old 08-03-2014, 12:17 PM   #7
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try compiling using the gcc '-save-temps' and you'll see that it generates a .s file which contains assembly code. If you wanted to you could use this generated assembly as is and it will work. You can also include global functions defined there in your C programs by making a function prototype defined as extern and adding the .s file to the gcc command line when compiling.

Yes, gcc passes the code to the 'as' assembler. It is usually in the same package as 'ld' which is the linker. Both of these are needed to create programs.
 
Old 08-03-2014, 12:17 PM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Member Response

Hi,

Not really, you could cross compile for a specific architecture. I would program for a Z80 on a x86 platform. Done all the time.
Hope this helps.
Have fun & enjoy!
 
Old 08-22-2014, 02:15 AM   #9
pix9
Member
 
Registered: Jan 2010
Location: Mumbai, India
Distribution: ArchLinux, Fedora 24, Centos 7.0
Posts: 177

Original Poster
Rep: Reputation: 19
Thanks for your suggestions and explanations.

Regards
 
  


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
Which Assembler. jokar.mohsen Programming 1 01-07-2013 02:40 AM
C compiler, assembler, architecture tautvilis Linux - Newbie 2 04-21-2010 12:22 PM
Assembler samjkd General 7 03-14-2005 01:52 PM
assembler usr Programming 2 11-15-2003 05:15 PM
assembler tda Programming 4 08-21-2002 02:54 AM

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

All times are GMT -5. The time now is 08:31 PM.

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