CS 208 s21 — Learning Block #9
Table of Contents
1 Review
2 C to Assembly
Translate this C code to assembly3
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) |
3 Practice
- 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.,
Footnotes:
1
mov copies the source to the destination, possibly computing a memory address and reading or writing a value there, while lea computes a memory address and stores that address in a register (instead of going to memory)
2
0x8(%rdx)accesses0xf008(%rdx,%rcx)accesses0xf100(%rdx,%rcx,4)accesses0xf4000x80(,%rdx,2)accesses0x1e080
3
One possible assembly implementation of arith:
arith: leaq (%rdi,%rsi), %rax // performs x + y using leaq addq %rdx, %rax // %rax now holds x + y + z (t2) leaq (%rsi,%rsi,2), %rcx // performs y * 3 using leaq salq $4, %rcx // %rcx now holds y * 48, since left shift 4 multiplies by 2^4 (16), and y * 3 * 16 == y * 48 leaq 4(%rdi,%rcx), %rcx // %rcx now holds y * 48 + x + 4 (t5 = t3 + t4) imulq %rcx, %rax // %rax now holds t2 * t5 ret // return using %rax as the return value