This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to refer to C bit-field lable of structure members in assembly code?

Hi, all

My preference is to program in mixed assembly and C environments, and share C headers containing declarations

between the C and assembly code. but, how to refer to C bit-field lable of structure members in assembly code?

I 'm puzzled by this question for a long time.

//for example
//in C Header file ctest.h
typedef struct CRITICAL_FAULT_FLAGS {  // bits   description
     int Err0:1;         // BIT0      
     int Err1:1;         // BIT1     
     int Err2:1;         // BIT2     
     int Err3:1;         // BIT3       
     int Err4:1;         // BIT4     
     int Err5:1;         // BIT5     
     int Err6:1;         // BIT6       
     int Err7:1;         // BIT7     
     int Err8:1;         // BIT8     
     int Err9:1;         // BIT9       
     int Err10:1;        // BIT10    
     int Err11:1;        // BIT11          
     int Err12:1;        // BIT12    
     int Err13:1;        // BIT13    
     int Err14:1;        // BIT14    
     int Err15:1;        // BIT15    
} CRITICAL_FAULTS;

//in C code sorce file ctest.c:

#include "ctest.h"
  CRITICAL_FAULTS    strCriticalFaults;
  strCriticalFaults.Err0 = 1;
  strCriticalFaults.Err5 = 1;

//Then I reference the C constructs in assembly code as below in asmTest.asm file
 .cdecls C,LIST,WARN,"ctest.h"
 .ref _strCriticalFaults

 MOVW DP, #_strCriticalFaults
 MOV AL, @_strCriticalFaults.Err5  ;here , I need mask off garbage bits
;//but how to mask off garbage bits, and access the  BIT5 using the C Bit-field lable Err5
;// by the way,when use #_Err5 or #_Err0 in asmTest.asm, Compilation failure and occur
;// "The following symbols are undefined", beacuse A label for a structure member cannot  be declared global

   thanks for any help

  • Please see this related thread which discusses a similar topic. Hope that helps.

  • Hello AartiG,

     thanks for your response. I see the thread you provided, it is about the label offset from the beginning of the structure.

     but my question is, how can I access bit-fields using C bit-field instead of hard-coded bit-masking constants.

     the label for a member of the structure is absolute and equates to the present offset from the beginning of the structure.

    as for my example, the offset of all structure members is 0x0, I need bit-masking operation to mask off garbage bits.

    how can I form the bit-mask from C label in the assembly statements?

    is there any better way to design it using existing C label which would get resolved at assembly time?

    Best regards

  • The value captured in an assembler constant expression like CRITICAL_FAULT_FLAGS.Err5 is the offset of the word which contains that bit.  There is no method for symbolically representing the bit offset within that word.  Sorry.

    Thanks and regards,

    -George