Skip to content

Commit b68fc53

Browse files
committed
Add assembly section 1 material.
1 parent 6f19f70 commit b68fc53

20 files changed

+902
-0
lines changed

asms1/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fun[0-9][0-9]
2+
fun0[1-6].s

asms1/GNUmakefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
PROGRAMS = $(sort $(patsubst fun%.cc,fun%,$(wildcard fun[0-9][0-9].cc)) $(patsubst fun%.s,fun%,$(wildcard fun[0-9][0-9].s)))
2+
ALLPROGRAMS = $(PROGRAMS)
3+
FULLSOURCES = $(patsubst fun%.cc,fun%.s,$(sort $(wildcard fun[0-9][0-9].cc)))
4+
5+
all: $(PROGRAMS) $(FULLSOURCES)
6+
7+
O = s
8+
include ../common/rules.mk
9+
LDFLAGS := $(if $(ISLINUX),-no-pie,)
10+
11+
%.o: %.cc $(BUILDSTAMP)
12+
$(CXX) $(CPPFLAGS) $(filter-out -g,$(CXXFLAGS)) $(O) $(DEPCFLAGS) -o $@ -c $<
13+
14+
%.s: %.cc $(BUILDSTAMP)
15+
$(CXX) $(CPPFLAGS) $(filter-out -g,$(CXXFLAGS)) $(O) $(DEPCFLAGS) -o $@ -S $<
16+
$(call cleanasm,$@)
17+
18+
fundriver.o: fundriver.cc $(BUILDSTAMP)
19+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(DEPCFLAGS) -o $@ -c $<
20+
21+
%.o: %.s $(BUILDSTAMP)
22+
$(call run,$(CXX) -o $@ -c,ASSEMBLE,$<)
23+
24+
fun%: fun%.o fundriver.o
25+
$(CXX) $(CXXFLAGS) $(O) -o $@ $^ $(LDFLAGS)
26+
27+
28+
clean:
29+
rm -rf $(DEPSDIR)
30+
rm -f $(ALLPROGRAMS) *.o $(FULLSOURCES)
31+
32+
.PHONY: all clean

asms1/README.md

+389
Large diffs are not rendered by default.

asms1/fun.gdb

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
set arch i386:x86-64
2+
3+
init-if-undefined $fun_continue = 0
4+
init-if-undefined $fun_file = ""
5+
init-if-undefined $fun_file_set = 0
6+
init-if-undefined $fun_argc = 0
7+
init-if-undefined $fun_arg0 = ""
8+
init-if-undefined $fun_arg1 = ""
9+
init-if-undefined $fun_arg2 = ""
10+
init-if-undefined $fun_arg3 = ""
11+
12+
define fun-file
13+
if $argc != 1
14+
expected_exactly_one_argument
15+
else
16+
file $arg0
17+
set $fun_file = $arg0
18+
set $fun_file_set = 1
19+
p $fun_file
20+
end
21+
end
22+
23+
document fun-file
24+
Set the fun-file to run.
25+
end
26+
27+
alias -a ff = fun-file
28+
29+
define run-fun
30+
if $fun_file_set == 0
31+
please_run_fun_file_first
32+
else
33+
if $argc > 0
34+
set $fun_argc = $argc
35+
set $fun_arg0 = $arg0
36+
end
37+
if $argc > 1
38+
set $fun_arg1 = $arg1
39+
end
40+
if $argc > 2
41+
set $fun_arg2 = $arg2
42+
end
43+
if $argc > 3
44+
set $fun_arg3 = $arg3
45+
end
46+
if $argc > 4
47+
too_many_arguments
48+
end
49+
if $_thread != 0
50+
kill
51+
end
52+
shell killall qemu-x86_64 >/dev/null 2>&1
53+
if $fun_argc == 0
54+
eval "shell qemu-x86_64 -g 12948 \"%s\" & sleep 0.2", $fun_file
55+
end
56+
if $fun_argc == 1
57+
eval "shell qemu-x86_64 -g 12948 \"%s\" \"%s\" & sleep 0.2", $fun_file, $fun_arg0
58+
end
59+
if $fun_argc == 2
60+
eval "shell qemu-x86_64 -g 12948 \"%s\" \"%s\" \"%s\" & sleep 0.2", $fun_file, $fun_arg0, $fun_arg1
61+
end
62+
if $fun_argc == 3
63+
eval "shell qemu-x86_64 -g 12948 \"%s\" \"%s\" \"%s\" \"%s\" & sleep 0.2", $fun_file, $fun_arg0, $fun_arg1, $fun_arg2
64+
end
65+
if $fun_argc == 4
66+
eval "shell qemu-x86_64 -g 12948 \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" & sleep 0.2", $fun_file, $fun_arg0, $fun_arg1, $fun_arg2, $fun_arg3
67+
end
68+
target remote localhost:12948
69+
if $fun_continue != 0
70+
continue
71+
end
72+
end
73+
end
74+
75+
document run-fun
76+
Restart the current fun-file.
77+
end
78+
79+
alias -a rf = run-fun

asms1/fun01.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstring>
2+
3+
int fun(const char* s) {
4+
if (strchr(s, '!') != nullptr) {
5+
return 0; /* fun */
6+
} else {
7+
return -1; /* no fun */
8+
}
9+
}

asms1/fun02.cc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <cstring>
2+
#include <cstdlib>
3+
4+
int fun(const char* s) {
5+
return strtol(s, nullptr, 0) + 1;
6+
}

asms1/fun03.cc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int fun(const char* s) {
2+
if (s[0] != 0) {
3+
return s[1];
4+
} else {
5+
return -1;
6+
}
7+
}

asms1/fun04.cc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int fun(const char* s) {
2+
if (s[0] != 0
3+
&& s[0] == s[1]
4+
&& s[1] != 0) {
5+
return s[2];
6+
} else {
7+
return -1;
8+
}
9+
}

asms1/fun05.cc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int fun(const char* s) {
2+
if (s[0] == 0) {
3+
return -1;
4+
}
5+
while (true) {
6+
if (s[0] != s[1]) {
7+
return s[1];
8+
}
9+
++s;
10+
}
11+
}

asms1/fun06.cc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <cstring>
2+
3+
int fun(const char* s) {
4+
unsigned len;
5+
for (len = 0; s[len]; ++len) {
6+
}
7+
return len % 4;
8+
}

asms1/fun07.s

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.file "fun07.cc"
2+
.text
3+
.globl _Z3funPKc
4+
.type _Z3funPKc, @function
5+
_Z3funPKc:
6+
endbr64
7+
pushq %rcx
8+
xorl %edx, %edx
9+
xorl %esi, %esi
10+
call strtol@PLT
11+
movl $2, %edx
12+
movl %eax, %esi
13+
.L2:
14+
cmpl %esi, %edx
15+
jge .L5
16+
movl %edx, %ecx
17+
imull %edx, %ecx
18+
.L6:
19+
cmpl %esi, %ecx
20+
jge .L10
21+
addl %edx, %ecx
22+
jmp .L6
23+
.L10:
24+
je .L7
25+
incl %edx
26+
jmp .L2
27+
.L5:
28+
decl %eax
29+
setle %al
30+
movzbl %al, %eax
31+
jmp .L1
32+
.L7:
33+
movl $1, %eax
34+
.L1:
35+
popq %rdx
36+
ret
37+
.size _Z3funPKc, .-_Z3funPKc
38+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
39+
.section .note.GNU-stack,"",@progbits

asms1/fun08.s

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.file "fun08.cc"
2+
.text
3+
.section .rodata.str1.1,"aMS",@progbits,1
4+
.LC0:
5+
.string "%d %d %d"
6+
.text
7+
.globl _Z3funPKc
8+
.type _Z3funPKc, @function
9+
_Z3funPKc:
10+
endbr64
11+
subq $40, %rsp
12+
leaq .LC0(%rip), %rsi
13+
movq %fs:40, %rax
14+
movq %rax, 24(%rsp)
15+
xorl %eax, %eax
16+
leaq 20(%rsp), %r8
17+
leaq 16(%rsp), %rcx
18+
leaq 12(%rsp), %rdx
19+
call __isoc99_sscanf@PLT
20+
movl %eax, %r8d
21+
orl $-1, %eax
22+
cmpl $3, %r8d
23+
jne .L1
24+
movl 16(%rsp), %eax
25+
addl 12(%rsp), %eax
26+
subl 20(%rsp), %eax
27+
.L1:
28+
movq 24(%rsp), %rdx
29+
subq %fs:40, %rdx
30+
je .L3
31+
call __stack_chk_fail@PLT
32+
.L3:
33+
addq $40, %rsp
34+
ret
35+
.size _Z3funPKc, .-_Z3funPKc
36+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
37+
.section .note.GNU-stack,"",@progbits

asms1/fun09.s

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.file "fun09.cc"
2+
.text
3+
.section .rodata.str1.1,"aMS",@progbits,1
4+
.LC0:
5+
.string "r"
6+
.text
7+
.globl _Z3funPKc
8+
.type _Z3funPKc, @function
9+
_Z3funPKc:
10+
endbr64
11+
pushq %rcx
12+
leaq .LC0(%rip), %rsi
13+
call fopen@PLT
14+
movq %rax, %rdi
15+
orl $-1, %eax
16+
testq %rdi, %rdi
17+
je .L1
18+
call fclose@PLT
19+
xorl %eax, %eax
20+
.L1:
21+
popq %rdx
22+
ret
23+
.size _Z3funPKc, .-_Z3funPKc
24+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
25+
.section .note.GNU-stack,"",@progbits

asms1/fun10.s

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.file "fun10.cc"
2+
.text
3+
.globl _Z3funPKc
4+
.type _Z3funPKc, @function
5+
_Z3funPKc:
6+
endbr64
7+
pushq %rcx
8+
xorl %edx, %edx
9+
xorl %esi, %esi
10+
call strtol@PLT
11+
movb $1, %dl
12+
movl %eax, %ecx
13+
shrl $8, %ecx
14+
je .L2
15+
leal -1(%rax), %edx
16+
testl %eax, %edx
17+
setne %dl
18+
.L2:
19+
movzbl %dl, %eax
20+
popq %rdx
21+
ret
22+
.size _Z3funPKc, .-_Z3funPKc
23+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
24+
.section .note.GNU-stack,"",@progbits

asms1/fun11.s

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.file "fun11.cc"
2+
.text
3+
.globl _Z3funPKc
4+
.type _Z3funPKc, @function
5+
_Z3funPKc:
6+
endbr64
7+
xorl %eax, %eax
8+
.L2:
9+
movb (%rdi,%rax), %dl
10+
movl %eax, %ecx
11+
incq %rax
12+
leal -65(%rdx), %esi
13+
cmpb $25, %sil
14+
jbe .L2
15+
cmpl $5, %ecx
16+
setle %al
17+
testb %dl, %dl
18+
setne %dl
19+
orl %edx, %eax
20+
movzbl %al, %eax
21+
ret
22+
.size _Z3funPKc, .-_Z3funPKc
23+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
24+
.section .note.GNU-stack,"",@progbits

asms1/fun12.s

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.file "fun12.cc"
2+
.text
3+
.globl _Z3funPKc
4+
.type _Z3funPKc, @function
5+
_Z3funPKc:
6+
endbr64
7+
pushq %rbx
8+
movq %rdi, %rbx
9+
call strlen@PLT
10+
xorl %edx, %edx
11+
movq %rax, %rcx
12+
cltq
13+
addq %rbx, %rax
14+
.L3:
15+
movb (%rbx,%rdx), %sil
16+
movl %edx, %edi
17+
testb %sil, %sil
18+
je .L4
19+
leaq 1(%rdx), %r8
20+
notq %rdx
21+
cmpb (%rax,%rdx), %sil
22+
jne .L4
23+
movq %r8, %rdx
24+
jmp .L3
25+
.L4:
26+
cmpl $4, %ecx
27+
popq %rbx
28+
setle %al
29+
cmpl %ecx, %edi
30+
setne %dl
31+
orl %edx, %eax
32+
movzbl %al, %eax
33+
ret
34+
.size _Z3funPKc, .-_Z3funPKc
35+
.ident "GCC: (Ubuntu 11.2.0-17ubuntu1) 11.2.0"
36+
.section .note.GNU-stack,"",@progbits

0 commit comments

Comments
 (0)