In this reading, you will explore how function calls are handled in x86-64. You’ll also learn about the program stack and the role it plays during program execution.
You’ll find function calls to be like an intricate dance with two partners performing specific steps at specific times. You’ll need to be careful about missing steps, or performing them at the wrong time as they can lead to some subtle and not-so-subtle problems. Like dancing, you’ll need to both see and perform the steps multiple times before you get comfortable.
Sections 3.4.4 and 3.7 (pp. 189-191, 238-254) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
pushq
or popq
instruction to an equivalent set of instructions that doesn’t use pushq
or popq
.pushq
and popq
instructions and the program stack, visually represent how both the stack and %rsp
changes with each instruction.call
(or ret
) instruction.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
Convert the following instructions into equivalent instructions that use only ADD
, SUB
, and MOV
instructions.
pushq %rcx
popq %rcx
Assume that you have the following values stored in registers.
Register | Value |
%rax | 0x75 |
%rbx | 0x850 |
%rcx | 0x42 |
%rsp | 0xFF40 |
Draw the stack, after the following set of instructions are executed. Your drawing should include the starting address for each value on the stack and the value stored there (similar to Figure 3.9 in your textbook).
pushq %rax
pushq %rbx
popq %rax
pushq %rax
pushq %rcx
In your own words, describe the role of each of the following stack areas:
Consider the following C function header.
void bar(int a, int b, char c, short int d, char *e, int f, int g, int h)
Once inside of the bar
function, list where each of the parameters may be located. If any of the parameters are on the stack, give their position relative to the stack pointer (e.g. %rsp + 75).
a
:b
:c
:d
:e
:f
:g
:h
:Consider the following C code.
int arr[3];
arr[0] = 82;
arr[1] = 7;
arr[2] = 31;
Write a sequence of x86-64 instructions to allocate space on the stack for the array and assign the given values to the elements of the array.