_start.s
authoryu.dongliang <18588496441@163.com>
Sat, 13 Dec 2025 13:46:35 +0000 (21:46 +0800)
committeryu.dongliang <18588496441@163.com>
Sat, 13 Dec 2025 13:46:35 +0000 (21:46 +0800)
Makefile
_start.s [new file with mode: 0644]
asm.s

index 331097d195e0a2f3da625563f8a53869f109c8c5..2f699c69e037c3198c2088b02027b6ca7da4c5c9 100644 (file)
--- 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 (file)
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 68a146702f881bba5e74a2f51b1782e56f12e443..e92ef741698b27bbd323479d713ad18c8b75d3d3 100644 (file)
--- 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