Page 1 of 1

Inline ASM in C(GCC)

Posted: Fri Nov 26, 2004 6:28 am
by Vladaz
Hello,
can someone help me and translate this line to full c function or smth, that would do inline assembler:
mov ax, x
when x is my variable.
That variabl x is unsigned char.

Re:Inline ASM in C(GCC)

Posted: Fri Nov 26, 2004 7:33 am
by Solar
With a tiny bit of tweaking the example given at http://www.osdev.org/osfaq2/index.php/InlineAssembly, I'd say:

Code: Select all

void foo( unsigned char x )
{
  asm ("mov ax, %0" : "=g"(x) );
}

Re:Inline ASM in C(GCC)

Posted: Mon Nov 29, 2004 4:52 am
by Pype.Clicker
hmm, now beware ...

"mov ax,<x>" in intel-syntax is "movw <x>,%ax" in AT&T

Why do you want to mov "ax" somewhere ? i guess you have some other ASM stuff that puts a result in AX and you want to retrieve that ...

By any chance, if the AX is the result of a function CALL, you need to know that C compilers will use (e)AX as the result of the function if it's atomic (integer, pointer, char, etc.)

In that case, if you have

Code: Select all

myAsmFct:
    ;; do stuff
    mov ax,<result>
    ret
then doing

Code: Select all

    myVar=myAsmFct();
is enough. No inline is required ...

HTH

Re:Inline ASM in C(GCC)

Posted: Tue Nov 30, 2004 2:37 am
by bubach
Pype.Clicker wrote:Why do you want to mov "ax" somewhere ? i guess you have some other ASM stuff that puts a result in AX and you want to retrieve that ...
what are you talking about?

Code: Select all

mov ax, x    ; moves location of variable x into ax
mov ax, [x]  ; moves contents of x into ax
[EDIT] forget it... :-[

Re:Inline ASM in C(GCC)

Posted: Tue Nov 30, 2004 3:04 am
by Pype.Clicker
well, actually my concern is that in the middle of a stream of C instructions, content of register have little meaning. Moreover, the optimizer is usually free to reorder things, etc. and the compiler might not be able to trace the dependency between instructions if you e.g. have

Code: Select all

    // do not do this, please.
    asm("cpuid");
    asm("mov %eax, %0","=g"(myVar));
If you have dependencies between some inline assembler instructions, you should put those instruction in a single asm("...") expression:

Code: Select all

    // do this instead
   asm("cpuid","=a"(myVar));

Re:Inline ASM in C(GCC)

Posted: Tue Nov 30, 2004 3:32 am
by Solar
Pype.Clicker wrote:
"mov ax,<x>" in intel-syntax is "movw <x>,%ax" in AT&T
:P ::)

There I am, the AT&T evangelist of the lord, and in a minute of not concentrating on what I type I screw up like that... :D

Re:Inline ASM in C(GCC)

Posted: Tue Nov 30, 2004 4:08 am
by distantvoices
praise the big gosh a'mighty - and St. Gnucius, just to be sure to get every important god outta there for the benefit of knowing at&t syntax by heart*rofl*

such things sometimes happen. Won't be funny wi'out 'em.