Last active
May 19, 2025 11:40
-
-
Save Silica163/ba63b304ef8f798bac3b5fedb9064b10 to your computer and use it in GitHub Desktop.
c without stdlib and libc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void exit(int code); | |
| int write(int fd, const void * buff, int count); | |
| int strlen(char * str){ | |
| char * end = str; | |
| while(*(end++)); | |
| return end - str; | |
| } | |
| int main(int argc, char ** argv, char ** envp){ | |
| for(;argc > 0;argc--,argv++){ | |
| int len = strlen(*argv); | |
| write(1, *argv, len); | |
| write(1, "\n", 1); | |
| } | |
| for(;*envp != 0;){ | |
| int len = strlen(*envp); | |
| write(1, *envp, len); | |
| write(1, "\n", 1); | |
| envp++; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; nasm -f elf64 -o nolibc_starter.o nolibc_starter.s | |
| ; gcc main.c -o main -nolibc -nostdlib -fno-builtin nolibc_starter.o | |
| global exit, _start, write | |
| extern main | |
| section .text | |
| _start: | |
| xor rbp, rbp | |
| mov rdi, [rsp] ; 1st argc | |
| lea rsi, [rsp+8] ; 2nd argv | |
| lea rdx, [rsp+16+8*rdi] ; 3rd envp | |
| call main | |
| mov rdi, rax | |
| call exit | |
| exit: | |
| mov rax, 60 | |
| syscall | |
| write: | |
| push rbp | |
| mov rbp, rsp | |
| mov rax, 1 | |
| syscall | |
| pop rbp | |
| ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment