You are here: Home > Knowledge Refreshers

KR editions 66 to 68


KR-66: Linking

We'll take a look at linking in this edition.

Note: This discussion is not exclusively for any particular high-level language.

  • Let's say we have 2 codes: CPP1 and CPP2. Let's say that CPP1 uses a function called print( ) but doesn't contain its definition (i.e. in CPP1 we don't specify what print( ) actually means). For the code to compile we only need to declare print( ) in CPP1 (declaration means telling the compiler "I'm going to use a function called print in this code; when you come across it don't flag an error"). The compiler assumes that we would have defined print( ) in some other code. 

  • In CPP2 we define the function print( ) - in a definition we tell the compiler that "when you encounter the function call to print, then execute the following statements". 

  • Each code will be compiled (separately) and so we will have 2 object codes CPP1.obj and CPP2.obj. 

  • But is CPP1.obj complete? No. 

  • CPP1 still doesn't know what code to execute for print( ). We need to somehow tell the system that the definition of print( ) is in CPP2.obj. And this is one function of the linker. 

  • We give CPP1.obj and CPP2.obj as input to the linker. The linker will resolve all unresolved function calls (i.e. function calls which didn't have a function definition in the object code). 

  • The output of the linker will be a single module (it'll combine both object codes into one module)- called the LOAD MODULE or the EXECUTABLE. 

TERMS: Source code- what we write. Object code (module)- created after compilation (by the compiler). Load module- created after linking (by the linker). 

 


KR-67: Low-level language

Before getting into what happens after linking; we'll take a pause and look at a couple of other things: about low-level languages and about memory. 

  • Every statement in a high level language (like COBOL) translates into multiple low-level language (machine) instructions.

  • For example we might say MULTIPLY 2 and 3 and store the answer in RESULT; this is one high-level statement but the CPU won't understand that - at the hardware level the CPU won't know what is multiplication; thus this statement will be converted (by the compiler) into a sequence of simpler statements which the CPU can understand - perhaps a series of additions. 

  • Another example: we might say DISPLAY HELLO. This statement will display the word HELLO on screen but for this to happen the statement has to be converted into statements that the processor understands. And usually 1 high-level language statement = multiple CPU statements (or multiple machine instructions). It forms a 1 is to many relation. 

  • Whereas if we did assembly level programming (where we use mnemonics- each mnemonic will correspond to a single machine instruction) there is a 1 to 1 mapping between the assembly instruction and machine language. Thus one assembly instruction is converted into one machine instruction by the assembler. Low-level languages are very close to the CPU whereas high-level languages are far away from the processor. 

  • Machine-level programming is also low-level programming but really hard (imagine writing a program using 1s and 0s!). In this case we don't need any intermediate translator like a compiler or an assembler (since we'll be talking to the CPU in a language it understands!). 


KR-68: Memory

Memories can be classified broadly under two categories: 

  1. Primary

  2. Secondary

Secondary memory (or external memory) - also called auxiliary memory. Common secondary storage devices are: hard disks; CDs, tapes, floppy disks etc. These can store data irrespective of whether power supply is available or not. This is where our compiled and linked programs get stored. 

Primary memory (or main memory): This is the memory directly accessible by the CPU (i.e. the address lines of the CPU are connected to primary memory). In our PCs this is usually the RAM (and ROM). For the processor (CPU) to perform any task, it needs to fetch instructions from the main memory. 

RAM (Random access memory) is a temporary memory; every time we restart the PC, the values stored in RAM are lost. ROM (read-only memory) contains programs that are always needed by the CPU (which remain the same always)- like the bootstrap loader (which helps load the OS into main memory). 

To sum up: For anything to execute, the instructions need to be present in main memory. The problem: When we compile and link a code, the program lies in secondary storage (it lies on our hard-disk; C drive or something).

Food for thought: How do the programs we create in the hard disk reach the main memory? 


Go back to the main contents page