# Mixing Assembly and C [closed]

Is it possible to use a mix of these 2?

Using a pic4550 and mplabc18

Lets say i have a microcontroller, that in my port B (B0 B1 B2 B3 B4 B5 B6 B7) 1 Byte, I set it to a '00000001'

Is it possible to assign that value to an int value of C and then do logic operations in C, like an XOR?

Lets say my final result by XORing the Port B Data with my Port A data, ends up being, Remanent = '10000000' , now i want to output that value in ports C and E in my microcontrtoller using a mix of C and assembly

  void main(void)
{
TRISA = 1;   //Pto A as input (A0 A1 A2 A3) Polinomio
TRISB = 1;   //Pto B as input (B0 B1 B2 B3 B4 B5 B6 B7) Byte
TRISC = 0    //Pto C as output  (C4 C5 C6 C7) Outpout
TRISE = 0;   //Pto E as output  (D0 D1 D2 D3 D4 D5 D6 D7) Output

while(1){
//Inicio del codigo
PORTA = int Polinomio;
PORTB = int Byte;}
}


if something like this is possible what is the right way to code using a mix of assembly and C in the same code?

## closed as too broad by Leon Heller, Matt Young, tcrosley, Nick Alexeev♦May 23 '15 at 21:53

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Depends on the device and the compiler. – Leon Heller May 23 '15 at 16:33
• Why would you need to mix C and assembly to do an XOR on a port and move the result to a couple of different registers? – Matt Young May 23 '15 at 16:34
• the idea is to obtain data from a microcontroller and do the process in C with the idea in mind of not having to deal with the assembly commands, and the datasheet – GoatZero May 23 '15 at 16:35
• This question is a mess. There is no such thing as a PIC4550 (I assume you mean PIC18F4550). Plus the code example has several syntax errors. – tcrosley May 23 '15 at 19:35

You usually don't need to deal with assembly commands directly- there will be compiler- and device-specific macros predefined to access hardware registers such as PORT and TRIS (on a PIC), but ignoring the hardware datasheet entirely is a recipe for trouble. For example, you will quickly discover that pins have multiple functions and the default may not be what you need.

An example from a header file: PORTA equ 0005h

As you may know, "PORTA" is not a part of standard C, but the above equ (a pre-processor directive similar to a #define in C) in a header file allows you to use the label PORTA without worrying about the exact hex address it refers to.

You will also find C macros such as this one

volatile bit RA0 @ ((unsigned)&PORTA*8)+0;

...that allows you to use C assign statements on individual bits of (in this case) PORTA, for example:

RA0 = 1;

You should study your compiler manual, examples, and header files to figure out how to do this on your compiler.

Dipping into assembly language (either inline or linking assembly language routines) is most often unnecessary with modern processors, even in the most critical portions such as interrupt service routines. You can almost always do it, but you shouldn't unnecessarily.

If you're dealing with individual bits it's better to use unsigned variables in C.

Yes, you can access memory-mapped registers and do XOR and other bitwise operations in C. Most microcontrollers have C compilers. You might find this list of C operators helpful. Here are the bitwise operators

AND: &

OR: &

XOR: ^

NOT: ~

I can't teach you the entire C language here, but there are lots of tutorials on the internet.