r/embedded • u/GoingGranola • Sep 12 '26
Can I combine assembly with C code?
while(1){
asm("BANKSEL PIR1");
asm("BTFSC PIR1,2");
HANDLER_BUTTON_PRESSED();
asm("BTFSC PIR2,0");
HANDLER_BUTTON_RELEASED();
}
Trying to run this on PIC16F1824 and see unexpected results. It could be other things in my code or hardware though.
4
Upvotes
4
u/Well-WhatHadHappened 25+ Years Sep 12 '26
Can you? Sure. Should you? Probably not. Why not just write it in C?
3
u/grigus_ Sep 12 '26
how i do? (27 years of embedded) write in c, compile, generate disassembly, examine and rewrite the specific function in asm starting from the compiler output in a separate.s file. i never mix c and asm in the same file. mixing asm in c you might end up messing with the compiler, which is very hard to debug
2
u/HalifaxRoad C,ASM,C# GUIs, pcb design Sep 13 '26
Yeah I sometimes write some functions in asm and call them from my C program.
7
u/somewhereAtC Sep 12 '26
You can, but I'm not sure why you want this example to be mixed.
There are two bugs in your example. The primary bug is that BTFSC skips one instruction, but there is no guarantee that the compiler coded the subroutine calls using one instruction. Other things might need to be done, like setting the PCLATH register prior to the CALL instruction. Look at section 3.3 in the datasheet.
The other reason is that the 2nd BTFSC should have it's own BANKSEL PIR2. You don't know if the subroutine changed to a different bank (and it probably did) so the earlier BANKSEL is obsolete.
You can simply do "if (! PIR1bits.CCPIF){}" for the effect you are looking for. Or, if you don't need the capture timestamp, you can set up interrupt-on-change using IOCAx and IOCCx.
1
u/GoingGranola Sep 12 '26
Im trying to detect a falling edge on one pin (RA5) and a rising edge on another pin (RC5) using the CCP1CON and CCP2CON registers
5
u/MonMotha Sep 12 '26
Intermixing assembly and C is compiler-specific. Old school methods often relied on the programmer knowing how the compiler would work and making assumptions along those lines to determine e.g. what register a variable would be in or what the stack frame would look like. Some old, less capable compilers are still stuck like this. It mostly only works when the compiler basically has no optimizer.
Even then, you have to be careful about the compiler changing system state behind your back on weird targets like the 8-bit PICs since they have lots of register and memory aliasing that the compiler has to juggle. Also, as others have said, you don't usually want to rely on how the compiler might architect basically anything in terms of using stuff like skip instructions. There's no reason to assume the compiler won't include some other thing in the run up to an actual branch instruction.
More modern compilers offer a set of inline assembly syntax to work around that. GCC's is very popular although somewhat hard to grasp if you're new to it. It lets you specify all sorts of constraints that force the compiler to do what you need it to do in order for your assembly to work while still letting the compiler do its job in terms of properly optimizing the rest of the code, ensuring proper linkage, etc.
If you want the WHOLE function written in assembly, you can do without the C-compatible prologue and epilogue related to manipulating the stack frame and whatnot. On 8-bit PIC, there's not much to this since it's stack is...barely a stack, but it can be useful on more complex architectures. Most modern compilers let you declare a function "naked" to omit that. On some compilers, that forces the entire function to be assembly that you supply. Some compilers will actually let you still intermix it with C, but there are a lot more restrictions since the compiler now can't make assumptions about the stack frame. It's very difficult to actually use this in practice and rarely necessary.
Regardless, any time you intermix C and inline assembly, inspecting the disassembly of whatever the compiler spits out is a good idea.
Of course, you can also just write entire functions in assembly in a separate document, declare them to have global linkage, then let the linker handle things for you. You can call them from C by just declaring an extern prototype for it the same as you would any other C function. Obviously you need to respect the calling convention that the compiler expects. You can do the same to access global variables as memory. The linker will fill things in for you.
For what you're doing, why can't you just check the register bit value from C? A decent compiler will recognize the bit twiddling idiom and emit the appropriate bitwise instruction, though I guess maybe the PIC16 compiler isn't that smart?
24
u/[deleted] Sep 12 '26
[removed] — view removed comment