CS 208 w20 lecture 8 outline

1 Why Study Assembly?

  • Understand optimizations made by the compiler and how your high-level code might affect them
  • High-level languages can hide details we need to know
    • Ex. investigate exactly where data is stored—can be crucial for concurrent programs
  • Write more secure software
    • Many of the ways programs can be attacked involve exploiting the way programs store their run-time control information

2 Registers

registers.png

3 Addressing Modes

Table 1: The scaling factor \(s\) must be 1, 2, 4, or 8. For indexed mode, \(Imm\) may be omitted. For scaled indexed mode, \(\mathtt{r}_b\) and/or \(Imm\) may be omitted.
Type Form Operand value Name
immediate $Imm Imm immediate
register \(\mathtt{r}_a\) \(\mathsf{R}[\mathtt{r}_a]\) register
memory Imm \(\mathsf{M}[Imm]\) absolute
memory \((\mathtt{r}_a)\) \(\mathsf{M}[\mathsf{R}[\mathtt{r}_a]]\) indirect
memory \(Imm(\mathtt{r}_b)\) \(\mathsf{M}[Imm + \mathsf{R}[\mathtt{r}_b]]\) base + displacement
memory \(Imm(\mathtt{r}_b, \mathtt{r}_i)\) \(\mathsf{M}[Imm + \mathsf{R}[\mathtt{r}_b] + \mathsf{R}[\mathtt{r}_i]]\) indexed
memory \(Imm(\mathtt{r}_b, \mathtt{r}_i, s)\) \(\mathsf{M}[Imm + \mathsf{R}[\mathtt{r}_b] + \mathsf{R}[\mathtt{r}_i]\cdot s]\) scaled indexed

Why only 1, 2, 4, and 8 for scaling factor?

3.1 Exercises

0xf000 in %rdx, 0x0100 in %rcx (omitting leading zeros)

  • 0x8(%rdx)0xf008
  • (%rdx,%rcx)0xf100
  • (%rdx,%rcx,4)0xf400
  • 0x80(,%rdx,2)0x1e080
  • What value does %rax hold after these instructions?
:
    mov $0x0070000077070000, %rdx
    mov %edx, %eax
    add %rax, %rax

4 Thinking in Assembly

4.1 Assembly to C

A C function with the signature long f(long *p, long i) compiled to the following assembly code:

f:
    movq    %rsi, %rax
    addq    (%rdi), %rax
    movq    %rax, (%rdi)
    ret
Register Use
%rdi 1st argument (p)
%rsi 2nd argument (i)

Write the C code for this function.

long f(long *p, long i) {
    *p += i;
    return *p
}

How would the assembly change if the return statement were removed?

4.2 lea Instruction

  • "load effective address", but more often "lovely efficient arithmetic"
  • instead of reading from the memory location given by the source operand, copies the effective address to the destination
    • generate pointers for later memory references
    • can also do a muliply and an addition in a single instruction
      • leaq 7(%rdx, %rdx, 4), %rax will set %rax equal to 5 * %rdx + 7
  • destination must be a register

4.3 C to Assembly

Translate this C code to assembly

long arith(long x, long y, long z)
{
    long t1 = x + y;
    long t2 = z + t1;
    long t3 = x + 4;
    long t4 = y * 48;
    long t5 = t3 + t4;
    long rval = t2 * t5;
    return rval;
}
Register Use
%rdi 1st argument (x)
%rsi 2nd argument (y)
%rdx 3rd argument (z)
arith:
    leaq    (%rdi,%rsi), %rax
    addq    %rdx, %rax
    leaq    (%rsi,%rsi,2), %rcx
    salq    $4, %rcx
    leaq    4(%rdi,%rcx), %rcx
    imulq   %rcx, %rax
    ret

Examples on godbolt.org: https://godbolt.org/z/j_WZwW