How would I take input or print output without using libc? I can use system calls, but didn't know if that would help.
-
1`libc` provides a portable abstraction for doing this... you can certainly do I/O without libc, but you need to tell us what system this is to get any meaningful answer. – FatalError Feb 08 '13 at 16:44
-
x64 system is what I am using – user1888502 Feb 08 '13 at 16:46
-
Given the mention of `libc` it appears that this is a Unix or Linux question. But yes, it would be nice to identify the target platform. – DigitalRoss Feb 08 '13 at 16:49
-
...why would you want to do that? – Mike Feb 08 '13 at 16:58
-
I am expected to do it, so I was wondering if you fine folks can put me on the right track. – user1888502 Feb 08 '13 at 17:01
-
1See: http://stackoverflow.com/questions/2548486/compiling-without-libc – jxh Feb 08 '13 at 17:08
-
@user1888502 - ok, so homework assignment? Were you given any guidance on what method to use? What was the actual problem statement? They way you've phrased it is very open to interpretation. – Mike Feb 08 '13 at 17:09
2 Answers
I stepped through write(2)
with gdb to get an idea of how the system call ABI works.
Anyway, no libc at all. Note that without special tricks, the cc(1) compiler/linker front-end will still link you with libc, but you won't be using it for anything. The C runtime start-up code will make some libc calls, but this program won't.
void mywrite(int fd, const void *b, int c) {
asm("movl $1, %eax");
asm("syscall");
}
int main(void) { const char *s = "Hello world.\n"; return mywrite(1, s, 13), 0; }

- 143,651
- 25
- 248
- 329
-
-
Aha, then you could use `asm()` statements or type in your own `write.s` wrapper around whatever instruction the x86_64 ABI uses to trap into the kernel. – DigitalRoss Feb 08 '13 at 16:54
-
how would I use asm statements, which system calls do I have to implement or call? for taking input and showing output? – user1888502 Feb 08 '13 at 16:58
-
2`how would I use asm statements` - if you have to ask that, you shouldn't be using asm statements. – Mike Feb 08 '13 at 17:01
-
@DigitalRoss, isn't that just for printing? How would I take input, since I can't use scanf, how would I go by having a user pass input through console, and me parsing; without using scanf.. – user1888502 Feb 08 '13 at 18:06
There is no platform-independent way to do this. In fact, the whole point of having libc
is to have a common interface to a set of functionality that most systems provide, but do so in fundamentally different ways.
Your best option would probably be to consult the documentation for whatever system you are currently using. You could look up your OS's set of interrupts and then try using the asm
keyword to write assembly instructions that tell the OS to read input or display output. You could look into libraries provided by the OS for doing input and output on file descriptors, then use those functions instead. Or, you could look at process creation libraries, then spawn off a process to read or write data from the console, where the second program uses libc
. None of these are guaranteed to be at all portable, though.
Hope this helps!

- 362,284
- 104
- 897
- 1,065