CS 208 s20 — Arithmetic in x86-64 Assembly

Table of Contents

1 Video Lecture

2 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

3 Arithmetic Instructions

Instruction Description Effect
inc \(D\) \(D \leftarrow D + 1\) increment
dec \(D\) \(D \leftarrow D - 1\) decrement
neg \(D\) \(D \leftarrow -D\) negate
not \(D\) \(D \leftarrow ~D\) complement
add \(S,\:D\) \(D \leftarrow D + S\) add
sub \(S,\:D\) \(D \leftarrow D - S\) subtract
imul \(S,\:D\) \(D \leftarrow D * S\) multiply
xor \(S,\:D\) \(D \leftarrow D\,\widehat{}\,S\) exclusive-or
or \(S,\:D\) \(D \leftarrow D\,\vert\,S\) or
and \(S,\:D\) \(D \leftarrow D\,\&\,S\) and
sal \(k,\:D\) \(D \leftarrow D\) << \(k\) left shift
shl \(k,\:D\) \(D \leftarrow D\) << \(k\) left shift (same as sal)
sar \(k,\:D\) \(D \leftarrow D\) >> \(k\) arithmetic right shift
shr \(k,\:D\) \(D \leftarrow D\) >> \(k\) logical right shift

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:

    movq    %rsi, %rax
    addq    (%rdi), %rax
    movq    %rax, (%rdi)
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 leaq 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
  • must have the q size designation on a 64-bit system—why?
    • lea specifically works with a memory addresses, which will always by 8 bytes on a 64-bit system
  • movq %rdx, %rax vs movq (%rdx), %rax vs leaq (%rdx), %rax
    • rdx holds 0x100, memory address 0x100 holds 0xab

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)
    leaq    (%rdi,%rsi), %rax
    addq    %rdx, %rax
    leaq    (%rsi,%rsi,2), %rcx
    salq    $4, %rcx
    leaq    4(%rdi,%rcx), %rcx
    imulq   %rcx, %rax

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

5 Homework

  1. The Week 3 quiz has been posted. It is due 9pm Wednesday, April 29.
  2. Do CSPP practice problems 3.6 (p. 192), 3.7 (p. 193), 3.10 (p. 196), and 3.11 (p. 197)
    • To do the comparison for 3.11 part C, you can write an assembly file containing both instructions (e.g., xor_test.s), compile it to an object file (xor_test.o) and use objdump to print out the bytes. See section 3.2.2 of CSPP for an example.
  3. Give yourself time to space out your work on lab 1—take a break and come back. It will be hard to do it all in one go.