From 7f051416a84e5496282e3ba63336e5b44e827d40 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sat, 13 Dec 2025 21:46:35 +0800 Subject: [PATCH] _start.s --- Makefile | 7 ++++++- _start.s | 20 ++++++++++++++++++++ asm.s | 19 ++++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 _start.s diff --git a/Makefile b/Makefile index 331097d..2f699c6 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,7 @@ all: - gcc -fPIE main.c str.s asm.s + as _start.s -o _start.o + as asm.s -o asm.o + ld _start.o asm.o -lc -o a.out +# gcc -fPIE main.c str.s asm.s +clean: + rm *.o diff --git a/_start.s b/_start.s new file mode 100644 index 0000000..fef28ca --- /dev/null +++ b/_start.s @@ -0,0 +1,20 @@ +.text +.global _start, asm_main + +_start: + mov %rsp, %rsi + add $8, %rsi # argv + mov %rsi, %rdx +1: + mov (%rdx), %rdi + add $8, %rdx # envp + test %rdi, %rdi + jnz 1b + + mov (%rsp), %rdi # argc + + call asm_main + mov %rax, %rdi + mov $60, %rax + syscall +.fill 5, 1, 0 diff --git a/asm.s b/asm.s index 68a1467..e92ef74 100644 --- a/asm.s +++ b/asm.s @@ -3,7 +3,13 @@ .global asm_main asm_main: -#rdi = asm code +#rdi = argc +#rsi = argv +#rdx = envp + cmp $2, %rdi + jl 5f + + mov 8(%rsi), %rdi 0: mov %rdi, %rsi 1: @@ -28,7 +34,9 @@ asm_main: 3:#before ',' is operand 1 4:#before '\n' is operand 2 movb $0, (%rdi) - +5: + push %rax + push %rax push %rdi mov %rsi, %rdi mov $0, %eax @@ -37,8 +45,13 @@ asm_main: mov $10, %rdi call putchar pop %rdi + pop %rax + pop %rax + + test %al,%al + je 6f inc %rdi jmp 0b -5: +6: ret -- 2.25.1