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.
Inline ASM in C(GCC)
Re:Inline ASM in C(GCC)
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.
- Pype.Clicker
- 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)
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
then doing
is enough. No inline is required ...
HTH
"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
Code: Select all
myVar=myAsmFct();
HTH
Re:Inline ASM in C(GCC)
what are you talking about?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 ...
Code: Select all
mov ax, x ; moves location of variable x into ax
mov ax, [x] ; moves contents of x into ax
- Pype.Clicker
- 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)
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
If you have dependencies between some inline assembler instructions, you should put those instruction in a single asm("...") expression:
Code: Select all
// do not do this, please.
asm("cpuid");
asm("mov %eax, %0","=g"(myVar));
Code: Select all
// do this instead
asm("cpuid","=a"(myVar));
Re:Inline ASM in C(GCC)
::)Pype.Clicker wrote:
"mov ax,<x>" in intel-syntax is "movw <x>,%ax" in AT&T
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...
Every good solution is obvious once you've found it.
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Inline ASM in C(GCC)
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.
such things sometimes happen. Won't be funny wi'out 'em.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image