Inline ASM in C(GCC)

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
Vladaz

Inline ASM in C(GCC)

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

Re:Inline ASM in C(GCC)

Post 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) );
}
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:Inline ASM in C(GCC)

Post 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
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Inline ASM in C(GCC)

Post 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... :-[
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
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:Inline ASM in C(GCC)

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

Re:Inline ASM in C(GCC)

Post 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
Every good solution is obvious once you've found it.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Inline ASM in C(GCC)

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Post Reply