Mixing asm code and c code.

Programming, for all ages and all languages.
Post Reply
sav1or
Posts: 2
Joined: Sun Apr 18, 2010 1:06 pm

Mixing asm code and c code.

Post by sav1or »

Hello.
My problem is how to pass control to my c code from bootloader? I used http://www.osdever.net/tutorials/view/mixing-assembly-c tutorial , but it is not working :(
Are there any other ways to do it?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Mixing asm code and c code.

Post by Combuster »

There's no other way to get access to C code than doing this in assembly:

Code: Select all

call my_function_name
All other methods are hacks, which is one of the reasons the tutorial is bogus and can easily refuse to work (for one, main = 0x1000 is not guaranteed, attempts to return from main will cause crashes).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Mixing asm code and c code.

Post by Owen »

Code: Select all

void out(unsigned short _port, unsigned char _data)
{
   __asm__ ("out %%al, %%dx" : : "a" (_data), "d" (_port));
}
* Owen cries. People need to learn of and use GCC's "r" specifier... #-o
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Mixing asm code and c code.

Post by gravaera »

And to know how to program as well...

Or at least rtfm before posting...
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Mixing asm code and c code.

Post by NickJohnson »

Well, there are other possible ways to call a C function, but none simpler than "call <function>". If you are part of some odd programming cult that dislikes "call", or are part of an obfuscated code contest:

Code: Select all

mov eax, .done
push eax
jmp main
.done:
or even more obfuscated:

Code: Select all

xor ebx, ebx
mov eax, $
cmp ebx, 1
je .done
push eax
mov ebx, 1
jmp main
.done:
:twisted:
tharkun
Member
Member
Posts: 51
Joined: Sat Mar 21, 2009 1:29 pm
Location: Ireland

Re: Mixing asm code and c code.

Post by tharkun »

Owen wrote:* Owen cries. People need to learn of and use GCC's "r" specifier... #-o
Yes but consider these two pieces of code:

Code: Select all

inline void outb (uint16_t port, uin8_t value)
{
    asm volatile ("outb %0, %1" :: "a" (value), "dN" (port));
}

Code: Select all

inline void outb (uint16_t port, uint8_t value)
{
    asm volatile ("outb %0, %1" :: "r" (value), "r" (port));
}
In the first example the outb command, will always work for any input. However, in the second example you don't know that value and port will end up in the right registers, e.g al and dx.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Mixing asm code and c code.

Post by NickJohnson »

Why use inline assembly at all? It's compiler and architecture specific no matter how you slice it: external assembly is only architecture specific.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Mixing asm code and c code.

Post by Owen »

pinged wrote:
Owen wrote:* Owen cries. People need to learn of and use GCC's "r" specifier... #-o
Yes but consider these two pieces of code:

Code: Select all

inline void outb (uint16_t port, uin8_t value)
{
    asm volatile ("outb %0, %1" :: "a" (value), "dN" (port));
}

Code: Select all

inline void outb (uint16_t port, uint8_t value)
{
    asm volatile ("outb %0, %1" :: "r" (value), "r" (port));
}
In the first example the outb command, will always work for any input. However, in the second example you don't know that value and port will end up in the right registers, e.g al and dx.

Crap. I forgot the ioio instructions were still hobbled with regards to the supported registers.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Mixing asm code and c code.

Post by gravaera »

You can use architecture specific includes and have the asm inline, with the right include file selected based on the architecture being compiled for, NickJohnson. Most ASM snippets are small bits of code that are oft repeated, and lots of times in tight loops where the cost of an extra memory reference (the push on the call instruction) is a desirable thing to optimize out.

I think that's really the main reason to use inline ASM as opposed to calling and returning from a single snippet.

--All the best,
gravaera
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Mixing asm code and c code.

Post by NickJohnson »

Premature optimization is the root of all evil...

At least in my OS, there are no instances of assembly snippets for anything other than special instructions (loading/saving control regs, invlpg, sti/cli, in/out, etc.), which are either not in tight loops or expensive to the point that a single function call overhead is meaningless. The only instance where this is not true is with semaphore operations in userspace, which are used in enough places to deter the compiler from placing them inline anyway. To me, not having to adjust anything for different compilers (my kernel compiles under gcc, tcc, and clang with no preprocessor switches) is more valuable than a couple microseconds here and there.

I can't tell why exactly this thread went OT about inline asm though...
sav1or
Posts: 2
Joined: Sun Apr 18, 2010 1:06 pm

Re: Mixing asm code and c code.

Post by sav1or »

tnks for all. problem solved =D>
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Mixing asm code and c code.

Post by Owen »

NickJohnson wrote:Premature optimization is the root of all evil...

At least in my OS, there are no instances of assembly snippets for anything other than special instructions (loading/saving control regs, invlpg, sti/cli, in/out, etc.), which are either not in tight loops or expensive to the point that a single function call overhead is meaningless. The only instance where this is not true is with semaphore operations in userspace, which are used in enough places to deter the compiler from placing them inline anyway. To me, not having to adjust anything for different compilers (my kernel compiles under gcc, tcc, and clang with no preprocessor switches) is more valuable than a couple microseconds here and there.

I can't tell why exactly this thread went OT about inline asm though...
You are aware GCC, TCC and CLang all use the same inline assembly syntax? #-o
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Mixing asm code and c code.

Post by NickJohnson »

I wouldn't know: I don't use inline assembly! My point is that I avoid extensions that would break compatibility between any compilers; I consider inline assembly to be an extension. Perhaps a better way to say it is that my kernel compiles with "-std=c99 -pedantic".
Post Reply