Answer:
There are many ways to combine programs written in high level and assembler languages:
In most system programming languages (like C or C++), programmer can insert assembler code within a high level language program using spcial compiler directives
Example in C:
int x; main() { x = 5; printf("x = %d\n", x); /* Print x */ /* x = 0 */ asm("sethi %hi(x), %l0"); asm("st %g0, [%l0 + %lo(x)]"); printf("x = %d\n", x); /* Print x again... */ }
Here is a DEMO: click here
The easiest way to combine programs written in high level and assembler languages is to write functions and combine the functions together.
The functions are stored in separate files and compiled separately
The only thing that you need to find out in order to combine functions written in a high level language and in assembler language is HOW the parameters are passed in the high level language.
How parameters are passed will depends on the system architecture and the compiler
It takes a bit of detective work to find this out.... what I did was to write some simple functions with different number of parameters, compile them and look at the assembler listing generated by the compiler to see how the compiler pass the parameters.
Anyway, I will spare you the detective work and just tell you that in the SPARC C compiler, the parameters are passed as follows:
Here is a little demo to show you how that is done:
Here are the commands you need to use to compile and combine different main and sum functions together:
cc sparc-example3a.s sparc-example3b-sum.c /home/cs255000/lib/sparclib.oNote: you need to compile in the /home/cs255000/lib/sparclib.o file because the SPARC assmbler main function calls Write, WriteInt and WriteLn function from my SPARC programming library (that I wrote to simplify SPARC input/output operations)
cc sparc-example3a-main.c sparc-example3b.cHere, you don't need my SPARC programming library because the main function uses C library's "printf" function.
You can mix and match by combining:
The resulting executable is: a.out