tmp
authoryu.dongliang <18588496441@163.com>
Thu, 11 Dec 2025 13:35:43 +0000 (21:35 +0800)
committeryu.dongliang <18588496441@163.com>
Thu, 11 Dec 2025 13:35:43 +0000 (21:35 +0800)
Makefile
asm.s [new file with mode: 0644]
main.c

index 3a2ff192b93397f51f40da3159c4974711404872..331097d195e0a2f3da625563f8a53869f109c8c5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,2 @@
 all:
-       gcc main.c str.s
+       gcc -fPIE main.c str.s asm.s
diff --git a/asm.s b/asm.s
new file mode 100644 (file)
index 0000000..20a22d2
--- /dev/null
+++ b/asm.s
@@ -0,0 +1,41 @@
+.text
+.global printf
+.global asm_main
+
+asm_main:
+#rdi = asm code
+0:
+       mov %rdi, %rsi
+1:
+       movb (%rdi), %al
+
+       cmpb $32, %al
+       je   2f
+
+       cmpb $44, %al
+       je   3f
+
+       cmpb $10, %al
+       je   4f
+
+       test %al, %al
+       je   5f
+
+       inc %rdi
+       jmp 1b
+
+2:#before ' ' is opcode
+3:#before ',' is operand 1
+4:#before '\n' is operand 2
+       movb $0, (%rdi)
+
+       push %rdi
+       mov  %rsi, %rdi
+       mov  $0, %eax
+       call printf
+       pop  %rdi
+
+       inc %rdi
+       jmp 0b
+5:
+       ret
diff --git a/main.c b/main.c
index 8dddeeac6daa8d8de08ed8f1b6b826228d975cbc..f1f3cde8fae69e78e7098b178447ca257ab0cc6b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,6 +4,8 @@
 int asm_strcmp (const char* s1, const char* s2);
 int asm_strncmp(const char* s1, const char* s2, int n);
 
+int asm_main(const char* asm_code);
+
 int main(int argc, char* argv[])
 {
        if (argc < 4) {
@@ -11,8 +13,17 @@ int main(int argc, char* argv[])
                return -1;
        }
 
+       printf("' ':%d, ',':%d\n", ' ', ',');
+
        printf("strcmp: %d, strncmp: %d\n",
                        asm_strcmp (argv[1], argv[2]),
                        asm_strncmp(argv[1], argv[2], atoi(argv[3])));
+
+       char asm_code[] = "mov rax,rdx\n";
+
+       int i;
+       for (i = 0; i < sizeof(asm_code); i++)
+               printf("asm_code[%d]: %d\n", i, asm_code[i]);
+       asm_main(asm_code);
        return 0;
 }