delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/16/22:45:47

Sender: nate AT cartsys DOT com
Message-ID: <3587261E.A337FF87@cartsys.com>
Date: Tue, 16 Jun 1998 19:12:46 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
MIME-Version: 1.0
To: oonly AT mailexcite DOT com
CC: djgpp AT delorie DOT com
Subject: Re: beginner inline assembly problem
References: <3585D17E DOT 4F15 AT mailexcite DOT com>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019