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
--- /dev/null
+.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
.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:
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
mov $10, %rdi
call putchar
pop %rdi
+ pop %rax
+ pop %rax
+
+ test %al,%al
+ je 6f
inc %rdi
jmp 0b
-5:
+6:
ret