Overview

In this reading, you will be introduced to the basics of the x86-64 assembly language, including how to copy data and perform basic arithmetic and logical operations.

Our ultimate goal in studying x86-64 isn’t to be able to write complex programs directly in this language: for the most part this job is handled automatically, and much more efficiently by a compiler. Rather, our goal is to use our knowledge of x86-64 to gain some insight into the processor interface and understand what programs run efficiently on real computer systems.

Required Reading

Sections 3.1 - 3.5.4 (pp. 166-196), excluding sections 3.4.4 and 3.5.1, 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. Define the following: ISA, program counter, register file.
  2. Describe the steps required to go from C code to machine code, equating each step with a specific GCC command.
  3. Map assembly-code suffices to sizes (in bytes) and give examples of C types that use those suffices.
  4. Given a register name, describe how many bytes it represents and its location in a full register (e.g. “the bottom half”).
  5. Given an instruction operand, determine whether it is an immediate, register, or memory value; additionally, give the effective address if it is a memory value.
  6. Given a data movement instruction, determine the source and destination and how much data will be moved.
  7. Given an arithmetic (e.g. add), logic (e.g. or), or shift instruction, write an equation that shows how the register file or memory will be updated.

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 an assignment statement in C, determine whether the corresponding assembly instruction will need to be zero extended, sign extended, or neither.
  2. Given tables of register and memory values, simulate the execution of a set of movement and ALU instructions.
  3. Convert between C code without conditionals, loops, or functions, and the equivalent x86-64 code.

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. Use GCC to compile the following C code to x86-64 code, copying the resulting x86-64 code here.

    #include <stdio.h>
    
    void foo() {
        printf("hello!\n");
    }
    
    int main() {
        foo();
        int x = 9;
        printf("%d\n", x);
        return 0;
    }
  2. What x86-64 instruction suffix (b, w, l, or q) is most likely to be used for copying from one int variable to another int?

  3. Assuming that %eax contains 8 and %ebx contains 10, what is the effective memory address for the following operands.

  4. Describe in your own words what the following instruction does: movsbq %bl, %rdx

  5. Write an equation using C-like syntax that shows the final value of %rax in terms of the other registers.

    incq %rbx
    addq %rbx, %rax
    salq $5, %rax