From: yu.dongliang <18588496441@163.com> Date: Thu, 11 Dec 2025 13:35:43 +0000 (+0800) Subject: tmp X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=279b55c3f390ea23c4aff923873e636c9aac14ab;p=asm_demo.git tmp --- diff --git a/Makefile b/Makefile index 3a2ff19..331097d 100644 --- 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 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 8dddeea..f1f3cde 100644 --- 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; }