0% found this document useful (0 votes)
111 views11 pages

Pic C2assembly

The document provides several examples of PIC microcontroller assembly language code and their equivalent C code implementations. The examples include: 1) Moving data from registers to ports 2) Implementing a for loop to increment a variable and add it to another 3) Generating a delay using nested for loops with a dummy operation 4) Rotating bits of a port from left to left 5) Selecting a memory bank and incrementing a variable 6) Computing the absolute value of a variable 7) Finding the maximum of two variables

Uploaded by

ShahAyzdii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views11 pages

Pic C2assembly

The document provides several examples of PIC microcontroller assembly language code and their equivalent C code implementations. The examples include: 1) Moving data from registers to ports 2) Implementing a for loop to increment a variable and add it to another 3) Generating a delay using nested for loops with a dummy operation 4) Rotating bits of a port from left to left 5) Selecting a memory bank and incrementing a variable 6) Computing the absolute value of a variable 7) Finding the maximum of two variables

Uploaded by

ShahAyzdii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

MOVWF PORTB; // move Wreg to PORTB

MOVF _myReg2,0,0; // move myReg2 to Wreg

MOVWF PORTB;

GOTO LOOP_START;

pic microcontroller assembly language example 2

The equivalent of the following C code in assembly is given


below.Use mickroC debugger to verify your code.

unsigned short x=0;


unsigned short i=0;

for (i= 1; i< 10; i++}{

x = x+i;

unsigned short x=0;

9 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

unsigned short i=0;

void main() {

OSCCON.IRCF0=0;
OSCCON.IRCF1=1;
OSCCON.IRCF2=1;

ANSELA =2;
asm{

MOVLW 0X01 ;
Loop:
INCF _i,1;// i=i+1
MOVF _i,0;// move i to Wreg
ADDWF _x,1; //x=x+Wreg

MOVLW 9; // Wreg=10
CPFSGT _i;
goto Loop

10 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

pic microcontroller assembly language example 3

Write the equivalent of the following C code in assembly. This is


used to generate delay by performing no operation instructions.

unsigned short I,J,X;


for (I= 0; I<255; I++){

for (J= 1; J<255; J++){

X = X+J; // dummy instruction to generate a delay of (256*256), do


not worry about overflow

X=I; // Set a break point here

unsigned short I,J,X;


void main() {

OSCCON.IRCF0=0;

11 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

OSCCON.IRCF1=1;
OSCCON.IRCF2=1;
ANSELB=0;
TRISB=0;
asm {

MOVLW 0xF0;
MOVWF LATB,0;
Rotate:
RLNCF LATB,1,0;
goto Outer

Inner:
INCF _J,1;// J=J+1
MOVF _J,0;// move J to Wreg
ADDWF _X,1; //X=X+Wreg

MOVLW 255; // Wreg=10

12 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

CPFSEQ _J;
goto Inner
goto Outer
Outer:
INCF _I,1;// i=i+1
MOVLW 255;
CPFSEQ _I;
goto Inner
goto Rotate

pic microcontroller assembly language examples 4

This assembly language program rotates the bits of LATB to the


left

unsigned short I,X,u,J;

13 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

Following code will select the memory bank and increment the
value of i variable.

unsigned short i absolute 0x400;


void main()
{
asm{
CLRF _i,BANKED;
MOVLB 4;
LOOP_START:
INCF _i,1,BANKED;
GOTO LOOP_START;

} }

pic microcontroller assembly language examples 6

Write the Equivalent Assembly code for the following C snippet.

short var1 = 63 //Assign any negative value


short var2 = 0;

var2 = abs(var1); //The var2 must get the absolute value of var1

15 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

Equivalent assembly code

short var1=-10;
short var2=0;

void main() {
asm{
MOVLW 0;
MOVFF _var1,_var2;
ADDWF _var1,0;
BNN end
NEGF _var2

end:
goto end
}

16 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

pic microcontroller assembly language examples 7

Write Assembly Equivalent of the following C function.

//Global Variables
short num1, num2, maximum;

short max_of_2(){

if(num1>num2){

maximum = num1;

}else{

maximum = num2;

Equivalent assembly code:

short num1=10;
short num2=12;
short maximum=0;

17 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

void main() {

asm{
//call max_of_2;

max_of_2:
MOVFF _num1, _maximum;
MOVF _num1,0;
CPFSGT _num2
return
MOVFF _num2,_maximum
return

pic microcontroller assembly language examples 8

18 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

Write Assembly Equivalent of the following C function. This code


finds the maximum value of the array

short array_element = 0;
short i =0;

const short size_array = 8;

data short My_array[size_array] = {2, 4, 5, 12, 56, 13, 1, 1}


absolute 0x0060;

void main() {

for(i=0;i<size_array;i++){

array_element=My_array[i];

while(1);

Equivalent assembly code

short array_element = 0;
short i =0;
const short size_array = 8;

19 of 21 12/12/17, 1:09 PM
pic microcontroller assembly language programming examples about:reader?url=http://microcontrollerslab.com/pic-microcontroller-asse...

data short My_array[size_array] = {2, 4, 5, 12,


56, 13, 1, 1} absolute 0x0060;

void main() {
My_array[1]=2;
asm{

LFSR 1, 0x0060;
for:
INCF _i,1;
MOVFF POSTINC1,_array_element;
MOVLW _size_array;
CPFSEQ _i
goto for

end:
goto end;

20 of 21 12/12/17, 1:09 PM

You might also like