Mixing asm code and c code.
Mixing asm code and c code.
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?
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?
- Combuster
- 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.
There's no other way to get access to C code than doing this in assembly:
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).
Code: Select all
call my_function_name
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Mixing asm code and c code.
Code: Select all
void out(unsigned short _port, unsigned char _data)
{
__asm__ ("out %%al, %%dx" : : "a" (_data), "d" (_port));
}
- gravaera
- 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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Mixing asm code and c code.
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:
or even more obfuscated:
Code: Select all
mov eax, .done
push eax
jmp main
.done:
Code: Select all
xor ebx, ebx
mov eax, $
cmp ebx, 1
je .done
push eax
mov ebx, 1
jmp main
.done:
Re: Mixing asm code and c code.
Yes but consider these two pieces of code:Owen wrote:* Owen cries. People need to learn of and use GCC's "r" specifier...
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));
}
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Mixing asm code and c code.
Why use inline assembly at all? It's compiler and architecture specific no matter how you slice it: external assembly is only architecture specific.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Mixing asm code and c code.
pinged wrote:Yes but consider these two pieces of code:Owen wrote:* Owen cries. People need to learn of and use GCC's "r" specifier...
Code: Select all
inline void outb (uint16_t port, uin8_t value) { asm volatile ("outb %0, %1" :: "a" (value), "dN" (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.Code: Select all
inline void outb (uint16_t port, uint8_t value) { asm volatile ("outb %0, %1" :: "r" (value), "r" (port)); }
Crap. I forgot the ioio instructions were still hobbled with regards to the supported registers.
- gravaera
- 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.
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
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.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Mixing asm code and c code.
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...
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...
Re: Mixing asm code and c code.
tnks for all. problem solved
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Mixing asm code and c code.
You are aware GCC, TCC and CLang all use the same inline assembly syntax?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...
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Mixing asm code and c code.
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".