简体   繁体   中英

Why odd operand error when compiling assembly?

Learning assembly and reading about the BIT instruction on msp430. When trying to compile this code:

int main (void)
{
  while(1){
    __asm__("BIT R2, 3");
  }

   return 0;
}

It says: error: odd operand: -3

Yet when writing __asm__("BIT.B R2, 3"); instead, it works.

Could somebody explain this please?

The instruction BIT R2, 3 is using symbolic mode for the destination address (ie an offset from the program counter). You must use BIT R2, #3 if you want to use the immediate value 3 .

The reason this fails with BIT and not with BIT.B is because BIT does a word operation and you are using an odd address which is illegal. Word operations must be word aligned (ie even addresses) in the MSP430. Byte operations can operate on any byte address, odd or even.

You can get quite detailed information if you read the User Guide for the family of MCU you are using. For example, for the MSP430x2xxx family you would read the https://www.ti.com/lit/ug/slau144j/slau144j.pdf document, Chapter 3 or 4 depending on whether your MCU has the newer 20-bit address core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM