Programming PIC in both assembly and C

Discussion in 'Embedded Systems and Microcontrollers' started by wannaBinventor, Jun 28, 2010.

  1. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    I'm considering using the HiTech C Compiler Lite to perform logical operations and math within my code. It just doesn't seem to make sense to me to write 8 or 9 lines of code in assembly for something that can be coded in C with 1 line.

    I envision it going something like this:

    Opening a file and beginning with:
    Code ( (Unknown Language)):
    1. #asm
    2.  
    3. SOME
    4. ASSEMBLY
    5. CODE
    6. HERE
    7.  
    8. #endasm
    9.  
    10.   A = B / C
    11.  
    12. #asm
    13.  
    14. MOVF    A,PORTB
    15. MORE
    16. ASSEMBLY
    17. HERE
    18.  
    19. END
    20.  
    21. #endasm
    22.  
    Does anyone anticipate a problem with this - considering I will be writing almost exclusively in assembly in a C compiler.

    Can I do all of the configuration and equating within the " #asm - #endasm " tags and still expect it to properly compile, program into the PIC, and operate?

    Will the variables work if I've equated a say, USERFILE1, in the assembly tags and then I reference it as an operand in the C code?

    Thanks for the input.
     
  2. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    The only issue you might have using C instead of ASM is the end file size might be bigger because the C code might rely on libraries.
     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    why use C to write ASM? Why not use MPASM that comes with MPLAB?
    I have seen people use asm code in c but never seen anyone write a complete assembly application using a c compiler....

    B. Morse
     
  4. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    BMorse:

    The idea is that I'm fairly comfortable doing some programming in assembly. All the projects I've done so far have been in assembly, and I don't really know C, so I want to continue to use assembly for the bulk of the program. What I want to do is combined my existing knowledge (however limited) of assembly with the ease of doing mathematical operations in C.

    If I want to divide by 9 in C I think it's something like:
    QUOTIENT = NUMERATOR / DENOMINATOR (or whatever you call the variables)

    In assembly... I really don't know how yet but from looking at examples it is SEVERAL lines of code.

    If you can break into C code in the middle of MPASM I'd probably just do that, but I'm not sure that you can.
     
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    The reason people use a compiler is to allow them to write code faster (for the programmer!) that's at least somewhat portable between systems and which is comprehensible to human readers. It's generally accepted that compiled code won't be as efficient in terms of program size and execution speed as hand-written assembly code, but if the compiled program will still do the job, there's no reason not to use it.

    Having said that, I usually skim through the list file for my programs and look for any lines of C code that are generating vast amounts of assembly. Sometimes slightly rearranging the source code helps the compiler be more efficient, and once in a while, if it seems obvious, I'll put in a few lines of assembly code.

    But generally, using a compiler and not letting it do its job is like "Buying a dog and then barking yourself".
     
  6. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    Thanks for the advice.

    I don't understand how I wouldn't be letting it do its job? I would write most of the code myself -- hence, more efficient, and then switch to C for more complex operations in assembly (like division) -- hence, easier to me.

    I feel like maybe I haven't properly articulated my intentions. All in all, I just wanted to know if it will work because I think doing it would be beneficial to me.

    My main concern is if the compiler will properly handle configuration and equates decleration in assembly and then calling those variables (declared in assembly equates section) in C math operations.

    Thanks for the help guys!
     
  7. Vaughanabe13

    Active Member

    May 4, 2009
    102
    5
    Honestly, I would advise you to start learning some C and just switch over to C entirely. You can do everything in C that you can in assembly (especially since you can use inline assembly instructions) and C is much easier to read and easier to program. If you have assembly knowledge I have confidence that you can learn C, as it is much easier.

    That being said, YES, you should theoretically be able to write an assembly program and use the C compiler for math instructions. But in practice, it won't be this easy, and you will probably have setup and compile issues. Generally if you use something in a way that it wasn't intended to be used, you will have problems. Also, code that is interpreted by a C compiler will almost always be more bloated and less efficient, because there are more intermediate steps while translating to machine code. Just because writing "c = a*b" is easier to understand does not mean it is more efficient.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  9. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Yes. It will work.
     
  10. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    I just tried a little sample program
    Got this when I tried to build it. Maybe I just need to look into actually learning the math in ASM.
    Error [939] ; . no file arguments.
     
  11. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
  12. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    Thanks for the help guys!
     
Loading...