problem in putting asm in c files

Programming, for all ages and all languages.
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

problem in putting asm in c files

Post by psnix »

hi

how can I convert this code which gcc compiler support this:

Code: Select all

static void gdtInstall (void)
{
    #if _HAL_COMPILER ==_HAL_COMPILER_MSVC
        _asm
        {
            lgdt [_gdtr]
            mov ax,0x10 // 0x10 is the offset in the GDT to kernel data segment
            mov ds,ax   // load all data segment selectors
            mov es,ax
            mov fs,ax
            mov gs,ax
            mov ss,ax
            jmp 0x08:.flush // 0x8 is the offset to kernel code segment in GDT
            .flush
        }
	#elif _HAL_COMPILER == _HAL_COMPILER_GNUC
             
       // GCC CODE HERE
}
	#endif
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: problem in putting asm in c files

Post by Selenic »

If GCC won't compile that asm block as it is, try surrounding it with quotes and see if that works

For anything more complicated (passing values to the assembly code or whatever), these two sections of the GCC manual are *very* useful:

Extended assembly and Assembly constraints
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: problem in putting asm in c files

Post by Owen »

Selenic wrote:If GCC won't compile that asm block as it is, try surrounding it with quotes and see if that works

For anything more complicated (passing values to the assembly code or whatever), these two sections of the GCC manual are *very* useful:

Extended assembly and Assembly constraints
I somehow doubt it will "just work" because, well, AT&T syntax looks quite different.

You'll just have to learn GCC's assembly syntax. It's more effort, but it's worth it: The code produced around it is miles better than that by MSVC for the same value
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: problem in putting asm in c files

Post by Selenic »

Owen wrote:I somehow doubt it will "just work" because, well, AT&T syntax looks quite different
Oh yeah, I didn't notice that. OTOH, I think GAS supports both styles, if you put the right directives around your code. If not, you just need to convert it to AT&T syntax (not very difficult)
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: problem in putting asm in c files

Post by Combuster »

Selenic wrote:you just need to convert it to AT&T syntax (not very difficult)
cough cough

I doubt the n00b next door could pull it off.
"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 ]
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: problem in putting asm in c files

Post by Selenic »

Combuster wrote:
Selenic wrote:you just need to convert it to AT&T syntax (not very difficult)
cough cough

I doubt the n00b next door could pull it off.
By 'not very difficult' I meant in comparison to, say, converting C to Haskell (as an example). You still have to be careful to make sure you get everything, but you *could* cheat and use an assembler in one format and disassemble it in the other (though for the OP's case that would probably be more work than doing it by hand)
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: problem in putting asm in c files

Post by Combuster »

Selenic wrote:By 'not very difficult' I meant in comparison to, say, converting C to Haskell (as an example).
Oh, but that's easy! You just don't do that :mrgreen:
"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
XanClic
Member
Member
Posts: 138
Joined: Wed Feb 13, 2008 9:38 am

Re: problem in putting asm in c files

Post by XanClic »

Selenic wrote:
Owen wrote:I somehow doubt it will "just work" because, well, AT&T syntax looks quite different
Oh yeah, I didn't notice that. OTOH, I think GAS supports both styles, if you put the right directives around your code.
Using -masm=intel as a command line parameter when invoking gcc is sufficient. :wink:
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: problem in putting asm in c files

Post by psnix »

hi
finally i convert this code but something is wrong and compiler can not compile it!

what is wrong in this code ?

Code: Select all

asm("lgdt (%0)": :"p" (&_gdtr));
 asm("movl $0x10,%eax \n\t"      // 0x10 is the offset in the GDT to kernel data segment
"movl %eax,%ds \n\t"       // load all data segment selectors
"movl %eax,%es \n\t"
"movl %eax,%fs \n\t"
"movl %eax,%gs \n\t"
"movl %eax,%ss \n\t"
"jmp $0x8:flush \n\t" // 0x8 is the offset to kernel code segment in GDT
"flush: nop \n\t"
            );
errors:
../hal/gdt.c|59|Error: junk `:(flush)' after expression|
../hal/gdt.c|59|Error: suffix or operands invalid for `jmp'|
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: problem in putting asm in c files

Post by pcmattman »

The error says what is wrong with the code. Now, how do you do a far jump in AT&T syntax compared to Intel syntax? Hint: the colon shouldn't be there.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: problem in putting asm in c files

Post by NickJohnson »

Or you could just remove that non-portable inline assembly, and instead use a separate assembler and combine them at link time. That way you're guaranteed it will work regardless of which compiler you're using. You can even run the C preprocessor on the assembly code if you need macros for configuration.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: problem in putting asm in c files

Post by Owen »

NickJohnson wrote:Or you could just remove that non-portable inline assembly, and instead use a separate assembler and combine them at link time. That way you're guaranteed it will work regardless of which compiler you're using. You can even run the C preprocessor on the assembly code if you need macros for configuration.
And also much slower at runtime ;-)

A separate assembler is unportable anyway.

To the OP: You need to tell GCC you're clobbering EAX.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: problem in putting asm in c files

Post by Solar »

Owen wrote:
NickJohnson wrote:Or you could just remove that non-portable inline assembly, and instead use a separate assembler and combine them at link time. That way you're guaranteed it will work regardless of which compiler you're using. You can even run the C preprocessor on the assembly code if you need macros for configuration.
And also much slower at runtime ;-)

A separate assembler is unportable anyway.
I don't see where calling static void gdtInstall (void) makes "much" difference, performance-wise, whether it's written in inline or extern ASM? The extern solution is definitely cleaner, as you have separate source files for separate languages. (Warning: ASM noob here, I might just be missing the obvious.)
Every good solution is obvious once you've found it.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: problem in putting asm in c files

Post by Brendan »

Hi,
Solar wrote:I don't see where calling static void gdtInstall (void) makes "much" difference, performance-wise, whether it's written in inline or extern ASM? The extern solution is definitely cleaner, as you have separate source files for separate languages. (Warning: ASM noob here, I might just be missing the obvious.)
Performance wise, using an external (assembly) routine would cost a CALL/RET, which adds about 2 cycles on a modern CPU. As this code would be run once during boot, the end result is that the OS would take about 1 nanosecond longer to boot.

On the other hand, for the life-time of the code, it'll probably cost programmers 20 hours or more in maintenance and assorted hassles.

Simple maths says that unless the OS is booted more than 72000000000000 times (e.g. 36 million users that boot it 2 million times each), using inline code is a waste of time (the time spent maintaining it is more than the time saved booting it).

However, there is a compromise - do the entire "gdtInstall()" in an external assembly routine. That way you avoid the extra CALL/RET, and separating architecture specific code from the portable C code will make it easier/quicker to port to a different architecture (and avoid time spent for "maintenance and assorted hassles"). It's a "win win" situation...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: problem in putting asm in c files

Post by Owen »

I wasn't refering to "gdtInstall" specifically there - but some functions probably have higher impact.
Post Reply