Chapter 10 Linking and Loading. Separate assembly creates “.mob” files.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programs in Memory Bryce Boe 2012/08/29 CS32, Summer 2012 B.
Chapter 11 Implementing an Assembler and a Linker Using C++ and Java.
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
The Assembly Language Level
Chapter 3 Loaders and Linkers
Linkage Editors Difference between a linkage editor and a linking loader: Linking loader performs all linking and relocation operations, including automatic.
3. Loaders & Linkers1 Chapter III: Loaders and Linkers Chapter goal: r To realize how a source program be loaded into memory m Loading m Relocation m Linking.
Chapter 3 Loaders and Linkers
Chapter 3 Loaders and Linkers. Purpose and Function Places object program in memory Linking – Combines 2 or more obj programs Relocation – Allows loading.
Machine Independent Assembler Features
CS 31003: Compilers ANIRUDDHA GUPTA 11CS10004 G2 CLASS DATE : 24/07/2013.
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
An introduction to systems programming
Guide To UNIX Using Linux Third Edition
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Separate Assembly allows a program to be built from modules rather than a single source file.
MIPS coding. SPIM Some links can be found such as:
Improving Program Performance Function Visibility in z/TPF C++ Load Modules October 2, /2/20151American Express Public.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© 2004, D. J. Foreman 1 Memory Management. © 2004, D. J. Foreman 2 Building a Module -1  Compiler ■ generates references for function addresses may be.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Programming With C.
Chapter 6: User-Defined Functions
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Chapter 13 : Symbol Management in Linking
Introduction to OOP CPS235: Introduction.
Loader and Linker.
LINKERS Execution of a program written in a language L involves the following steps: 1.Translation of the program: Performed by the translator for language.
Assembler Design Options One-Pass and Multi-Pass Assemblers.
CSc 453 Linking and Loading
Linking Loader untuk SIC/XE Machine. Lebih Lanjut mengenai Absolute Loader Shortcoming of an absolute loader –Programmer needs to specify the actual address.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Hello world !!! ASCII representation of hello.c.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 10 – Loaders.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 3 Translation.
Visit for more Learning Resources
Computer Architecture & Operations I
Computer Science 210 Computer Organization
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
System Programming and administration
The University of Adelaide, School of Computer Science
Memory Management © 2004, D. J. Foreman.
Separate Assembly allows a program to be built from modules rather than a single source file assembler linker source file.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Assembler Design Options
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Chap. 6 Programming the Basic Computer
Loaders and Linkers.
Computer Organization and Design Assembly & Compilation
Loaders and Linkers.
Chapter 6 Programming the basic computer
An introduction to systems programming
Chapter 3 Loaders and Linkers
SPL – PS1 Introduction to C++.
Presentation transcript:

Chapter 10 Linking and Loading

Separate assembly creates “.mob” files

Linking separately-assembled modules creates a “.mac” file

What if load point = 0? Or 500?

Correct addresses depend on the load point

Use the /p argument to specify the load point

Program works when load point = 0

Addresses are in bold

The st instruction correctly stores 9 into location 3 (z).

Now let’s see if the same program works when the load point is 500. Will it have the correct addresses?

It works!

The st instruction correctly stores 9 into location 503 (the location of z when the load point is 500).

Somehow addresses (and ONLY addresses) were changed to reflect the load point 500. How did sim (the OS) know which fields to adjust?

A “.mac” file consists of two parts: the header and the text. The header contain R entries which indicate the location of the “relocatable” fields. The text is the machine code. A T entry separates the header from the text.

R entries in “.mac” field indicate fields that have to be adjusted.

How the OS determines the location of the relocatable fields in the loaded program

OS adds the load point to the relocatable fields of the program as it sits in memory.

The pic program provides a conceptual picture of a “.mac” file

mex program shows hex and ASCII

Don’t jump over data. Use end directive instead.

The end directive results in a small- s entry in the header which indicates the entry point relative to the beginning of the module.

Absolute addresses should not be relocated. An absolute address in the end directive creates a big-S entry in the header.

Type Address Symbol S 0003 T This is the header for the program in the preceding slide. A big-S entry means that the entry point is absolute—the entry point does not depend on the load point. The lack of R entries means no relocation of addresses will occur.

Programs consisting of multiple modules are generally better than programs consisting of one big module. With the multiple module approach, the modules need some way to communicate with each other if they are assembled separately.

The two modules in this example are not assembled separately so communication is easy.

Separate assembly results in assembly-time errors

For separate assembly, we need the public and extern directives. public makes a symbol global. extern indicates that a symbol is defined in some other module.

Now separate assembly works

The assembler does not know the addresses of the external symbols sub and total when it is assembling m1, so it uses 0 for their addresses. call sub st total is translated to E

Assembly listings

When the linker combines m1 and m2, It must update addresses. For example, references to total must contain the new address of total, computed as follows:

The linker must update the address in the add instruction:

To assemble and link: mas m1 mas m2 lin m1 m2 lin creates m1.mac

To see “.mob” and “.mac” files use the pic or mex programs. pic m1.mob mex m1.mob pic m2.mob mex m2.mob pic m1.mac mex m1.mac

pic output for m1.mob and m2.mob (‘^’ flags an external symbol)

mex output for m1.mob

There is an E entry in a header for every external reference

Step 1 in linking process: read in modules to be linked. Addresses in header entries are adjusted. The modified header entries are saved in internal tables (P table, E table, R table, and S table). When an R entry is saved, the start address of the associated module is included in the saved R entry. The text portions of the modules are loaded one after the other into a text buffer.

Step 2 in the linking process: resolve external references. For each E entry, Determine the address in the combined program of the external field (this address is in the saved E entry). By searching the P entries, determine the location in the combined program of the symbol referenced. Add the address of the symbol referenced to the external field.

Step 3 in the linking process: relocate relocatable fields. For each R entry, Get the address of the relocatable field from the R entry. Add the module start address (also obtained from the R entry) to the relocatable field in the combined text.

Example of R entry processing R Add 0004 to the relocatable field in the combined text at location 5 from the start of the text.

Last step in the linking process: output the “.mac” file. The saved headers, with all E entries turned into R entries, are outputed. The combined text in the text buffer (which now has all its addresses adjusted appropriately) is outputed.

pic output for m1.mac

mex output for m1.mac

lin creates a file (with extension “.tab”) that shows the link process. See the “.tab” file on the next slide.

A library is a collection of object modules collected into a single file. Let’s create a library consisting of several simple math modules.

A technique for multiplying n by 15: shift n left 4 times (which multiplies by 16) and then subtract n.

To square a whole number n, add the first n odd numbers. For example, 3 2 = = 9

Now create a function poly that computes 15 x n 2 that calls the square and mult15 modules.

Create a main module that calls poly, passing it 2.

main module that calls poly

To create an executable program, first assemble. Use file names— not module names

Next, link all the modules

It is easy to make an error when linking—you have to remember the names of all the modules that are needed.

Creating a library fmult15.lib mas fmult15 mas fsquare mas fpoly lib fmult15 fsquare fpoly Base name of library defaults to base name of first file specified.

Using the library fmult15.lib lin fmain /Lfmult15 Now you do not have to remember all the modules that fmain requires.

Library format

Linking with a library involves five steps. See the next slide.

Libraries can be displayed with pic and mex. The next slide shows the mex output for the fmult15.lib library.

The linker always links entire object modules from a library even if only one function within a module is needed. See the next slide.

f1, f2, and f3 linked even if only f3 needed.

Can specify multiple libraries

For a link with a library, lin’s “.tab” file shows the loading sequence of modules from the library. See the next slide.

Advantages of separate assembly Can control scope of identifiers. Saves time to update an executable file— you need to reassemble only those modules that are changed, and then link all the modules. Can avoid problems because of assembler limitations, such as symbol table size. Most important: Can use libraries.

Start-up code Code that is part of an executable module obtained from a C++ program. Start-up code gets control first from the OS. It handles start-up initialization and then calls main.

Suppose you invoke the test program as follows:

Arguments passed to main

Linking with start-up code sup.mob Start-up code has an end directive so the order of linking modules is not critical.

How to set up a program to use start-up code

Suppose the test program includes start-up code and it is invoked with The next three slides shows how the argc and argv parameters are built for this invocation.

Here is a program that accesses the command line arguments. Suppose its executable file is kangaroo.mac.

Why main should return a valid error code.

What is the effect of the static keyword on a global variable declaration and on a function definition? Answer: restricts scope to the file.

How does static restrict scope? Answer: by suppressing the generation of a public directive.

It is good that in C++ variables that are not locally defined are not assumed to be external variables—that is, they are not assumed to be global variables defined in another module. See line 10 in the next slide.

C ++ requires a prototype or function definition preceding a call. Thus, the C++ compiler detects the error on line 11. The C compiler does not. Both compilers detect the error on line 10.