3

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 2
    First of all, that's not a safe way to use inline asm. Modifying general-purpose registers without telling the compiler about it will cause problems. 2nd, did you check the manual for the `BIT` instruction? Presumably it's not encodeable with an odd immediate. That seems to be what the assembler error message is telling you. – Peter Cordes Jul 17 '20 at 18:52
  • @PeterCordes yes I did read documentation about the ISA, but it is still not clear to me why this does not compile. I read [this doc from TI (page 5-6)](https://www.ti.com/sc/docs/products/micro/msp430/userguid/as_5.pdf) and [this page about the x86 equivalent](https://en.wikipedia.org/wiki/Bit_Test). OK this is not encodable... but *why*? I don't see what prohibits this. Finally what precisely should I tell the compiler to be "safe"? –  Jul 17 '20 at 18:57
  • For safety you need https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html with input / output constraints to connect your asm to C variables. – Peter Cordes Jul 17 '20 at 18:58
  • x86 is irrelevant here, and you're looking at the wrong part of the msp430 architecture manual. You found a high-level summary of the instruction set, but what you need is the detailed description of each individual instruction, which will say exactly what immediate operands are accepted by `BIT` and `BIT.B`. – zwol Jul 17 '20 at 19:00
  • 4
    x86 instructions are totally irrelevant to what MSP430 can do. Note that MSP430 uses `op src, dst`, so I think `bit r2, 3` is a memory-destination instruction with an odd word address. Does MSP430 require aligned memory operands? That would explain why it allows the address `3` for the single-byte version but not the word version. – Peter Cordes Jul 17 '20 at 19:01
  • @zwol got a link to the precise document I should read? Thanks –  Jul 17 '20 at 19:02
  • 3
    I _think_ you want https://www.ti.com/sc/docs/products/micro/msp430/userguid/ag_05.pdf instead of .../as_05.pdf. I'm looking at it now. I've already discovered that R2 and R3 have special properties; the issue here _may_ be the combination of R2 and that immediate operand, not BIT with that immediate operand. – zwol Jul 17 '20 at 19:06
  • AFAICT, MSP430 `bit` (bit test) is equivalent to x86 `test reg, reg/mem` (or with an immediate if you use immediate syntax). i.e. bitwise AND and set flags, discarding the result. https://phas.ubc.ca/~michal/phys319/MSP430Reference-RyansEdit.pdf. Other references I found indicated that a bare numeric address should be `&3`, and an immediate should be `#3`, but maybe GAS's version of MSP430 syntax is different if it accepts bare `3` at all. – Peter Cordes Jul 18 '20 at 00:29

1 Answers1

1

The instruction BIT R2, 3 is using symbolic mode for the destination address (i.e. 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 (i.e. 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.

Parakleta
  • 1,121
  • 10
  • 19