parent
563b978bf2
commit
0e469426dd
5 changed files with 73 additions and 0 deletions
@ -0,0 +1,35 @@ |
||||
/* Do not use this as the basis of your own libc.
|
||||
* The code is probably unoptimal or wonky, as I |
||||
* had no prior experience with this, but instead |
||||
* just fiddled with the code until it worked. |
||||
*/ |
||||
|
||||
#include<stdio.h> |
||||
|
||||
#define STDOUT 1 |
||||
#define SYS_WRITE 4 |
||||
|
||||
int simple_print(const char *msg, const long bufsize) { |
||||
int count; |
||||
long total_written = 0; |
||||
while(total_written < bufsize) { |
||||
asm( |
||||
"int $0x80\n\t" |
||||
: "=a"(count) |
||||
: "0"(SYS_WRITE), "b"(STDOUT), "c"(msg+total_written), "d"(bufsize-total_written) |
||||
:); |
||||
if(count == 0) { |
||||
return 1; |
||||
} |
||||
total_written += count; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
int simple_strlen(const char *str) { |
||||
int len = 0; |
||||
while(str[len] != '\0') { |
||||
len++; |
||||
} |
||||
return len; |
||||
} |
@ -0,0 +1,18 @@ |
||||
project('own libc', 'c') |
||||
|
||||
# A simple project that uses its own libc. |
||||
|
||||
# Start with manual flags to compile, then add platform support. |
||||
|
||||
libc = static_library('c', 'libc.c', 'stubstart.s', |
||||
c_args : '-nostdlib', |
||||
link_args : '-nostdlib', |
||||
) |
||||
|
||||
exe = executable('selfcontained', 'prog.c', |
||||
c_args : '-nostdlib', |
||||
link_args : '-nostdlib', |
||||
link_with : libc, |
||||
) |
||||
|
||||
test('standalone test', exe) |
@ -0,0 +1,7 @@ |
||||
|
||||
#include<stdio.h> |
||||
|
||||
int main() { |
||||
const char *message = "Hello without stdlib.\n"; |
||||
return simple_print(message, simple_strlen(message)); |
||||
} |
@ -0,0 +1,5 @@ |
||||
#pragma once |
||||
|
||||
int simple_print(const char *msg, const long bufsize); |
||||
|
||||
int simple_strlen(const char *str); |
@ -0,0 +1,8 @@ |
||||
.globl _start
|
||||
|
||||
_start: |
||||
|
||||
call main |
||||
movl %eax, %ebx |
||||
movl $1, %eax |
||||
int $0x80 |
Loading…
Reference in new issue