make program to
control compilation of ref.c, etc., as well as some other
useful tasks.Makefile has tab characters in it - if
you copy-and-paste its contents from the web page, you run the risk of
turning the tabs into spaces. It's better to use control-click to
download the files directly, if you plan to run them on your own system.| function |
usage |
| // pass
int by value, C or C++ void valswap(int m, int n) { int t = m; m = n; n = t; } |
int a, b; a = 1; b = 2; valswap(a,b); now, a is 1, b is 2 |
| // pass
int by reference, C++ only void refswap(int &m, int &n) { int t = m; m = n; n = t; } |
int a, b; a = 1; b = 2; refswap(a,b); now, a is 2, b is 1 |
| // pass
pointer by value, C or C++ void ptrswap(int *m, int *n) { int t = *m; *m = *n; *n = t; } |
int a, b; a = 1; b = 2; ptrswap(&a,&b); now, a is 2, b is 1 |
| // pass
pointer by value, C or C++ // but implement it incorrectly void badswap(int *m, int *n) { int t = m; m = n; n = t; } |
int a, b; a = 1; b = 2; badswap(a,b); (see below) |
| function | compiled, imc, no optimization |
compiled, imc, optimization |
| valswap |
pushq
%rbp movq %rsp, %rbp movl %edi, -20(%rbp) movl %esi, -24(%rbp) movl -20(%rbp), %eax movl %eax, -4(%rbp) movl -24(%rbp), %eax movl %eax, -20(%rbp) movl -4(%rbp), %eax movl %eax, -24(%rbp) leave ret |
pushq %rbp movq %rsp, %rbp leave ret (see Exercise 1) |
| refswap |
pushq
%rbp movq %rsp, %rbp movq %rdi, -24(%rbp) movq %rsi, -32(%rbp) movq -24(%rbp), %rax movl (%rax), %eax movl %eax, -4(%rbp) movq -32(%rbp), %rax movl (%rax), %edx movq -24(%rbp), %rax movl %edx, (%rax) movq -32(%rbp), %rdx movl -4(%rbp), %eax movl %eax, (%rdx) leave ret |
pushq %rbp movq %rsp, %rbp movl (%rdi), %edx movl (%rsi), %eax movl %eax, (%rdi) movl %edx, (%rsi) leave ret |
| ptrswap |
same as above |
same as above |
| badswap |
see below |
see below |
| function |
compiled, lnx, no optimization |
compiled, lnx, optimization |
| valswap |
pushl
%ebp movl %esp, %ebp subl $4, %esp movl 8(%ebp), %eax movl %eax, -4(%ebp) movl 12(%ebp), %eax movl %eax, 8(%ebp) movl -4(%ebp), %eax movl %eax, 12(%ebp) leave ret |
pushl %ebp movl %esp, %ebp leave ret (see Exercise 1) |
| refswap |
pushl
%ebp movl %esp, %ebp subl $4, %esp movl 8(%ebp), %eax movl (%eax), %eax movl %eax, -4(%ebp) movl 8(%ebp), %edx movl 12(%ebp), %eax movl (%eax), %eax movl %eax, (%edx) movl 12(%ebp), %edx movl -4(%ebp), %eax movl %eax, (%edx) leave ret |
pushl %ebp movl %esp, %ebp pushl %ebx movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx) popl %ebx leave ret |
| ptrswap |
same as above |
same as above |
| badswap |
see below |
see below |