cs24-20fa Introduction to Assembly

Introduction to Computing Systems (Fall 2020)

hello

Machine Model Abstraction

Types of Data

Immediates

Immediates are constants. There are several notations for them, but they consistently start with a dollar sign:

Registers

Registers are variables of a fixed-width. For example:

eax

Unlike variables, there are only so many registers, and we cannot “declare” more. They must be used judiciously.

Memory

mov and retq


ten.c

int ten() {
    return 10;
}

\(\leftrightarrow\)

clang -S ten.c

ten:
    mov  $10, %eax
    retq


mov Instruction

x86-64 [data to register]

mov $imm, %dst
mov %src, %dst
mov (%src), %dst

x86-64 [register to memory]

mov %src, (%dst)
mov $imm, (%dst)

x86-64 [memory displacement]

mov D(%src), %dst
mov %src, D(%dst)

\(\leftrightarrow\)

C Pseudocode

dst = imm
dst = src
dst = *src

C Pseudocode

*dst = src
*dst = imm

C Pseudocode

dst = *(src + D)
*(dst + D) = src

There is no instruction to mov directly from memory to memory!

D is an immediate without the $ prefix.


ret Instruction

x86-64

retq

\(\leftrightarrow\)

C Pseudocode

return %eax

Notice that %eax always contains the return value.

We cannot return any other register!

Identity Function (Arguments)


identity.c

int identity(int x) {
    return x;
}

\(\leftrightarrow\)

clang -S identity.c

identity:
    mov  %edi, %eax
    retq



More on Registers

A Note about Suffixes

Submit QuickChecks For Credit