Overview

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.

Required Reading

Sections 3.4.4 and 3.7 (pp. 189-191, 238-254) from the course textbook.

Learning Objectives

BASIC Learning Objectives

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.

  1. Convert a given pushq or popq instruction to an equivalent set of instructions that doesn’t use pushq or popq.
  2. Given a sequence of pushq and popq instructions and the program stack, visually represent how both the stack and %rsp changes with each instruction.
  3. Describe the role of each part of the stack frame (e.g. arguments and saved registers), indicating which are required and which are optional.
  4. Describe what happens to the program counter, stack pointer, and stack after executing a call (or ret) instruction.
  5. Given a C function header, list the location where each of the parameters will be stored during a call to that function.
  6. Write x86-64 code to allocate space for local variables/arrays on the stack, assign values to those variables, and then deallocate that space.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Given two C functions, one that calls the other, convert these functions into equivalent x86-64 code that strictly follows the x86-64 calling convention.
  2. Identify bugs caused by failure to strictly follow the x86-64 calling convention.
  3. Trace and write x86-64 assembly code that involves function calls.

Pre-class Exercises

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.

  1. Convert the following instructions into equivalent instructions that use only ADD, SUB, and MOV instructions.

  2. 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
  3. In your own words, describe the role of each of the following stack areas:

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

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