• Aucun résultat trouvé

Assemble the Program with NASM

Dans le document Assembly Language Step-by-Step (Page 179-182)

Taking a Trip Down Assembly Lane

Step 2: Assemble the Program with NASM

The NASM assembler does not have a user interface as nontechnical people understand ‘‘user interface’’ today. It doesn’t put up a window, and there’s no place for you to enter filenames or select options in check boxes. NASM works via text only, and you communicate with it through a terminal and a Linux console session. It’s like those old DOS days when everything had to be entered on the command line. (How soon we forget!)

Open a terminal window. Many different terminal utilities are available for Ubuntu Linux. The one I use most of the time is called Konsole, but they will all work here. Terminal windows generally open with your home directory as the working directory. Once you have the command prompt, navigate to the

‘‘eatsyscall’’ project directory using thecdcommand:

myname@mymachine:~$ cd asmwork/eatsyscall

If you’re new to Linux, make sure you’re in the right directory by checking the directory contents with thels command. The file eatsyscall.asm should

144 Chapter 5 The Right to Assemble

at least be there, either extracted from the listings archive for this book, or entered by you in a text editor.

Assuming that the file eatsyscall.asm is present, assemble it by (carefully) entering the following command and pressing Enter:

nasm -f elf -g -F stabs eatsyscall.asm

When NASM finds nothing wrong, it will say nothing, and you will simply get the command prompt back. That means the assembly worked! If you entered the eatsyscall.asm file yourself and typed something incorrectly, you may get an error. Make sure the file matches Listing 5-1.

Now, what did all that stuff that you typed into the terminal mean?

I’ve dissected the command line you just entered in Figure 5-11. A NASM invocation begins with the name of the program itself. Everything after that are parameters that govern the assembly process. The ones shown here are nearly all of the ones you’re likely to need while first learning the assembly language development process. There are others with more arcane purposes, and all of them are summarized in the NASM documentation. Let’s go through the ones used here, in order:

NASM -f elf -g -F stabs eatsyscall.asm

The name of the source code file to be assembled.

Specifies that debug information is to be generated in the

“stabs” format.

Specifies that debug information is to be included in the .o file.

Specifies that the .o file will be generated in the “elf” format.

Invokes the assembler

Figure 5-11: The anatomy of a NASM command line

Chapter 5 The Right to Assemble 145 -f elf: There are a fair number of useful object file formats, and each one is generated differently. The NASM assembler is capable of generating most of them, including other formats, such as bin, aout, coff, and ELF64, that you probably won’t need, at least for awhile. The-fcommand tells NASM which format to use for the object code file it’s about to generate. In 32-bit IA-32 Linux work, the format is ELF32, which can be specified on the command line as simply elf.

-g:While you’re still working on a program, you want to have debugging information embedded in the object code file so that you can use a debugger to spot problems. (More on how this is done shortly.) The-gcommand tells NASM to include debugging information in the output file.

-F stabs:As with the output file, there are different formats in which NASM can generate debug information. Again, as with the output file format, if you’re working in IA-32 Linux, you’ll probably be using the STABS format for debug information, at least while you’re starting out. There is a more powerful debug format called DWARF that can also be used with ELF (get it?), and NASM will generate DWARF data instead of STABS data if you replace ‘‘stabs’’ with

‘‘dwarf’’ in this command. Remember too that Linux commands are case sensitive. The-f command and the-F command are distinct, so watch that Shift key!

eatsyscall.asm: The last item on the NASM command line is always the name of file to be assembled. Again, as with everything in Linux, the filename is case sensitive. EATSYSCALL.ASM and EatSysCall.asm (as well as all other case variations) are considered entirely different files.

Unless you give NASM other orders, it will generate an object code file and name it using the name of the source code file and the file extension .O. The ‘‘other orders’’ are given through the -ooption. If you include a-o command in a NASM command line, it must be followed by a filename, which is the name you wish NASM to give to the generated object code file. For example:

nasm -f elf -g -F stabs eatsyscall.asm -o eatdemo.o

Here, NASM will assemble the source file eatsyscall.asm to the object code file eatdemo.o.

Now, before moving on to the link step, verify that the object code file has been created by using thelscommand to list your working directory contents.

The file eatsyscall.o should be there.

146 Chapter 5 The Right to Assemble

Dans le document Assembly Language Step-by-Step (Page 179-182)