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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 389 additions & 0 deletions
Large diffs are not rendered by default.

asms1/fun.gdb

Lines changed: 79 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)