0

I am currently creating a simple kernel and I just jumped to using C with the GCC(MinGW) compiler on windows. but the way I am using C with assembly is kind of problematic here is what I am doing :

:: First I am assembling the assembly files down to a flat binary   
nasm -f bin Assembly/asm.asm -o asm.bin


:: doing the same thing for the C files
gcc -c C/c.c     
objcopy -O binary -j .text c.o c.bin



:: combing the all binaries by adding them together In the desired order
COPY /B asm.bin + c.bin + asm1.asm ..... os.bin

it is kind of working, but it is also tedious working that way, first I have to break the code into segments (because I am adding each binary file on top of the other), I have to calculate the addresses manually by looking at the raw binary files, and I can't call functions from other files .

what should I do ? What is the best way to combine C and Nasm assembly on windows for OSdev ? I am guessing I should use a some sort of a linker or something.

Note that I want also to be able to call assembly functions inside c and use C functions inside assembly .

6
  • 1
    Yes, link first (don't use flat binary for the asm) and then do the objcopy. Commented Jul 7, 2023 at 13:13
  • Maybe a bit off-topic, but since you are using gcc, then why not use gas for assembly? Commented Jul 7, 2023 at 13:53
  • @EugeneSh. I have developed a strong familiarity with NASM syntax and directives since I first began working with assembly language. So, NASM has become my go-to choice and the assembler with which I am most comfortable with. Commented Jul 7, 2023 at 14:24
  • @Jester when I assemble down to an object file certain directives won't work for example, the org directive doesn't work, the [bits 32] directive also produces an error Commented Jul 7, 2023 at 14:45
  • Right, you don't use org. If you care you can set base address using linker. The bits should not produce an error, but you should already be targeting 32 bits if you want to link with C code so you don't need it. The 16 bit boot sector should be assembled separately (you don't want to link C code there). Commented Jul 7, 2023 at 18:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.