Assembly and C code: conversion and integration in Windows 10

Spread the word!

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)

Conversion Paths

The following picture summarize different code conversion you can perform. I recommend to read the NOTES section to be more aware of such functionalities.

nasm

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

For more explanations, see FURTHER READING.

NOTES

  • 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.:

  • 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 this

    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.

FURTHER READING

https://stackoverflow.com/questions/20737947/how-to-generate-a-nasm-compilable-assembly-code-from-c-source-code-on-linux

https://stackoverflow.com/questions/17676026/converting-c-to-nasm-assembly

https://www.devdungeon.com/content/how-mix-c-and-assembly

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*