Prerequisites
:
Knowledge of C language, concepts of pointers,
Structure
of Turbo C
Compiler used
: TC
Assembler used :
NASM
Introduction
I'm talking specifically about the Turbo C++ compiler, but most
other compilers follw the same suit.
We all are familiar with the main function in C. It defines
the starting point of execution of a C program.
However, the compiler
creates a few other housekeeping functions in the background,
which are
not visible to the C programmer.
The actual begining of the program starts in a function called _start,
its an assembly language routine,
which contains the first lines of execution.
Various low level things like the stack pointer is setup here.
Then it goes to the actual _main function for the program logic.
Compilation
The individual C files are compiled seperately, and the object code
for each,
with their unresolved symbols are compiled into individual
object codes.
Conceptually, the following C program:
#include"main.h"
main(){
int a;
printf("Hello World");
}
will be compiled to the following object code:
- reserve relocatable space for the variable a, insert it into the
symbol table
- reserve relocatable space for the constant string "Hello World"
- push the char * for "Hello World" to stack, and call an external
function printf(unknown to the compiler, at this point)
Linking
Take all the object supplied to the linker and combine them into one
file
search for unresolved symbols(printf),in the object codes and the
libraries (LIBC etc)
LINKING
ASSEMBLY AND C
The process of linking assembly and C, and accessing each others
variables at compile-time and run-time
requires understanding of how the object code store the variable
information. In object code the variables
have not yet been assigned relocatable address.The object code produced
by NASM and TCC are of the same format.
The TLINK program is used to combine together all the object code
(irrespective of the program which created it) along
with the libraries to produce the final executable image in .exe
format.
ACCESSING
C VARIABLES IN ASSEMBLY
To access a variable from C in assembly, prefix an underscore ( _ ) to
the variable or function name
and declare it extern in C. To access a variable from assembly in C,
have an underscore ( _ ) prefixed
to the assembly language variable and declare it global.
To call a function in C written in assembly, it is called as an
ordinary function call.
To call a function in assembly written in C, it is called with a call
instruction.
REFERENCE
[1]Peter Ebel, “IBM PC Assembly language programming on”, Prentice
Hall, India edition 5
[2]Andrew S Tannebum, “Operating Systems”,TechMedia Prentice Hall,
India 2002,
[3]INTEL, INTEL 80386 PROGRAMMER'S REFERENCE MANUAL, 1986
[4]RALPH BROWN, RALPH BROWNS INTERRUPT LIST, 1986
[5]http://www.nondot.org/sabre/os/files/
[6]http://cvs.sourceforge.net
[7]http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single
/Filesystems-HOWTO.html
[8]http://www.osdev.org/
[9]http://www.osdev.org/developers/guide01/index.jsp
[10]http://foldoc.doc.ic.ac.uk/
[11]http://www.acm.uiuc.edu/sigops/
Back
to main Page
In case you found this page useful, and want more of it, drop a mail to
iamxsj@yahoo.co.in