Assembly Language Programming
Chapter 3
INSTRUCTION SET SUMMARY
PIC18F4580 devices incorporate the standard set of 75 PIC18 core instructions PLUS an extended set of 8 new instructions for the optimization of code.
Standard Instruction Set
Most instructions are a single program memory word (16 bits), but there are four instructions that require two program memory locations.
The instruction set is grouped into four basic categories:
Byte-oriented operations Bit-oriented operations Literal operations Control operations
Standard Instruction Set (contd)
All single-word instructions are executed in a single instruction cycle Unless a conditional test is true or the program counter is changed as a result of the instruction. In these cases, the execution takes two instruction cycles with the additional instruction cycle(s) executed as a NOP.
DECFSZ (decrement fileReg skip if zero)
DECFSZ
f = fileReg d = destination
f,d,a
If a is ignored (i.e. DECFSZ COUNT,F) then MPLAB assume a=0; access bank is selected by default.
d = 0: store result in WREG @ w d = 1: store result in file register f @ f
a = RAM access bit
a = 0: RAM location in Access RAM (Access Bank) a = 1: RAM bank is specified by BSR register
Example 3-1
Write a program to: a) Clear W b) Add 3 to W ten times and place the result in PORTB. Use DECFSZ to perform the looping.
Example 3-1 (Solution)
COUNT EQU MOVLW MOVWF MOVLW ADDLW DECFSZ GOTO MOVWF 0x25 d'10' COUNT 0 3 COUNT,F AGAIN PORTB
AGAIN
BNZ (branch if not zero)
Example 3-2
Write a program to: a) Clear W b) Add 3 to W ten times
Example 3-2 (Solution)
COUNT EQU 0x25 MOVLW MOVWF MOVLW ADDLW DECF BNZ MOVWF d'10' COUNT 0 3 COUNT, F AGAIN PORTB
AGAIN
Example 3-2 Flowchart
Nested Loop (Example 3-4)
Write a program to: a) Load the PORTB with 55H b) Complement PORTB 700 times
Example 3-4 (Solution)
R1 R2 COUNT_1 COUNT_2 EQU EQU EQU EQU MOVLW MOVWF MOVLW MOVWF MOVLW MOVWF COMF DECF BNZ DECF BNZ 0x25 0x26 d'10' d'70' 0x55 PORTB COUNT_1 R1 COUNT_2 R2 PORTB, F R2, F LOP_2 R1, F LOP_1
LOP_1 LOP_2
How to Loop 100,000 Times?
BZ (branch if zero)
Example 3-5
Write a program to determine if fileReg location 0x30 contains the value 0. if so, put 55H in it.
Example 3-5 (Solution)
MYLOC EQU MOVF BNZ MOVLW MOVWF ... Ox30 MYLOC,F NEXT 0x55 MYLOC
NEXT
BNC (branch if no carry, CY=0)
Example 3-6
Find the sum of the values 79H, F5H and E2H. Put the sum in fileReg location 5 (low byte) and 6 (high byte).
Example 3-6 (Solution)
L_Byte H_Byte EQU 0x5 EQU 0x6 ORG 0h MOVLW MOVWF ADDLW BNC INCF ADDLW BNC INCF ADDLW BNC INCF MOVWF END 0x0 H_Byte 0x79 N_1 H_Byte,F 0xF5 N_2 H_Byte,F 0xE2 OVER H_Byte,F L_Byte
N_1 N_2 OVER
All conditional branches are short jumps
Target address must be within 256 bytes! Refer to page 107 of textbook!
Unconditional Branch
GOTO (long jump) Can go to any memory location in PIC18 (up to 2MB)
Unconditional Branch
BRA (Branch Always)
GOTO to itself using $ sign
here GOTO @ GOTO here $
over
bra
bra
over @ $
CALL instruction and STACK
CALL is used to call subroutine within 2MB of address space (00000-1FFFFH) STACK is used to store temporary address during CALL statement (address is PUSHed into the stack)
RETURN instruction is used to return to the main program (address is POPped out of the stack)
31 Level of Stack PIC18 has 31 level of STACK!
Example 3-9
Toggle all the bits of PORTB by sending the value 55H and AAH continuously. Use a delay between the toggle process
Example 3-9 (Solution)
MYREG
BACK
EQU ORG MOVLW 0x55 MOVWF PORTB CALL MOVLW MOVWF CALL GOTO ORG MOVLW MOVWF NOP NOP DECF BNZ RETURN END
0x08 0 DELAY 0xAA PORTB DELAY BACK 300H 0xFF MYREG
DELAY AGAIN
MYREG, F AGAIN
Study Example 3-10
What is your findings?
CALL vs RCALL
CALL anywhere within 2MB address RCALL within 2kB address Using RCALL could save a number of bytes in Program ROM space.
CALL vs RCALL (contd)
Study Example 3-12
How many bytes this program occupied in program ROM space? Compare with Example 3-9
Instruction Cycle Time
One instruction cycle consists of 4 oscillator periods. Instruction cycle, Tcy = 4/(Crystal frequency)
Example 3-18
MYREG EQU BACK 0x08
ORG MOVLW MOVWF CALL MOVLW MOVWF CALL GOTO
0 0x55 PORTB DELAY 0xAA PORTB DELAY BACK
300H 0xFA MYREG 1 1 1 1 1 1 2/1 2
ORG DELAY MOVLW MOVWF AGAIN NOP NOP NOP DECF BNZ RETURN END
MYREG, F AGAIN
Delay Calculation
DELAY AGAIN MOVLW MOVWF NOP NOP NOP DECF BNZ RETURN 0xFA MYREG 1 1 1 1 1 1 2/1 2
Loop
MYREG, F AGAIN
True condition
False condition
Total ins. Cycles = 1 + 1 + 249(1+1+1+1+2) + 1(1+1+1+1+1) + 2 = 1503 cycles Time delay = 1503 x 1us = 1503us (assume 4MHz crystal is used)
Example 3-20
Show the mathematical calculation to obtain the value in figure below (based on example 3-20).
Instruction Pipeline in PIC18