how to use asm functions in C code

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gmax136

how to use asm functions in C code

Post 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!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:how to use asm functions in C code

Post 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.
OZ

Re:how to use asm functions in C code

Post 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);
hendric

Re:how to use asm functions in C code

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how to use asm functions in C code

Post 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.
hendric

Re:how to use asm functions in C code

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:how to use asm functions in C code

Post 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.
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how to use asm functions in C code

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:how to use asm functions in C code

Post 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>
Every good solution is obvious once you've found it.
hendric

Re:how to use asm functions in C code

Post by hendric »

Thank you for all your replies. :P
gmax136

Re:how to use asm functions in C code

Post by gmax136 »

hey wait this my topic :D

OK, i learned somethings, but how can i compile this files?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how to use asm functions in C code

Post by Pype.Clicker »

which files ? be more precise, please: this is not "guess what's wrong with 20 questions" game, you know ...
gmax136

Re:how to use asm functions in C code

Post by gmax136 »

my asm function file and my C code
How to compile?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:how to use asm functions in C code

Post 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.
Post Reply