Section 9.3 Creating a Program in Assembly Language¶
This seems like a good point to start writing in assembly language, so we will go through the steps of creating a program for the assembly language program in Listing 9.1.3.
The notation I use here assumes that I am doing this for a class named CS 252, and my instructor has specified that each project should be submitted in a directory named “
CS252lastNameNN,” where “
lastName” is the student's surname and “NN” is the project number. I have appended “
.0” to the project folder name for my own use. As I develop my project, subsequent versions will be numbered “
.2,” etc. I will explain the steps as I go.
pi@rpi3:~ $ mkdir CS252plantz01.0 pi@rpi3:~ $ cd CS252plantz01.0/ pi@rpi3:~/CS252plantz01.0 $ ls
I create a directory for my program. All the files that you create for each program should be kept in a separate directory only for that program. Changing into the new directory and listing the contents shows that it contains no files.
pi@rpi3:~/CS252plantz01.0 $ vi doNothingProg2.s
pi@rpi3:~/CS252plantz01.0 $ ls doNothingProg2.s
This shows that I have created an assembly language source code file.
pi@rpi3:~/CS252plantz01.0 $ as --gstabs -o doNothingProg2.o doNothingProg2.s pi@rpi3:~/CS252plantz01.0 $ ls doNothingProg2.o doNothingProg2.s
I run the assembler. The
--gstabs option tells the assembler to include debugging information with the output file. We will very definitely make use of the debugger! The
-o option is followed by the name of the output (object) file. You should always use the same name as the source file, but with the
.o extension. And I verify that the object file has been created in my directory.
pi@rpi3:~/CS252plantz01.0 $ gcc -o doNothingProg2 doNothingProg2.o pi@rpi3:~/CS252plantz01.0 $ ls doNothingProg2 doNothingProg2.o doNothingProg2.s
Next I link the object file. Even though there is only one object file, this step is required in order to bring in the C runtime libraries needed to create an executable program. Instead of using the
ld program, as described in Section 9.1, it is much easier to use
gcc recognizes that
doNothingProg2.o is an object file and automatically links in the required C runtime libraries. If you use
ld, you need to explicitly specify each of the libraries. As with the assembler, the
-o option is used to specify the name of the output file, which will be the final product of our efforts, the executable program.
pi@rpi3:~/CS252plantz01.0 $ ./doNothingProg2
Finally, I execute our new program, which as the name implies, does nothing.