CS 208 s20 — Arithmetic in x86-64 Assembly
Table of Contents
1 Video Lecture
Watch the video lecture for the material outlined below: https://carleton.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=e60cf079-d9b9-41f9-8865-aba700fbea2f.
2 Exercises
0xf000 in %rdx, 0x0100 in %rcx (omitting leading zeros)
0x8(%rdx)→0xf008(%rdx,%rcx)→0xf100(%rdx,%rcx,4)→0xf4000x80(,%rdx,2)→0x1e080- What value does
%raxhold 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:
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 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), %raxwill set%raxequal to5 * %rdx + 7
- destination must be a register
- must have the
qsize designation on a 64-bit system—why?leaspecifically works with a memory addresses, which will always by 8 bytes on a 64-bit system
movq %rdx, %raxvsmovq (%rdx), %raxvsleaq (%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) |
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
5 Homework
- The Week 3 quiz has been posted. It is due 9pm Wednesday, April 29.
- 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 useobjdumpto print out the bytes. See section 3.2.2 of CSPP for an example.
- To do the comparison for 3.11 part C, you can write an assembly file containing both instructions (e.g.,
- 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.