From f1816f6a1db537069e1f874b9730cf9262006de9 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Thu, 11 Dec 2025 20:53:27 +0800 Subject: [PATCH 1/1] asm 1 commit --- Makefile | 2 ++ main.c | 18 ++++++++++++++++++ str.s | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 Makefile create mode 100644 main.c create mode 100644 str.s diff --git a/Makefile b/Makefile new file mode 100644 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 index 0000000..8dddeea --- /dev/null +++ b/main.c @@ -0,0 +1,18 @@ +#include +#include + +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 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 -- 2.25.1