CS520
Spring 2017
Laboratory 9
Friday April 7
The lab must be submitted prior to noon on Sunday April 9.


The goal of this lab is to write a simple Intel 64 assembly language function and learn how to call this from C code.

Before starting this lab, review the lecture on The Intel 64 Architecture, particularly the discussion of slide 8 in the slides for the lecture.

  1. Write an Intel 64 assembly language function, called getFP, to return the caller's frame pointer, i.e. the contents of the %rbp register. This function takes no arguments and needs to be callable from C code. Place this function in a file called asm.s. From the C code point of view this function returns either a long or some kind of pointer type. That is, this function returns a 64-bit value.

  2. Test the function by calling it from the main function of a C program and explore the stack frames of main and the caller of main. What is in the old %rbp slot of the last frame on the stack? Look for some way to be able to identify the last frame on the stack. Keep this C code in a separate file from the function because you will not be submitting this code that you used for testing.

  3. Write a C function, called frameCount, that takes no arguments and uses the assembly language function of step 1 to count and return the number of stack frames that there are on the stack at the time the function is called. This count should not include the function performing the count. The count should include main but not the caller of main. Place this function in a file called lab9.c.

  4. Test your C function by calling it from other C code, with a variety of number of frames on the stack when you call the function. Keep this C code in a separate file from the function because you will not be submitting this code that you used for testing.

The getFP function will be worth 40 points and the frameCount function will be worth 60 points.

Please turn-off any debugging code before you submit your lab.

Submit the code you wrote for steps 1 and 3 via:
~cs520/bin/submit lab9 lab9.c asm.s

Submissions can be checked by typing:
~cs520/bin/scheck lab9

Remember that there are no late submissions of labs. Submit what you have prior to noon on Sunday April 9.


Last modified on March 24, 2017.

Comments and questions should be directed to pjh@cs.unh.edu