Programming PIC in both assembly and C

Thread Starter

wannaBinventor

Joined Apr 8, 2010
180
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:
Rich (BB code):
#asm

SOME
ASSEMBLY
CODE 
HERE

#endasm

  A = B / C

#asm

MOVF    A,PORTB
MORE
ASSEMBLY
HERE

END

#endasm
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.
 

BMorse

Joined Sep 26, 2009
2,675
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
 

Thread Starter

wannaBinventor

Joined Apr 8, 2010
180
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.
 

John P

Joined Oct 14, 2008
2,026
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".
 

Thread Starter

wannaBinventor

Joined Apr 8, 2010
180
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".
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!
 

Vaughanabe13

Joined May 4, 2009
102
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.
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.
 

Thread Starter

wannaBinventor

Joined Apr 8, 2010
180
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.
 
Top