Mail Archives: djgpp/1998/06/16/22:45:47
Bob Berkawitz wrote:
>
> When I try to compile this I get the this error "13:Error: operands
> given don't match any known 386 instruction"
> Here is the code:
> int x;
> void main()
> {
> asm("movw 5, %ax\n\t"
> "movw %ax, $_x");
> }
> What am I doing wrong?
Think about AT&T asm for a minute. That is equivalent to the following
in Intel asm:
mov ax, [5]
mov offset _x, ax
First you try to load AX with the word at memory address 5. Bad idea,
and not what you want, but valid asm. Then you try to move the contents
of AX into the *address* of _x, which is an immediate value! That, as
you might guess, won't work.
There are several other problems.
* In DJGPP, an `int' is a doubleword-- 32 bits.
* `main' must return an `int', according to ANSI.
* You don't inform the compiler that you clobber the AX register.
If what you want to do is set a 16-bit variable to 5, try this:
short x;
int main(void)
{
asm volatile("movw $5, %%ax \n\t"
"movw %%ax, _x" : : "ax");
return 0;
}
Note also that the paradigm of accessing variables directly by their
name is non-portable and may break in future versions. Better yet (and
also shorter by one instruction) is:
asm volatile("movw $5, %0" : "=g" (x));
For more info, there's a great tutorial at:
http://www.rt66.com/~brennan/djgpp/djgpp_asm.html
--
Nate Eldredge
nate AT cartsys DOT com
- Raw text -