Assembling and linking steps for assembly language
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?
Do the output o开发者_StackOverflow社区f assembler and output of the linker have the same format? Are they both binary files?
Thanks and regards!
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.
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...
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.
精彩评论