asm 1 commit
authoryu.dongliang <18588496441@163.com>
Thu, 11 Dec 2025 12:53:27 +0000 (20:53 +0800)
committeryu.dongliang <18588496441@163.com>
Thu, 11 Dec 2025 12:53:27 +0000 (20:53 +0800)
Makefile [new file with mode: 0644]
main.c [new file with mode: 0644]
str.s [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..3a2ff19
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+all:
+       gcc main.c str.s
diff --git a/main.c b/main.c
new file mode 100644 (file)
index 0000000..8dddeea
--- /dev/null
+++ b/main.c
@@ -0,0 +1,18 @@
+#include<stdio.h>
+#include<stdlib.h>
+
+int asm_strcmp (const char* s1, const char* s2);
+int asm_strncmp(const char* s1, const char* s2, int n);
+
+int main(int argc, char* argv[])
+{
+       if (argc < 4) {
+               printf("usage: ./a.out s1 s2 n\n");
+               return -1;
+       }
+
+       printf("strcmp: %d, strncmp: %d\n",
+                       asm_strcmp (argv[1], argv[2]),
+                       asm_strncmp(argv[1], argv[2], atoi(argv[3])));
+       return 0;
+}
diff --git a/str.s b/str.s
new file mode 100644 (file)
index 0000000..be69be8
--- /dev/null
+++ b/str.s
@@ -0,0 +1,56 @@
+.text
+.global asm_strncmp, asm_strcmp
+
+asm_strcmp:
+#rdi = s1
+#rsi = s2
+0:
+       movb (%rdi), %al
+       cmpb (%rsi), %al
+       jl   2f
+       jg   3f
+
+       test %al, %al
+       je   1f 
+
+       inc  %rsi
+       inc  %rdi
+       jmp  0b 
+1:
+       mov $0, %eax
+       ret
+2:
+       mov $-1, %eax
+       ret
+3:
+       mov $1, %eax
+       ret
+
+asm_strncmp:
+#rdi = s1
+#rsi = s2
+#edx = n
+0:
+       movb (%rdi), %al
+       cmpb (%rsi), %al
+       jl   2f
+       jg   3f
+
+       test %al, %al
+       je   1f 
+
+       inc  %rsi
+       inc  %rdi
+       dec  %edx
+
+       test %edx, %edx
+       jne  0b
+1:
+       mov $0, %eax
+       ret
+2:
+       mov $-1, %eax
+       ret
+3:
+       mov $1, %eax
+       ret