This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Interrupt Keil Style Vector Table

Hi,

MCBSTR7 User's Guide suggests below code to assign interrupt vector addresses. I wonder the second line. It makes first cast to u16 and then shift by 16 bits. I checked the code in debug, the compiler generates 32 bits shift even it requests u16 bit. Should it be casted to u32 instead of u16?

// Configure and enable IRQ for Timer 3
  EIC->IVR = (u32)T3TIMI_IRQHandler;                // setup IRQ vector
  EIC->SIR[T3TIMI_IRQChannel] = ((u16)T3TIMI_IRQHandler << 16);

MCBSTR7 User's Guide:
www.keil.com/.../mcbstr7_ex_keil_locate.htm

10x.

  • The cast to u16 chops off the upper 16 bits. The shift is done on an int thanks to integer promotion rules.
    Using a cast to u32 instead would also work since the upper 16 bits would be thrown away during the shift anyway. It would also be slightly more intuitive, I think.

  • The cast to u16 chops off the upper 16 bits.

    Which is superfluous, since those upper 16 bits will be shifted into oblivion anyway. So that cast achieves nothing worth having.

    The shift is done on an int thanks to integer promotion rules.

    And that is a potentially serious bug. 'int' is signed, and you pretty much never want to shift signed values. Evaluating

    (int)0x8000 << 16
    

    causes undefined behaviour on a 32-bit platform like this.