Page 1 of 1

how to use asm functions in C code

Posted: Tue Feb 07, 2006 6:46 am
by gmax136
i wrote an asm code
[BITS 16]


start:???
jmp $??? ???

how to use start function in my c program?
what can i do?

NOTE:
i am sorry for vote.When i try send a message without a vote, i get an error: NO question!

Re:how to use asm functions in C code

Posted: Tue Feb 07, 2006 7:56 am
by Candy
gmax136 wrote: i wrote an asm code
[BITS 16]


main:???
jmp $??? ???

how to use start function in my c program?
what can i do?

NOTE:
i am sorry for vote.When i try send a message without a vote, i get an error: NO question!
Try using the "New topic" button instead of the "New poll" button.

Re:how to use asm functions in C code

Posted: Tue Feb 07, 2006 8:12 am
by OZ
you have to declare/specify/whatever it's called for asm the function to be global. Then you can declare it in C to be an extern function and use it. But then you've got to respect the C calling conventions like standard stack frame etc. Look in the local FAQ for that (click on the Mega-Tokyo banner above!)

Code: Select all

; ASM PART
global kwrite_cr3

kwrite_cr3:
   push ebp
   mov ebp,esp
   mov eax,[ebp+8]
   mov cr3, eax
   pop ebp
   retn

/* C Head file */
extern void kwrite_cr3(ulong value);

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 12:52 am
by hendric
First of all,the function should be under __cdecl.Say,
.code32
.globl _start;
_start:
jmp $

Then you can declare a function as incoming prototype:
extern "C" void start();
And you will be able to call it anywhere.

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 5:16 am
by Pype.Clicker
hendric wrote: First of all,the function should be under __cdecl.Say,
.code32
.globl _start;
_start:
jmp $
that underscore characer is something specific to MS platforms.
Then you can declare a function as incoming prototype:
extern "C" void start();
And you will be able to call it anywhere.
that's for C++ only, afaik. with pure C compiler, nothing like that is needed (and i'm not sure pure C would allow that construct anyway).

In other words, read your assembler's manual and tutorials: that's very likely to be detailed there. i can remember of plenty of foo/bar examples in NASM tutorials and exemples showing how to call ASM from C, C from ASM , ASM from other ASM, passing parameters, etc.

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 7:12 am
by hendric
Pype.Clicker wrote:
hendric wrote: First of all,the function should be under __cdecl.Say,
.code32
.globl _start;
_start:
jmp $
that underscore characer is something specific to MS platforms.
Is that true?I found there were many functions with underscore prefix in .S file when reading about the source of linux.

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 7:18 am
by Solar
Some compilers prefix C function names with an underscore when translating them to assembler. In GCC, this behaviour can be toggled with -fleading-underscore / -fno-leading-underscore.

If your C compiler adds such underscores, you have to prefix your Assembler function with an underscore so it can be accessed from within the C code without the underscore.

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 7:47 am
by Pype.Clicker
hendric wrote: Is that true?I found there were many functions with underscore prefix in .S file when reading about the source of linux.
yes, afaik. checking quickly "entry.S" validates it since some symbols (syscall_table), obviously coming from C world are used without prepended underscores.

It indeed appears that some of those symbol do have __ prepended, and i assume this has a special semantic (e.g. __kernel_vsyscall being used specifically in a linker script, etc. -- in arch/i386/kernel on my release). but when C code use it (e.g. um/os-linux/elf_aux.c), they still do invoke it as __kernel_vsyscall.

I can therefore confirm you that not only Clicker and regular linux programs but also the linux kernel (and afaik, anything that complies to the ELF abi) do not require underscore to be prepended and that any leading underscore is the sole decision of the programmer himself.

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 7:51 am
by Solar
<language lawyer mode>

Note that any identifier beginning with two underscores, or one underscore and an uppercase letter, is "reserved by the implementation" by standard's definition, i.e. reserved by the compiler and / or the standard library. Look out for collisions.

</language lawyer mode>

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 8:56 am
by hendric
Thank you for all your replies. :P

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 11:23 am
by gmax136
hey wait this my topic :D

OK, i learned somethings, but how can i compile this files?

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 11:46 am
by Pype.Clicker
which files ? be more precise, please: this is not "guess what's wrong with 20 questions" game, you know ...

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 12:46 pm
by gmax136
my asm function file and my C code
How to compile?

Re:how to use asm functions in C code

Posted: Wed Feb 08, 2006 2:59 pm
by Pype.Clicker
i don't know about how to compile your files! that's up to you ... that depends on what format you want, what tools you have etc.

What i can give you is this FAQ page that shows how to build and link together an ASM file and a C file with GNU toolchain and feed them to GRUB for booting.