File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // -nostdlib -ffreestanding
2+
3+ //gcc -nostdlib -static
4+
5+ asm(
6+ ".global _start\n"
7+ "_start:\n"
8+ " xorl %ebp,%ebp\n" // mark outermost stack frame
9+ " movq 0(%rsp),%rdi\n" // get argc
10+ " lea 8(%rsp),%rsi\n" // the arguments are pushed just below, so argv = %rbp + 8
11+ " call bare_main\n" // call our bare_main
12+ " movq %rax,%rdi\n" // take the main return code and use it as first argument for...
13+ " movl $60,%eax\n" // ... the exit syscall
14+ " syscall\n"
15+ " int3\n" ); // just in case
16+
17+ asm(
18+ "bare_write:\n" // write syscall wrapper; the calling convention is pretty much ok as is
19+ " movq $1,%rax\n" // 1 = write syscall on x86_64
20+ " syscall\n"
21+ " ret\n" );
22+
23+
24+ int bare_write (int fd , const void * buf , unsigned count );
25+
26+ unsigned my_strlen (const char * ch ) {
27+ const char * ptr ;
28+ for (ptr = ch ; * ptr ; ++ ptr );
29+ return ptr - ch ;
30+ }
31+
32+ int bare_main (int argc , char * argv []) {
33+ for (int i = 0 ; i < argc ; ++ i ) {
34+ int len = my_strlen (argv [i ]);
35+ bare_write (1 , argv [i ], len );
36+ bare_write (1 , "\n" , 1 );
37+ }
38+ return argc ;
39+ }
You can’t perform that action at this time.
0 commit comments