Using C libs from Assembly... Help linking?

Hello all. I'd like to ask for some help with this. I've started getting into assembly a little. I'm enjoying myself, but sometimes I just want a freaking printf() or fscanf(). I'm under the impression that I just push the arguments to the stack in reverse order, but how do I include/link to the C libraries? And do I need to add back to the stack pointer after the function call or does the C function do that for me? Any help would be appreciated. Thanks!
 
I assume you're talking about IA32 using a world built with the default CFLAGS (cdecl); then you push the parameters on the stack, CALL printf, and get it's return code in the accumulation register. After the return from printf, you still have the stack pointer pointing to the zero-th parameter to printf (where it was pointing when you did CALL), and therefore you'll have to ADD it up by the length of the parameters(or not, if you want to reuse the parameters).

Add enough optimization CFLAGS to your buildworld, and this might not work any longer though. Also, on some implementations printf might be a macro for fprintf, but in FreeBSD it's not ;)

To link to printf, you'll need to link against the libc. Using GCC that's done with -lc
The include syntax depends on your assembler. With GCC it's #include <stdio.h> , like in C
 
Thanks xibo, I tested with putchar and was told:
Code:
/usr/lib/libc.so : undefined reference to environ and __progname
using as on amd64. This is a linker problem not programming problem, correct?
 
If it is reading and writing to the command prompt you would like to do, maybe the kernel system calls would suffice (if you are not using them already). instead of printf() you could use write() (writing to the stdout file descriptor (1)). I believe you can see the system call reference in /usr/src/sys/kern/syscalls.master.
 
I thing that I'm just linking wrong.
as -o this.o this.s
ld -o this this.o -lc
If anyone knows what I'm doing wrong, please lend a hand. Otherwise, I'll just have to do without for a bit. Thanks again for the help with the calling convention thing.
Tim
 
Back
Top