From: yu.dongliang <18588496441@163.com> Date: Wed, 28 Aug 2024 08:56:16 +0000 (+0800) Subject: support argc & argv in main() for x64, support fopen(), fclose(), ... X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=e837f3a4fa9a11350cca0ce8e147787c3b4f6fdb;p=scf.git support argc & argv in main() for x64, support fopen(), fclose(), ... --- diff --git a/examples/fopen.c b/examples/fopen.c new file mode 100644 index 0000000..62be9a5 --- /dev/null +++ b/examples/fopen.c @@ -0,0 +1,26 @@ +/*#include +#include +*/ +void* fopen(const char* path, const char* mode); +int fclose(void* fp); +int fscanf(void* fp, const char* fmt, ...); + +int printf(const char* fmt, ...); + +int main() +{ + void* fp = fopen("1.txt", "r"); + if (!fp) { + printf("fp: %p\n", fp); + return -1; + } + + char* s[64]; + + int n = fscanf(fp, "%s", s); + + printf("s: %s, n: %d\n", s, n); + + fclose(fp); + return 0; +} diff --git a/examples/main_argv.c b/examples/main_argv.c new file mode 100644 index 0000000..fe50ca8 --- /dev/null +++ b/examples/main_argv.c @@ -0,0 +1,11 @@ + +int printf(const char* fmt, ...); + +int main(int argc, char** argv, char** envp) +{ + int i; + for (i = 0; i < argc; i++) + printf("%s\n", argv[i]); + + return 0; +} diff --git a/lib/_start.o b/lib/_start.o index bca4a4d..38dbb23 100644 Binary files a/lib/_start.o and b/lib/_start.o differ diff --git a/lib/_start.s b/lib/_start.s index a5ba655..2df9e03 100644 --- a/lib/_start.s +++ b/lib/_start.s @@ -2,8 +2,19 @@ .global _start, 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 main mov %rax, %rdi mov $60, %rax syscall -.fill 7, 1, 0 +.fill 5, 1, 0