Assembly Language Programming Chapter 3 INSTRUCTION SET SUMMARY

Assembly Language Programming Chapter 3

INSTRUCTION SET SUMMARY • PIC 18 F 4580 devices incorporate the standard set of 75 PIC 18 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 (cont’d) • 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.

Branch Instructions and Looping

DECFSZ (decrement file. Reg skip if zero)

DECFSZ f, d, a If a is ignored (i. e. DECFSZ COUNT, F) then MPLAB assume a=0; access bank is selected by default. • f = file. Reg • d = destination ▫ 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 0 x 25 AGAIN MOVLW MOVWF MOVLW ADDLW DECFSZ GOTO MOVWF d'10' COUNT 0 3 COUNT, F AGAIN PORTB

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 0 x 25 AGAIN MOVLW MOVWF MOVLW ADDLW DECF BNZ MOVWF d'10' COUNT 0 3 COUNT, F AGAIN PORTB

Example 3 -2 Flowchart

Nested Loop (Example 3 -4) Write a program to: a) Load the PORTB with 55 H b) Complement PORTB 700 times

Example 3 -4 (Solution) R 1 R 2 COUNT_1 COUNT_2 LOP_1 LOP_2 EQU EQU MOVLW MOVWF COMF DECF BNZ 0 x 25 0 x 26 d'10' d'70' 0 x 55 PORTB COUNT_1 R 1 COUNT_2 R 2 PORTB, F R 2, F LOP_2 R 1, F LOP_1

How to Loop 100, 000 Times?

BZ (branch if zero)

Example 3 -5 Write a program to determine if file. Reg location 0 x 30 contains the value 0. if so, put 55 H in it.

Example 3 -5 (Solution) MYLOC NEXT EQU MOVF BNZ MOVLW MOVWF. . . Ox 30 MYLOC, F NEXT 0 x 55 MYLOC

BNC (branch if no carry, CY=0)

Example 3 -6 Find the sum of the values 79 H, F 5 H and E 2 H. Put the sum in file. Reg location 5 (low byte) and 6 (high byte).

Example 3 -6 (Solution) L_Byte H_Byte N_1 N_2 OVER EQU 0 x 5 EQU 0 x 6 ORG 0 h MOVLW MOVWF ADDLW BNC INCF MOVWF END 0 x 0 H_Byte 0 x 79 N_1 H_Byte, F 0 x. F 5 N_2 H_Byte, F 0 x. E 2 OVER H_Byte, F L_Byte

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 PIC 18 (up to 2 MB)

Unconditional Branch • BRA (Branch Always)

GOTO to itself using $ sign here over GOTO @ GOTO bra over @ $ here $

CALL instruction and STACK • CALL is used to call subroutine within 2 MB of address space (00000 -1 FFFFH) • 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 PIC 18 has 31 level of STACK!

Example 3 -9 Toggle all the bits of PORTB by sending the value 55 H and AAH continuously. Use a delay between the toggle process

Example 3 -9 (Solution) MYREG BACK DELAY AGAIN EQU ORG MOVLW 0 x 55 MOVWF PORTB CALL MOVLW MOVWF CALL GOTO 0 x 08 0 ORG MOVLW MOVWF NOP DECF BNZ RETURN END 300 H 0 x. FF MYREG DELAY 0 x. AA PORTB DELAY BACK MYREG, F AGAIN

Study Example 3 -10 • What is your findings?

CALL vs RCALL • CALL – anywhere within 2 MB address • RCALL – within 2 k. B address • Using RCALL could save a number of bytes in Program ROM space.

CALL vs RCALL (cont’d)

Study Example 3 -12 • How many bytes this program occupied in program ROM space? • Compare with Example 3 -9

PIC 18 Time Delay

Instruction Cycle Time • One instruction cycle consists of 4 oscillator periods. Instruction cycle, Tcy = 4/(Crystal frequency)

Example 3 -18 MYREG EQU 0 x 08 ORG MOVLW MOVWF CALL GOTO 0 0 x 55 PORTB DELAY 0 x. AA PORTB DELAY BACK ORG DELAY MOVLW MOVWF AGAIN NOP NOP DECF BNZ RETURN END 300 H 0 x. FA MYREG BACK MYREG, F AGAIN 1 1 1 2/1 2

Delay Calculation DELAY AGAIN MOVLW MOVWF NOP NOP DECF BNZ RETURN 0 x. FA MYREG, F AGAIN True condition 1 1 1 2/1 2 Loop False condition Total ins. Cycles = 1 + 249(1+1+2) + 1(1+1+1) + 2 = 1503 cycles Time delay = 1503 x 1 us = 1503 us (assume 4 MHz 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 PIC 18
- Slides: 40