Name:
Date:
Lab 4 – Finding Function Arguments on the Stack
Introduction
- Functions
are perhaps the most fundamental language feature for abstraction and code
reuse. The stack is so important because it keeps track of the functions running
in a program. When a function is called, a stack frame is created to
support the functionÕs execution. The stack frame contains the functionÕs
local variables and the arguments passed to the function by its caller.
The frame also contains housekeeping information that allows the called
function (the callee) to return to the caller
safely. The exact contents and layout of the stack vary by processor
architecture and function call convention. In this lab we look at Intel
x86 stacks.
- We will use
the GNU debugger (GDB) to step through an X86 program. The class website has links to a quick gdb summary and to a printable gdb reference card; you can also find other
information on Google.
Due
- Hand in a
single copy this completed lab sheet at the end of the session
designated for this work, or at a time mutually agreed to by you and the
instructor.
Lab Steps
Part 1 – Function Arguments
- Log into your Unix account on felix or helix (not on tanner, since we will need a Linux
machine for this lab), then change your current directory to csc8400/x86.
- Copy the executable x86stack1
from the /mnt/a/mdamian/courses/x86 directory into your
csc8400/x86 directory. Invoke gdb with x86stack1 as an argument:
gdb ./x86stack1
- Set the disassembly language
to Intel:
set disassembly-flavor intel
4. Disassemble the main function, then answer the questions listed below.
5. Write down the names of the two functions invoked by the main function: ________________________________________________
6. Focus on the 2nd invoked function. Write down the instructions that place the function arguments onto the stack (just before the function is invoked with call):
__________________________________________________
__________________________________________________
7. Write down the number of arguments for the 2nd invoked function: _________________
8. Write down the values of the arguments passed to the 2nd invoked function: _________________
There are two ways to answer this question (do it both ways please, the result should be the same).
Method 1:
a) Set a breakpoint at the second function call (identified in part a)
break *(main+???)
b) Check the values that just got placed onto the stack
x /d $esp
x /d $esp+4
Method 2:
a) Disassemble the 2nd function invoked by main. Set a breakpoint at the first instruction following the common prolog
push ebp
mov ebp, esp
b) Check the values stored at addresses $ebp+8, $ebp+12, etc
x /d $ebp+8
x /d $ebp+12
9. Write down the value returned by the 2nd invoked function: _________________ (To do so, check the contents of eax before the function returns.)
10. Write down the value returned by the main function: _________________
Part 2 – Repeat the Activities of Part 1 on a Different
Piece of Code
- Copy the executable x86stack2
from the /mnt/a/mdamian/courses/x86 directory into your
csc8400/x86 directory. Invoke gdb with x86stack2 as an argument.
- Set the
disassembly language to Intel. Disassemble the main function, then answer the
questions listed below.
- Write down
the names of the two functions invoked by the main function:
________________________________________________
- Focus on the
2nd invoked function. Write down the instructions that place the
function arguments onto the stack (just before the function is invoked
with call):
_______________________________________________
__________________________________________________
- Write down
the number of arguments for the 2nd invoked function: _________________
- Write down
the values of the arguments passed to the 2nd invoked function:
_________________
- Write down
the value returned by the 2nd invoked function:
_________________
- Write down
the value returned by the main
function: _________________