How to compile assembly language in c
I am looking at lots of assembly language code that is compiled a开发者_StackOverflow中文版long with c. They are using simple #define assembly without any headers in boot.s code. How does this work ?
Typically .s
files are processed by an assembler. Without knowing any other details, there's nothing more to say. .s
file goes in, .o
file comes out.
Many assemblers provide some kind of include
directive to allow use of headers, which would also be in assembly language.
Ah, the code you linked is for use by the GNU as
assembler. If you're on Linux or Mac, do man as
to learn about it. If you're on Windows, install MinGW or Cygwin.
Compilers can frequently include in-line assembly, but I believe it is compiler specific.
I don't remember the precise details, but I think its something like:
void myFunc(void)
{
int myNum; /* plain old C */
__asm /* Assembly */
{
mov ax,bx;
xor cx,cx;
}
myNum = 5; /* more C */
}
Research your specific compiler for details.
The link you post in your comment is an assembly language source file that is meant to be first run through a c-preprocessor. It's just a programming convenience, but lots of assembly language compilers support similar constructs anyway, so I'm not sure why they went the c-preprocessor route.
If you have "main proc" inside of your code, you are using x86 architecture and your file ends with .asm you con use for compilation: tasm fileName.asm
In result you will get your fileName.obj file. After that you need to link it and for that you can use tlink filename.obj
To run, just enter the filename.exe on the command line
If you need to link more than one file use tlink filename1.obj filename2.obj and so on
during the compilation and linking is not necessary to specify the file extension like .obj or .asm. Using just filename should be fine.
精彩评论