File tree Expand file tree Collapse file tree 6 files changed +107
-13
lines changed Expand file tree Collapse file tree 6 files changed +107
-13
lines changed Original file line number Diff line number Diff line change 11#! /bin/bash
2- # see .github/workflows/make.yml
2+ #
3+ # see .github/workflows/make.yml
34
4- cd cat
5- make
6- make check
5+ die () {
6+ echo " ERROR: $@ "
7+ exit 1
8+ }
9+
10+ cd print_int
11+
12+ make print_int || die " make print_int"
13+ make write || die " make write"
14+ make strcmp || die " make strcmp"
15+ make check || die " make check"
16+ make check_write || die " make check_write"
17+ make check_strcmp || die " make check_strcmp"
Original file line number Diff line number Diff line change 33define hook-quit
44 set confirm off
55end
6- break print_int
6+ break string_compare
Original file line number Diff line number Diff line change 11/print_int
22/write
3+ /strcmp
Original file line number Diff line number Diff line change 1- build :
1+ print_int :
22 $(CC ) main.s -c -o print_int.o
33 $(LD ) -e main -o print_int print_int.o
44
5- check : build
5+ check : print_int
66 ./print_int | md5sum | grep -q ^3cca2fa && printf " \033[1;32m[OK]\033[0m\n"
77
8- build_write :
8+ write :
99 $(CC ) write.s -c -o write.o
1010 $(LD ) -e main -o write write.o
1111
12- check_write : build_write
12+ check_write : write
1313 ./write | grep -q ' ^ABC$$' && printf " \033[1;32m[OK]\033[0m\n"
1414
15+ strcmp :
16+ $(CC ) strcmp.s -c -o strcmp.o
17+ $(LD ) -e main -o strcmp strcmp.o
18+
19+ check_strcmp : strcmp
20+ ./strcmp | md5sum | grep -q ^00350de && printf " \033[1;32m[OK]\033[0m\n"
1521
1622clean :
17- rm -f * .o write print_int
23+ rm -f * .o write print_int strcmp
Original file line number Diff line number Diff line change 3737 add $24 , %rsp
3838 .endm
3939
40+ .macro strcmp s1 s2 count
41+ push \s1
42+ push \s2
43+ push \count
44+ call string_compare
45+ add $24 , %rsp
46+ .endm
47+
4048 .macro write address size
4149 push \address
4250 push \size
@@ -191,9 +199,9 @@ copymem:
191199 enter
192200 push_all
193201 push %rax
194- mov 32 (%rbp ), %rsi # param: source
195- mov 24 (%rbp ), %rdi # param: destination
196- mov 16 (%rbp ), %rdx # param: count
202+ mov 32 (%rbp ), %rsi # char * source
203+ mov 24 (%rbp ), %rdi # char * destination
204+ mov 16 (%rbp ), %rdx # int count
197205 mov $0 , %rcx
198206 test %rdx , %rdx
199207 jz copymem_done
@@ -207,3 +215,30 @@ copymem_done:
207215 pop %rax
208216 pop_all
209217 return
218+
219+ string_compare:
220+ enter
221+ push_all
222+ mov 32 (%rbp ), %rsi # char *s1
223+ mov 24 (%rbp ), %rdi # char *s2
224+ mov 16 (%rbp ), %rdx # int count
225+ mov $0 , %rcx
226+ test %rdx , %rdx
227+ jz string_compare_ret0
228+ string_compare_loop:
229+ mov (%rsi , %rcx ), %rax
230+ mov (%rdi , %rcx ), %rbx
231+ cmp %al , %bl
232+ jne string_compare_ret1
233+ inc %rcx
234+ cmp %rcx , %rdx
235+ je string_compare_ret0
236+ jmp string_compare_loop
237+ string_compare_ret1:
238+ mov $1 , %rax
239+ jmp string_compare_done
240+ string_compare_ret0:
241+ mov $0 , %rax
242+ string_compare_done:
243+ pop_all
244+ return
Original file line number Diff line number Diff line change 1+ .data
2+ foo:
3+ .ascii "abc"
4+ .text
5+ .globl main
6+
7+ .include "print_int.s"
8+
9+ main:
10+ enter
11+ sub $48 , %rsp
12+ movb $0x41 , -24 (%rbp )
13+ movb $0x42 , -23 (%rbp )
14+ movb $0x43 , -22 (%rbp )
15+ movb $0x44 , -21 (%rbp )
16+ movb $0x41 , -48 (%rbp )
17+ movb $0x42 , -47 (%rbp )
18+ movb $0x43 , -46 (%rbp )
19+ movb $0x12 , -45 (%rbp )
20+ mov $2 , %rax
21+ mov %rbp , %rsi
22+ mov %rbp , %rdi
23+ sub $24 , %rsi
24+ sub $48 , %rdi
25+ strcmp %rsi , %rdi , $3
26+ log %rax
27+ mov $2 , %rax
28+ strcmp %rdi , %rsi , $3
29+ log %rax
30+ mov $2 , %rax
31+ strcmp %rdi , %rsi , $4
32+ log %rax
33+ mov $2 , %rax
34+ strcmp %rdi , %rsi , $3
35+ log %rax
36+ call exit
37+
38+ exit:
39+ movq $EXIT, %rax
40+ movq $0 , %rdi
41+ syscall
You can’t perform that action at this time.
0 commit comments