Skip to content

Commit d1bf39b

Browse files
author
Margo Seltzer
committed
Add exercise code.
1 parent 8b4dc26 commit d1bf39b

14 files changed

+163
-0
lines changed

asm1x/arg1.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func1(long arg1) {
4+
return arg1;
5+
}

asm1x/arg2.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func2(long arg1, long arg2) {
4+
return arg1 + arg2;
5+
}

asm1x/arg3.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func3(long arg1, long arg2, long arg3) {
4+
return arg1 + arg2 + arg3;
5+
}

asm1x/arg4.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func4(long arg1, long arg2, long arg3, long arg4) {
4+
return arg1 + arg2 + arg3 + arg4;
5+
}

asm1x/arg5.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func5(long arg1, long arg2, long arg3, long arg4, long arg5) {
4+
return arg1 + arg2 + arg3 + arg4 + arg5;
5+
}

asm1x/arg6.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func6(long arg1, long arg2, long arg3, long arg4, long arg5, long arg6) {
4+
return arg1 + arg2 + arg3 + arg4 + arg5 + arg6;
5+
}

asm1x/arg7.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Let's figure out the calling conventions on this compiler!
2+
3+
long func7(long arg1, long arg2, long arg3, long arg4, long arg5, long arg6, long arg7) {
4+
return arg1 + arg2 + arg3 + arg4 + arg5 + arg6 + arg7;
5+
}

asm1x/main.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
3+
// Because printf is actually called a bit differently from how we write
4+
// it in C, we're going to separate calls to printf from other calls for
5+
// the moment.
6+
void print_nargs(int n) {
7+
printf("We ran this with %d arguments\n", n);
8+
}
9+
10+
int main (int argc, char *argv[]) {
11+
(void)argv;
12+
13+
// Let's figure out how many arguments we passed to main.
14+
// Remember that argc counts the numbe of arguments in argv,
15+
// and that includes the command name itself.
16+
int nargs = argc - 1;
17+
print_nargs(nargs);
18+
}
19+

asm1x/mul.c

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
long times(long a) {
2+
return 16 * a;
3+
}

asm1x/mystery1.S

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.file "mystery1.c"
2+
.text
3+
.globl mystery1
4+
.type mystery1, @function
5+
mystery1:
6+
.LFB0:
7+
imull $300, %edi, %eax
8+
imull $500, %esi, %esi
9+
addl %esi, %eax
10+
addl %edx, %eax
11+
ret
12+
.LFE0:
13+
.size mystery1, .-mystery1
14+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
15+
.section .note.GNU-stack,"",@progbits

asm1x/mystery2.S

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.file "mystery2.c"
2+
.text
3+
.globl mystery2
4+
.type mystery2, @function
5+
mystery2:
6+
.LFB0:
7+
movq %rdi, %rax
8+
salq $4, %rax
9+
addq %rsi, %rax
10+
salq $4, %rax
11+
addq %rdx, %rax
12+
salq $4, %rax
13+
addq %rcx, %rax
14+
ret
15+
.LFE0:
16+
.size mystery2, .-mystery2
17+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
18+
.section .note.GNU-stack,"",@progbits

asm1x/mystery3.S

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.file "mystery3.c"
2+
.text
3+
.globl op1
4+
.type op1, @function
5+
op1:
6+
.LFB23:
7+
movl %edi, %eax
8+
andl %esi, %eax
9+
notl %eax
10+
ret
11+
.LFE23:
12+
.size op1, .-op1
13+
.globl op2
14+
.type op2, @function
15+
op2:
16+
.LFB24:
17+
notl %edi
18+
movl %edi, %eax
19+
orl %esi, %eax
20+
ret
21+
.LFE24:
22+
.size op2, .-op2
23+
.section .rodata.str1.1,"aMS",@progbits,1
24+
.LC0:
25+
.string "It worked!"
26+
.text
27+
.globl mystery3
28+
.type mystery3, @function
29+
mystery3:
30+
.LFB25:
31+
movl %edi, %eax
32+
andl %esi, %eax
33+
notl %eax
34+
notl %edi
35+
orl %edi, %esi
36+
cmpl %esi, %eax
37+
jne .L7
38+
subq $8, %rsp
39+
movl $.LC0, %edi
40+
call puts
41+
addq $8, %rsp
42+
.L7:
43+
rep ret
44+
.LFE25:
45+
.size mystery3, .-mystery3
46+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
47+
.section .note.GNU-stack,"",@progbits

asm1x/mystery4.S

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.file "mystery4.c"
2+
.text
3+
.globl mystery4
4+
.type mystery4, @function
5+
mystery4:
6+
.LFB0:
7+
pushq %rbx
8+
movl %edi, %ebx
9+
movl $0, %eax
10+
testl %edi, %edi
11+
je .L2
12+
leal -1(%rdi), %edi
13+
call mystery4
14+
addl %ebx, %eax
15+
.L2:
16+
popq %rbx
17+
ret
18+
.LFE0:
19+
.size mystery4, .-mystery4
20+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
21+
.section .note.GNU-stack,"",@progbits

asm1x/sub.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//! -O1
2+
long
3+
sub(long a, long b) {
4+
return a - b;
5+
}

0 commit comments

Comments
 (0)