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

Section9.4Programming Exercises

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