How to make a Makefile for a program for assembly Language?

22,689

First of all, read an introduction to Makefile.

Your Makefile is read by the make program on invocation. It executes either the given rule (e.g. make clean) or executes the default one (usually all).

Each rule has a name, optional dependencies, and shell code to execute.

If you use objects file you usally start your Makefile like that:

all: main.o
    gcc -o PROGRAM_NAME main.o

The dependency here is main.o, so it must be resolved before the execution of the code. Make searches first for a file named main.o and use it. If it doesn't exist, it search for a rule permitting to make the file.

You have the following:

main.o: main.asm
    nasm -f elf -g -F stabs main.asm

Here the rule to make main.o depends on main.asm, which is your source file so I think it already exists. The code under the rule is then executed and has to make the file matching the rule name. If your nasm invocation is correct, then the file is created and the code of the all (or Program as you named it) is executed.

So your Makefile should work, you just have to test it to be sure :) It is barely the same as invoking:

nasm -f elf -g -F stabs main.asm && gcc -o Program main.o

As a side note, you can still test this on windows as long as you have all the tools installed. The cygwin project provides an environment where you can install make, gcc and nasm.

Share:
22,689
Ale Rojas
Author by

Ale Rojas

Updated on September 07, 2020

Comments

  • Ale Rojas
    Ale Rojas over 3 years

    I've come across this task to build a Makefile for a program in assembly language I made (nothing fancy, like a hello world). The program is in Linux 32 bits and I'm using NASM assembler. So far I can only find Makefiles for programs for C, I'm aware that there's not much difference from one to another but I'm not familiar with this thing. What I have is this:

    Program: main.o
        gcc -o Program main.o
    main.o: main.asm
        nasm -f elf -g -F stabs main.asm
    

    I can't tell whether this is correct or, if it does, how it works. I can't try the code because this computer doesn't have Linux. I really would like to know what's going on in the code.