Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 296
Ale Rojas Avatar asked Sep 06 '13 08:09

Ale Rojas


People also ask

How do I create a makefile file?

Creating a Makefile A Makefile typically starts with some variable definitions which are then followed by a set of target entries for building specific targets (typically .o & executable files in C and C++, and . class files in Java) or executing a set of command associated with a target label.

Where can I create a makefile?

This makefile should be located in the src directory. Note that it also includes a rule for cleaning up your source and object directories if you type make clean. The . PHONY rule keeps make from doing something with a file named clean.


1 Answers

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.

like image 193
Geoffroy Avatar answered Oct 04 '22 20:10

Geoffroy