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

Combining C and assembly codes into one header file.

Hi!

At school we are going to have the final test in programming. Most of the class are really good at programming at C and Assembly therefore teachers allowed us to create universal header file with some functions for peripheries we are going to use. We have created header files for almost everything we need. There is just one little question. In one of the files we use C and Asm code we are using separate *.A51 file. Is there a way to put it into this header file? (We are creating one universal file with preprocessor directives and we want to stick to just this one file).

SO again questions:

1. Can we create something like this:

type FunctionInC(type foo)
{
   #pragma asm
     JMP $  ;
   #pragma endasm
}


(Because we are reading lots of articles against it.)

2. Is there any other way to put it together? (Everything else is in C but 3 functions need "direct supervision")

Please excuse my English (I am still not on level I want to be ;))

Parents
  • 1. Can we create something like this:

    type FunctionInC(type foo)
    {
       #pragma asm
         JMP $  ;
       #pragma endasm
    }
    

    You can, but you really shouldn't. It'll cause you a good deal more problems than it's likely to solve.

    But then again, since all you're talking about is header files, you shouldn't be writing anything like that at all because that's a function definition, and that absolutely does not belong into a header file. Well, either that or you've invented your own meaning of the term "header file", which maybe you should share with us before going on.

Reply
  • 1. Can we create something like this:

    type FunctionInC(type foo)
    {
       #pragma asm
         JMP $  ;
       #pragma endasm
    }
    

    You can, but you really shouldn't. It'll cause you a good deal more problems than it's likely to solve.

    But then again, since all you're talking about is header files, you shouldn't be writing anything like that at all because that's a function definition, and that absolutely does not belong into a header file. Well, either that or you've invented your own meaning of the term "header file", which maybe you should share with us before going on.

Children
  • I know that I should create separate *.h and *.c file corresponding to it, but I can just as well write the whole function down into h file. I used it for several larger projects. We are creating multipurpose file. It contains: ADC, DAC, timers (functions for stopwatch), interrupts (special buttons), thermometer, keyboard, lcd, watchdog + alarm, motion sensors.

    There will be 8 tasks and we are going to pick up one off them (or none there are task like built pc, labview etc.). So we do not know witch of the function we are going to use we have something like this:

    Main.c:

    #define LCD     1
    #define KBD     0
    #define ADC     0
    #define DAC     0
    #define SER     0
    #define WDT     0
    #define TMP     0
    #define TIM     0
    #define INT     0
    
    #include "../libraries/universal.h"
    
    void main(void)
    {
            lcd_init();
            lcd_cmd(0x80);
            lcd_string("Hello!");
            for(;;);
    }
    

    Universal.h:

    ...
    #if LCD == 1
     e.g.:
    /**********************************************************/
    /* Function Name:   LCD_STRING();                         */
    /* Entered From:    ADC_LCD() or STANDALONE               */
    /* Calls:           DELAY(),LCD_CHAR()                    */
    /**********************************************************/
    /**********************************************************/
    /* Purpose: send bunch of chars given as string           */
    /*                                                        */
    /**********************************************************/
    
    void lcd_string(char* str)
            {
                    unsigned int index = 0;
                    while (str[index] != 0)
                    {
                            lcd_char(str[index]);
                            index++;
                    }
            }
    ...
    #endif
    ...
    

  • Code in c files.

    Just decide which c file to link into project - or link in everyone and let linker strip out unused.


  • So just do it, then!

    Separate "modules" is a far better structure than one huge, monolithic blob of a "universal" file!!

    And, again, you should not be putting object definitions in any header file - universal or otherwise.

    The object definitions belong in .c files - you should have only 'extern' declarations in headers.

    c-faq.com/.../decldef.html

  • but I can just as well write the whole function down into h file.

    No, you can't. If you still think you can, that's because you haven't built anything but tiny, single-file micro-programs yet.

    I used it for several larger projects.

    I'm quite sure you have no idea what a "larger project" actually is.

    Actually your explanation reveals that your real problem is the other one I mentioned: you are using the term "header file" in a totall different meaning than its usual one. A better name for what you're actually making there would be "UPOS" --- for "unstructured pile of stuff".

    You're spending energy on fighting your tools. Don't. You'll eventually lose anyway.

  • Deciding what things belong together in a single module, and what things should be separated into distinct modules has to do with Cohesion:

    en.wikipedia.org/.../Cohesion_(computer_science)

    Simply dumping everything into one huge, amorphous UPOS is the worst kind - sometimes called Coincidental Cohesion:

    en.wikipedia.org/.../Cohesion_(computer_science)#Types_of_cohesion

    www.google.co.uk

    www.google.co.uk