5
  1. I was wondering what is the purpose of linking step after assembling step? Why not run the output of assembler without the linking step?

    For C, linking is to combine several object files generated by compilation into a single object file. But for assembly language, there is just one object file to "link", so why bother to link a single object file? For example, http://zahidirfan.blogspot.com/2010/01/two-steps-to-using-assembly-in-linux.html

    If there is only one object file and no library is needed, will linking be unnecessary? Just like in the example I gave above?

  2. Do the output of assembler and output of the linker have the same format? Are they both binary files?

Thanks and regards!

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
Tim
  • 1
  • 141
  • 372
  • 590
  • See also this answer but substitute 'compile' with 'assemble': http://stackoverflow.com/questions/311882/what-do-statically-linked-and-dynamically-linked-mean/311889#311889 – paxdiablo Jun 09 '11 at 01:55
  • For C, linking is to combine several object files generated by compilation into a single object file. But for assembly language, there is just one object file to "link", so why bother to link a single object file? For example, http://zahidirfan.blogspot.com/2010/01/two-steps-to-using-assembly-in-linux.html – Tim Jun 09 '11 at 02:03
  • @Tim: because, unless you want to duplicate a bucketload of code in every one of your source files, it's good to use libraries :-) Keep in mind it's not mandated, just common practice. EG: a86 assembler was so blindingly fast, it could assemble source code faster than most linkers of the time could link object files, so it had no need for a linker. – paxdiablo Jun 09 '11 at 02:07
  • Thanks! If there is only one object file and no library is needed, will linking be unnecessary? Just like in the example I gave above? – Tim Jun 09 '11 at 02:09
  • @Tim, that depends entirely on the assembler itself. The vast majority will create object files simply because they provide so much more flexibility. Object files are (almost always) not runnable as executables without a link step to prepare them. – paxdiablo Jun 09 '11 at 02:14
  • Thanks! How does link step prepare them? In other words, what differences are between object file generated by assembler and executable files generated by linker, in terms of format? Why usually the former is not executable? – Tim Jun 09 '11 at 02:25
  • @Tim, that's probably best left for another question. The comment parts aren't really suitable for long discussions and the question you're asking is a good candidate for a proper SO question. – paxdiablo Jun 09 '11 at 02:29
  • I asked these in part 2 of in my original post. I think there are no fundamental differences between the two parts of my post. But I will wait some time to decide if I need a new post. – Tim Jun 09 '11 at 02:32

3 Answers3

5

An assembler produces object files as output, just like a compiler does.

You link them for pretty much the same reason as well -- to be able to use libraries. The linker is also what (normally) knows about target executable formats.

That said, there are assemblers that produce executables directly, without a linker being involved. If memory serves, NASM can produce a few executable formats directly, and some older assemblers for MS-DOS (e.g., A86) can/do work this way as well.

The simpler setup and faster assembly cycle with these makes them really handy for beginners, but the requirement to put all the code into a single module makes the much less suited to larger projects.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks! For C, linking is to combine several object files generated by compilation into a single object file. But for assembly language, there is just one object file to "link", so why bother to link a single object file? For example, http://zahidirfan.blogspot.com/2010/01/two-steps-to-using-assembly-in-linux.html – Tim Jun 09 '11 at 02:06
  • If there is only one object file and no library is needed, will linking be unnecessary? Just like in the example I gave above? – Tim Jun 09 '11 at 02:10
  • @Tim: For anything but really trivial projects, assembly uses more than one object file (given its low level, you use more files as a rule). As I pointed out above, a linker isn't not strictly necessary -- but most assemblers still only "know" how to produce object files as output, not executables, so for them it's still needed. There's no reason they *couldn't* produce executables directly, but a lot of reasons they don't want to (mostly boiling down to a couple: complexity and portability). – Jerry Coffin Jun 09 '11 at 02:13
  • Thanks! What differences are between object file generated by assembler and executable files generated by linker, in terms of format? Why usually the former is not executable? – Tim Jun 09 '11 at 02:24
  • @Tim: the differences vary widely -- in some cases (e.g. OMF object vs. MZ executable) they're nothing alike. In others (COFF object vs. COFF executable) they're virtually identical, but with a few fields like the base load address filled in. – Jerry Coffin Jun 09 '11 at 03:06
1

Your "1)" question description is faulty. Assembly language programs (other than the most trivial sample apps) will normally have multiple obj files that need to be linked together.

In the very simple case of all the code being in a single file, as others have mentioned, many assemblers do permit assembling straight to binary. However, this is special behaviour to satisfy an exception to the rule...

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
  • Thanks! About part 1, does the example http://zahidirfan.blogspot.com/2010/01/two-steps-to-using-assembly-in-linux.html only have one obj file? – Tim Jun 09 '11 at 14:00
  • @Tim The example does only have one object file, but is being assembled to a linkable object rather than straight to binary. The linker will do required fixups to generate the binary. – Brian Knoblauch Sep 14 '17 at 13:41
0

1- Linking is necessary because the binary file will at least need some platform-specific code to be put inside it. 2- Concluding from (1), before linking, the binary file is incomplete to be ran. It's a binary file, although not ready to be executed standalone.

Gabriel
  • 1,803
  • 1
  • 13
  • 18