Assembly basis: linking and memory management 
Author Message
 Assembly basis: linking and memory management

Hi,

I'm not a beginner in assembly, but I've never wondered what was the
goal of linking.
I know it allow the code to be executed by the machine, by organizing
it, but how, and does it depend of the OS, or of the CPU, what are the
diff method...

Please give me information on the linking theories.

Thanks, The Fugitive



Fri, 14 Dec 2001 03:00:00 GMT  
 Assembly basis: linking and memory management

alt.lang.asm:

Quote:
> Hi,

> I'm not a beginner in assembly, but I've never wondered what was the
> goal of linking.
> I know it allow the code to be executed by the machine, by organizing
> it, but how, and does it depend of the OS, or of the CPU, what are the
> diff method...

> Please give me information on the linking theories.

> Thanks, The Fugitive

<Jack>

Most tools for most language implementations allow you to build a
large program in small pieces, including code from other languages and
using pre-built libraries.

In assembly language you have the "extrn" or "extern" operator to tell
the assembler that some code or data object which your program refers
to is not in the source code file it is assembling but will be
provided when the all the pieces of the program are put together in
the finished product.  The assembler can't determine the machine
address of these items at assembly time, not even relative to the
contents of the file it is assembling.

So in the output file it builds in generates a call to the imaginary
external function DoSomethingSilly by generating a call to address 0
(usually), but adding a message to the linker telling it that it is
looking for the final address of DoSomethingSilly when the program is
put together, and kindly put its address in place of the 0 at this
location in my code image.  And so on for other externs.

There is also the "public" operator to use for code and data items in
one source file which can be referred to from other files.  When you
define the function DoSomethingSilly in a file and add a public
directive, the assembler puts a record in the output file saying it
has an object with that name, and where it is relative to the start of
that file.

When the linker puts these two files together it sees that the first
one needs DoSomethingSilly and that the second one supplies an object
with that name.  As it builds the final image it knows exactly where
DoSomethingSilly will be in the final finished program, so if can put
that address in the proper spot in first file's code.

There is usually a lot more involved.  Even when each source file only
refers to objects it defines, the assembler does not know where they
will wind up in the finished program.  That depends on what other
files it is linked with and perhaps they order in which they are
linked.  So even for objects which are not public or external, most
assemblers have to generate relative addresses for data and code.
Again when the linker decides the final location of these objects as
it builds the final program it can convert the relative addresses into
absolute ones.

Also there is linking in pre-built libraries (like printf() in C),
perhaps such system specific things as allocating a stack, linking
operating system calls, and specifying where in the program code
execution should begin.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.



Sat, 15 Dec 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Assembly basis: linking and memory management

2. CFP IWMM'95, Intl Workshop on Memory Management

3. Memory management cause of COM server crash

4. Re Memory and Space Management

5. APL2/PC Memory Management

6. J memory management, again

7. Call for Papers/Participation: 2002 International Symposium on Memory Management (ISMM)

8. Memory Management in VisualWorks?

9. Pictures and memory management

10. Memory Management - Smalltalk/V PM

11. AWK Memory Management

 

 
Powered by phpBB® Forum Software