The following topics will be briefly introduced in this post:
- conversion of assembly (using nasm syntax), C and object code into each other;
- understand the linker;
- include assembly code into C code;
Install MinGW, objconv, nasm
Step 1.1: Install MinGW
Step 1.2: Add the MinGW’s bin folder (C:\MinGW\bin) to PATH
I just searched for “PATH” in the main search of Windows 10, followed the suggestion till Environment Variables and added “C:\MinGW\bin\” to the system variable.
Test it by opening the Windows terminal and entering gcc to see if the command is known or not.
Step 2.1: Download / Extract objconv
Step 2.2: Add the objconv.exe path to PATH (see Step 1.2)
Step 3.1: Download / Extract / Install nasm
Step 3.2: Add the nasm.exe path to PATH (see Step 1.2)
The following picture summarize different code conversion you can perform. I recommend to read the NOTES section to be more aware of such functionalities.
If you genetated the .asm as described above, you can remove the “function” in line 8 and all the “align=… noexecute”. Test if there are still warnings of any kind by entering
nasm -f elf32 program.asm
For more explanations, see FURTHER READING.
- Object files have .obj extension on Windows but .o on Linux
- If your ASM or C code depends on external libraries, you can create the object files for each of them and link them together in an executable by listing all object files one after another, e.g.:
Shell1gcc part1.obj part2.obj -o final.exe
- You can also omit the option -o of gcc which let you specify the name of the executable. In this case, the output file will be automatically called a.exe
- You very easily include assembly functions in your C program using the keyword extern
assuming you have “
function_name:” in your .asm file which has been declared global like this12SECTION .TEXTGLOBAL function_name
Then you just need to link the corresponding object files together.
- By removing the -O2 option of the gcc command, the final .asm file will not call the C coded function. Instead, it will contain the corresponding assembly instructions.