You are on page 1of 19

Introduction to Instruction Set.

OBJECTIVES i) ii) iii) iv) v) To familiarise yourself with the instruction set. To learn how the instructions affect the register values, w and status bits. To learn where the results after the execution of instructions is stored. To be able to understand programs through the analysis of instructions. To be able to write short programs.

INSTRUCTION SET & ADDRESSING MODE We can classified the PIC16F877 into 5 different addressing modes. These addressing mode defines the manner in which an instruction will obtain the data for its execution. They are: inherent, immediate, direct, indirect and relative addressing modes.

INHERENT ADDRESSING MODE In inherent addressing mode, all information required for the operation is already known to the CPU. Some examples of inherent addressing mode is given in Table 1. Name Clear w register (Accumulator) No operation Return from subroutine Return from interrupt Go into standby mode Load Option register Mnemonic CLRW NOP RETURN RETFIE SLEEP OPTION Description Reset all bits in the w register to zero. Do nothing for one instruction cycle. Return from subroutine by loading the return address back into the program counter. Return from interrupt by loading the return address back into the program counter and enable the global interrupt flag. Reduce power consumption by putting the microcontroller to sleep. It will wake-up thru reset, watchdog timer or external interrupt. The w register content is sent to the option register to control prescaler ratio, trigger edge for the interrupt, Port B pull-up enable and real time counter/clock source. TABLE 1 IMMEDIATE ADDRESSING MODE
PIC Microcontroller & Appications Page1

In the immediate addressing mode, the data is built into the instruction word. The instruction will loads data from the program memory into the w register. The immediate data is an 8-bit number of possible values from H00 to HFF. Some examples of immediate addressing mode is given in Table 2. Name Load w register Return with literal in w register Add literal to w register Subtract w from literal Logical AND Logical OR Exclusive OR Mnemonic MOVLW D10 RETLW H0A ADDLW H25 SUBLW H25 ANDLW HAA IORLW D07 XORLW HFF Description Load w register with the decimal value 10 (or hexadecimal value 0A). Return from subroutine and load w register with the hexadecimal value 0A or decimal value 10. Add H25 to the contents of w register. Place the sum of the calculation in the w register. Subtract the contents of w register from the value H25 (H25 - w). Place the result in the w register. ANDs the contents of w register with HAA and store the result in the w register. ORs the contents of w register with D07 and store the result in the w register. XORs the contents of w register with HFF and store the result in the w register. TABLE 2 DIRECT ADDRESSING MODE In the direct addressing mode, the address of the data is specified following the instruction. Some examples of direct addressing mode are given in Table 3. Name Clear register Store w register Mnemonic CLRF H20 MOVWF H60 Description Clear the content at memory address H20 to zero. Store the contents of the w register into memory address H60. W register remains unchanged.

Name Load w register Load register

Mnemonic MOVF H50,W MOVF H50

Description Load the w register with the contents in memory address H50. The contents in memory address H50 is loaded
Page2

PIC Microcontroller & Appications

back into itself. Purpose is to test the Zero flag. Complement register Complement register Increment register COMF COMF INCF HA0,W HA0 H51,W Complement the content at memory address HA0 and store the result into w register. Complement the content at memory address HA0 and store the result back into HA0. The contents at memory address H51 is incremented by one and the result is store into w register. The contents at memory address HB0 is incremented by one and the result is store back into HB0. The contents at memory address H32 is decremented by one and the result is store into w register. The contents at memory address H22 is decremented by one and the result is store back into H22. The contents of memory address H25 is added to the contents of w register. Place the sum of the calculation in the w register. The contents of memory address H55 is added to the contents of w register and the result is store back into H55. Substract the contents of w register from the contents of memory address H70 ( [H70] w) and place the result in the w register. Substract the contents of w register from the contents of memory address H65 ( [H65] w) and place the result back into H65.

Increment register

INCF

HB0

Decrement

DECF

H32,W

Decrement

DECF

H22

Add register

ADDWF H25,W

Add register

ADDWF H55

Substract register

SUBWF H70,W

Substract register

SUBWF H65

Name Logical AND with register Logical AND with register

Mnemonic ANDWF H0F,W

Description ANDs the contents of w register with contents of memory address H0F and store the result in the w register. ANDs the contents of w register with contents of memory address H10 and store the result back into
Page3

ANDWF H10

PIC Microcontroller & Appications

H10. Logical OR with register Logical OR with register Exclusive OR with register Exclusive OR with register Rotate Left IORWF H3F,W ORs the contents of w register with contents of memory address H3F and store the result in the w register. ORs the contents of w register with contents of memory address H30 and store the result back into H30. XORs the contents of w register with contents of memory address H3F and store the result in the w register. XORs the contents of w register with contents of memory address H30 and store the result back into H30. Rotates bits in memory address H2A one position to the left and through the carry bit. The result is store in the w register. Rotates bits in memory address H2A one position to the left and through the carry bit. The result is store back into H2A. Rotates bits in memory address H30 one position to the right and through the carry bit. The result is store in the w register. Rotates bits in memory address H30 one position to the right and through the carry bit. The result is store back into H30. Clear bit 3 of the contents in memory address H06 to 0. Set bit 7 of the contents in memory address H28 to 1. TABLE 3

IORWF H30

XORWF H3F,W

XORWF H30

RLF

H2A,W

Rotate Left

RLF

H2A

Rotate Right

RRF

H30,W

Rotate Right Clear bit Set bit

RRF BCF BSF

H30 H06,3 H28,7

PIC Microcontroller & Appications

Page4

THE MPLAB 2.1 Double click on the MPLAB icon to start up the system. You will see MPLABs desktop as in Figure 1

Figure 1: MPLAB Desktop 2.2 2.3 2.4 Create a new project(Lab2-1.pjt) and source file(Lab2-1.asm) under the c:\program files\mplab\mct directory by followings the procedures in Handon 1. Now type the program given in Appendix A1. Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding. Now run your program using the simulator by followings the procedures in Handon 1. 2.5 2.6 Invoke the watch windows and set the registers (from Special Function Registers) and memory location (from File Register) you want to monitor. Fill in the results into Table 4.

PIC Microcontroller & Appications

Page5

Program ORG H00 BEGIN NOP CLRW CLRF CLRF CLRF MOVLW MOVWF ADDLW MOVWF SUBLW ADDLW MOVWF ADDWF FINISH GOTO H30 H31 H32 H68 H30 H0F H31 H70 H0C H32 H30,W FINISH

pcl

Register w status

H30

Memory H31 H32

TABLE 4

PIC Microcontroller & Appications

Page6

PROGRAMMING EXERCISE 3.1 Write a simple program to add three numbers using immediate addressing mode and store the result at address H40. Let the three numbers be D35, H1C and B11010100. Simulate the program. (Note : D = decimal; H = Hexadecimal; B = Binary) A blank table is shown in Table 5 is for you to fill your program and results. Answer : Address Location H40 = Program ORG H00 Register w Memory H40

pcl

status

TABLE 5 3.2 Write a simple program to add three numbers using direct addressing mode and store the result at address H40. Let the three numbers be taken from the following memory locations:
PIC Microcontroller & Appications Page7

H60 = D35 H61 = H1C H62 = B11010100 (Hint: you can use the window ->modify to change the values in these memory locations) Simulate the program. (Note : D = decimal; H = Hexadecimal; B = Binary) A blank table is shown in Table 6 is for you to fill your program and results. Answer : Address Location H40 = Program ORG H00 Register w status Memory H40

pcl

TABLE 6 3.3 What did you observe from 3.1 and 3.2 programs.

PIC Microcontroller & Appications

Page8

LOGICAL OPERATIONS 4.1 4.2 4.3 Create a new project(Lab2-2.pjt) and source file(Lab2-2.asm) under the c:\program files\mplab\mct directory. Now type the program given in Appendix A2. Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding. Now run your program using the simulator. 4.4 4.5 Invoke the watch windows and set the registers and memory location you want to monitor. Fill in the results into Table 7. Register w status Memory H41 H42

Program ORG H00 BEGIN NOP MOVLW H01 MOVWF H40 MOVWF H41 MOVWF H42 BCF STATUS,C RRF RRF RLF RLF IORWF ANDLW COMF FINISH GOTO H40 H40 H41 H41,W H42 HF0 H40 FINISH

pcl

H40

TABLE 7
PIC Microcontroller & Appications Page9

PROGRAMMING EXERCISE 5.1 Write a simple program to convert the contents at address H5F into ASCII values and store the result at address H60. Let the contents in address H5F be: H5F = H05 Simulate the program. A blank table is shown in Table 8 is for you to fill your program and results. Answer : Address Location H50 = Program ORG H00 Register w status Memory H60

pcl

TABLE 8

PIC Microcontroller & Appications

Page10

INDIRECT ADDRESSING MODE In indirect addressing mode, the full 8-bit register address is first written into the special-purpose register(FSR) that serves as an address pointer to any address throughout the entire register file. A subsequent direct access of INDF will actually access the register file using the content of FSR as a pointer to the desired location of the operand. Hence, every instruction that employ the direct addressing mode can alternatively employ the indirect addressing mode. One example of data transferring using indirect addressing is shown below. MOVLW MOVWF MOVF : : RELATIVE ADDRESSING MODE Relative addressing for the PIC16F877 involves altering the contents of the program counter. This constitutes a computed jump from wherever the program counter is to some address relative to that point. Some examples of immediate addressing mode is given in Table 9. Name Goto address Call subroutine Bit test, skip if clear Bit test, skip if set Decrement register by one, skip if zero Decrement register by one, skip if zero Mnemonic GOTO LOOP CALL DELAY Description Go to memory address specified by label LOOP. Call the subroutine which starting address is specified by label DELAY. Test bit 3 of the contents in memory address H06. Skips the next instruction if bit 3 tested to be 0. Test bit 7 of the contents in memory address H28. Skips the next instruction if bit 7 tested to be 1. Decrement the contents in memory address H48 by one and save the result in w register. If result is zero, skips the next instruction. Decrement the contents in memory address H50 by one and save the result into H50. If result is zero, skips the next instruction. H31 FSR INDF,W ; initialize pointer ; store the pointer in FSR ; load w register with data pointed by FSR

BTFSC H06,3 BTFSS H28,7 DECFSZ H48,W

DECFSZ H50

Name

Mnemonic

Description
Page11

PIC Microcontroller & Appications

Increment register by one, skip if zero Increment register by one, skip if zero

INCFSZ H48,W

Increment the contents in memory address H48 by one and save the result in w register. If result is zero, skips the next instruction. Increment the contents in memory address H50 by one and save the result into H50. If result is zero, skips the next instruction. TABLE 9

INCFSZ H50

BLOCK TRANSFER(USING INDIRECT ADDRESSING MODE) 6.1 6.2 6.3 Create a new project(Lab2-3.pjt) and source file(Lab2-3.asm) under the c:\program files\mplab\mct directory. Now type the program given in Appendix A3. Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding. 6.4 6.5 6.6 Now run your program using the simulator. Invoke the watch windows and set the registers and memory location you want to monitor. Fill in the results into Table 10.
Program ORG NOP MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF MOVLW AGAIN MOVWF MOVWF MOVF MOVWF MOVF A'P' H'50' A'I' H'51' A'C' H'52' H'03' COUNT H'60' TEMP2 H'50' FSR TEMP INDF,W TEMP1 TEMP2,W Page12 H'00' Register pcl w 60 61 62 Memory TEMP TEMP1 TEMP2 COUNT

PIC Microcontroller & Appications

MOVWF MOVF MOVWF INCF INCF MOVF DECFSZ GOTO AGAIN MOVWF MOVWF MOVF MOVWF MOVF MOVWF MOVF MOVWF INCF INCF MOVF DECFSZ GOTO AGAIN MOVWF MOVWF MOVF MOVWF MOVF MOVWF MOVF MOVWF INCF INCF MOVF DECFSZ FINISH GOTO

FSR TEMP1,W INDF TEMP TEMP2 TEMP,W COUNT AGAIN FSR TEMP INDF,W TEMP1 TEMP2,W FSR TEMP1,W INDF TEMP TEMP2 TEMP,W COUNT AGAIN FSR TEMP INDF,W TEMP1 TEMP2,W FSR TEMP1,W INDF TEMP TEMP2 TEMP,W COUNT FINISH

TABLE10

PIC Microcontroller & Appications

Page13

NESTED LOOP(FOR SOFTWARE DELAY) 7.1 7.2 7.3 Create a new project(Lab2-4.pjt) and source file(Lab2-4.asm) under the c:\program files\mplab\mct directory. Now type the program given in Appendix A3. Save and assemble your program and make sure that there are no assembly errors (typo errors). If there are any errors eliminate them before proceeding. 7.4 Now run your program using the simulator. 7.5 7.6 Invoke the watch windows and set the registers and memory location you want to monitor. Fill in the results into Table 11.
Program ORG BEGIN DELAY LOOP LOOP1 NOP MOVLW MOVWF MOVLW MOVWF NOP NOP DECFSZ GOTO LOOP1 NOP NOP DECFSZ DECFSZ GOTO LOOP LOOP1 MOVLW MOVWF NOP NOP DECFSZ GOTO LOOP1 NOP NOP DECFSZ DECFSZ FINISH GOTO INNER OUTER FINISH Page14 INNER LOOP1 INNER OUTER LOOP D2 INNER INNER LOOP1 D'2' OUTER D2 INNER H'00' pcl Register w Z Flag Memory INNER OUTER Cycles

PIC Microcontroller & Appications

TABLE11 7.7 What is the total execution time for the above program?

7.8

What is the total execution time if you change the INNER counter from D2 to D200 for the above program?

PIC Microcontroller & Appications

Page15

Appendix A1 : Listing of Lab2-1.asm Program

;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab2-1.asm * ; Date: 18/3/2000 * ; File Version: 1 * ; Author: * ; Company: Temasek Poly * ; Description: Addressing mode * ; * ;********************************************************************** list p=16f877 #include <p16f877.inc> ; define processor ; processor specific variable

;********************************************************************** ORG H00 ; processor reset vector BEGIN NOP ; no operation CLRW CLRF CLRF CLRF MOVLW MOVWF ADDLW MOVWF SUBLW ADDLW MOVWF ADDWF FINISH GOTO END H30 H31 H32 H68 H30 H0F H31 H70 H0C H32 H30,W FINISH

PIC Microcontroller & Appications

Page16

Appendix A2 : Listing of Lab2-2.asm Program

;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab2-2.asm * ; Date: 18/3/2000 * ; File Version: 1 * ; Author: * ; Company: Temasek Poly * ; Description: Addressing mode * ; * ;********************************************************************** list p=16f877 #include <p16f877.inc> ; define processor ; processor specific variable

;********************************************************************** ORG H00 ; processor reset vector BEGIN NOP ; no operation MOVLW H01 MOVWF MOVWF MOVWF BCF RRF RRF RLF RLF IORWF ANDLW COMF FINISH GOTO END H40 H41 H42 STATUS,C H40 H40 H41 H41,W H42 HF0 H40 FINISH

PIC Microcontroller & Appications

Page17

Appendix A3 : Listing of Lab2-3.asm Program


;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab2-3.asm * ; Date: 9/12/2000 * ; File Version: 2 * ; Company: Temasek Poly * ; Description: Copy a String of Data starting from memory H50 to * ; memory starting at H'60' using Indirect addressing * ;********************************************************************** list p=16f877 ; define processor #include <p16f877.inc> ; processor specific variable COUNT EQU H'20' ; define counter TEMP EQU H'21' ; temporary storage TEMP1 EQU H'22' ; temporary storage 1 TEMP2 EQU H'23' ; temporary storage 2 ;********************************************************************** ORG H'00' ; processor reset vector BEGIN NOP ; no operation MOVLW A'P' MOVWF H'50' MOVLW A'I' MOVWF H'51' MOVLW A'C' MOVWF H'52' MOVLW H'03' MOVWF COUNT MOVLW H'60' MOVWF TEMP2 MOVLW H'50' AGAIN MOVWF FSR MOVWF TEMP MOVF INDF,W MOVWF TEMP1 MOVF TEMP2,W MOVWF FSR MOVF TEMP1,W MOVWF INDF INCF TEMP INCF TEMP2 MOVF TEMP,W DECFSZ COUNT GOTO AGAIN FINISH GOTO FINISH END

PIC Microcontroller & Appications

Page18

Appendix A4 : Listing of Lab2-4.asm Program


;********************************************************************** ; PICmicro PIC16F877 Source Program * ; Filename: Lab2-4.asm * ; Date: 9/12/2000 * ; File Version: 2 * ; Author: * ; Company: Temasek Poly * ; Description: Software Delay routine using Relative Addressing * ; mode. * ; RC Clock Frequency = 2MHz * ; Instruction Clock = 2MHz/4 = 0.5MHz * ; Duration per cycle = 1/0.5MHz = 2us * ;********************************************************************** list p=16f877 ; define processor #include <p16f877.inc> ; processor specific variable OUTER EQU H'20' ; outer counter INNER EQU H'21' ; inner counter ;********************************************************************** ORG H'00' ; processor reset vector BEGIN NOP ; no operation DELAY MOVLW D'2' MOVWF OUTER LOOP MOVLW D'2' MOVWF INNER LOOP1 NOP NOP DECFSZ INNER GOTO LOOP1 DECFSZ OUTER GOTO LOOP FINISH GOTO FINISH END

PIC Microcontroller & Appications

Page19

You might also like