Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section9.3Creating 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. 1  I have appended “.0” to the project folder name for my own use. As I develop my project, subsequent versions will be numbered “.1,” “.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

This is where I used vi to enter the program from Listing 9.1.3, saved the program, and quit vi. You may use your favorite editor here, as described in Section 1.4.

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. 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.

Subsection9.3.1Exercises

Some of the functions you are asked to write in the following exercises are not complete programs. You can check that you have written a valid function by writing a main function in C that calls the function you have written in assembly language. Compile the main function with the -c option so that you get the corresponding object (.o) file. Assemble your assembly language file. Make sure that you specify the debugging options when compiling/assembling. Use the linking phase of gcc to link the .o files together. Run your program under gdb and set a breakpoint in your assembly language function. (Hint: you can specify the source file name in gdb commands.) Now you can verify that your assembly language function is being called. If the function returns a value, you can print that value in the main function using printf.

1

Enter the program in Listing 9.1.3 and use gdb to make sure it works. Next, change the program so that it returns a non-zero integer. Run it with gdb. What number base does gdb use to display the exit code?

Hint Solution
2

Write the C function:

/* f.c */
int f(void) {
return 0;
}

in assembly language. Make sure that it assembles with no errors. Use the -S option to compile f.c and compare gcc's assembly language with yours.

Hint Solution
3

Write the C function:

/* g.c */
int g(void) {
return 123;
}

in assembly language. Make sure that it assembles with no errors. Use the -S option to compile g.c and compare gcc's assembly language with yours.

Solution
4

Write three assembly language functions that do nothing but return an integer. They should each return different, non-zero, integers. Write a C main function to test your assembly language functions. The main function should capture each of the return values and display them using printf.

Solution
5

Write three assembly language functions that do nothing but return a character. They should each return different characters. Write a C main function to test your assembly language functions. The main function should capture each of the return values and display them using printf.

Solution