03 instruction set - Philadelphia University

18
1 Embedded Systems PIC16F84A Instruction Set Dr. Nasser Halasa Second Semester 2017-2018 PIC mid range ALU Block Diagram of the PIC 16 Series ALU Assembly language syntax An assembly language program consists of statements. The syntax of an assembly language program statement obeys the following rules: - Only one statement is written per line. - Each statement is either an instruction or an assembler directive. - Each instruction has an op-code and possibly one, two or no operands at all. - An op-code is known as mnemonic. - Each mnemonic represents a single machine instruction. - Operands provide the data to work with. Basic Assembly Program Structure Assembly language is made up of two(2) types of statements: Assembler Directive: Inform the assembler about the program and the environment and NOT be translated into machine code. Executable Instruction: One of the processor's valid instructions which can be translated into machine code form by the assembler.

Transcript of 03 instruction set - Philadelphia University

1

Embedded Systems

PIC16F84A Instruction Set

Dr. Nasser HalasaSecond Semester 2017-2018

PIC mid range ALU

Block Diagram of the PIC 16 Series ALU

Assembly language syntax

An assembly language program consists of statements. Thesyntax of an assembly language program statement obeysthe following rules:

- Only one statement is written per line.- Each statement is either an instruction or an assembler

directive.- Each instruction has an op-code and possibly one, two or

no operands at all.- An op-code is known as mnemonic.- Each mnemonic represents a single machine instruction.- Operands provide the data to work with.

Basic Assembly Program Structure

Assembly language is made up of two(2) types ofstatements:

Assembler Directive:

Inform the assembler about the program and theenvironment and NOT be translated into machine code.

Executable Instruction:

One of the processor's valid instructions which can betranslated into machine code form by the assembler.

2

Assembler directives are instructions that are directed tothe assembler to do a specific thing.

It is not translated into machine code.(Assembler directives are executed by the assembler atassembly time, not by the CPU at run time).

Directives can be used to : Link symbolic names to actual values. Set up pre-defined constants. Allocate storage for data in memory. Control the assembly process. Include additional source files. starting address for the program.

Assembler directive

EQU - EquateAssigns a value to a symbol (same as = ) e.g. TRISA EQU 0x85

ORG - OriginSets the current origin to a new value. This is used to set the program or register addressduring assembly. For example, ORG 0x00 tells the assembler to assemble all subsequentcode starting at address 0000H.

INCLUDEAn assembler include, or header, file is any file containing valid assembly code.Usually, the file contains device-specific register and bit assignments. This file may be“included” in the code so that it may be reused by many programs. As an example, toadd the standard header file for the PIC16F84A device to your assembly code, use:

#INCLUDE P16F84A.INC

ENDThis directive is the last statement in an assembly language program. The ENDdirective terminates an assembly language program.

Example of assembler directives

Assembler Directive

Assembler Directive Summary of Action

list implement a listing option*

#include include additional source file

org set program origin

equ define an assembly constant; thisallows us to assign a value to a label

end end program block

Radix Example Representation

Decimal D‘255’

Hexadecimal H‘8d’ or Ox8d

Octal O‘574’

Binary B‘01011100’

ASCII ‘G’ or A‘G’

Assembler File Structure (Simple Form)

For us, this will beMPASM

Written by you, as a textfile, in Assembler format

Files that the Assembler(e.g. MPASM) generates

Source File Assembler

Executable File

List File

Error File

.asm

.hex

.lst

.err

3

PIC Assembly Code

Consists of 4 fields:

f = Source : name of special-purpose register or RAM variable

F(W) = Destination :F – destination is f

W – destination is Working Register

operand1

42

3

Instruction format – Label

A label is used to represent a line or group of code, or aconstant value. It is needed for branching instructions.

Labels should start in column 1. They may be followed by acolon (:), space, tab or the end of line.

Labels must begin with an alpha character or an under bar(_) and may contain alphanumeric characters, the under barand the question mark.

Labels must not: begin with two leading underscores, e.g. __temp begin with a leading underscore and number.

e.g. _2NDLOOP be an assembler reserved word (mnemonic, directive, etc.).

Labels may be up to 32 characters long.

By default they are case sensitive, but casesensitivity may be overridden by a command-lineoption (/c).

If a colon is used when defining a label, it is treatedas a label operator and not part of the label itself.

Example:

Here NOP

GOTO Here

Instruction format – Label Instruction format - Opcode

This field consists of a symbolic operation code,known as op-code.

The opcode describes the operation.

Symbolic op-codes (known as mnemonic) aretranslated into machine language opcode.

Mnemonics are not case sensitive.

Example: BSF (Bit Set f)

CLRW (Clear W)

4

Operands give information to the instruction on the datathat should be used and the storage location for theinstruction.

Operands must be separated from mnemonics by one ormore spaces, or tabs.

It may has one, two or no operands at all. Multipleoperands must be separated by commas.

Examples of instructions with different operand fields

NOP ; Instruction with no operand

ANDLW 0x34 ; Instruction with one operand

ADDWF FSR,1 ; Instruction with two operand

Instruction format - Operand Field Instruction format – Comment Field

Comments are text explaining the operation of a line orlines of code.

A semicolon (;) marks the beginning of a comment

A semicolon in the beginning of a line makes it all acomment line. All characters following the semicolon areignored through the end of the line.

Good programming practice dictates the use of acomment on almost every line.Example:

;Statement line with a comment field

BSF PortA,0 ;set pin 0 of PortA

Example MPASM Assembler Source Code PIC16F84A Instruction set

Each microcontroller family has its own set ofinstructions, which carry out essentially the same set ofoperations, but using different syntax.

The PIC16F84A only has 35 instructions. Each instructionis 14-bit words.

This instruction set organized by functional groups islisted in Table 1.

5

Table 1: PIC instruction set by functional groupsInstruction Type Definition Examples

MOVE The contents of a register are copiedto another.

MOVF, MOVWF, MOVLW

REGISTER Register operations affect only asingle register, and all except CLRW(clear W) operate on file registers.

CLRW, CLRF, DECF, INCF,SWAPF, COMF, RLF, RRF,BCF, BSF

ARITHMETIC Addition and subtraction in binarygives the same result as in decimal orhex. .

ADDWF, ADDLW, SUBWF,SUBLW

LOGIC Logic operations are carried out onbit pairs in two numbers to give theresult which would be obtained ifthey were fed to the correspondinglogic gate

ANDWF, ANDLW, IORWF,IORLW, XORWF, XORLW

TEST, SKIP & JUMP make decisions (conditional programbranches) which depend on someinput condition or the result of acalculation

BTFSC, BTFSS, DECFSZ,INCFSZ, GOTO, CALL,RETURN, RETLW, RETFIE

CONTROL NOP, SLEEP, CLRWDT

Some instructions with alternate result destinations. The default destinationfor the result of an operation is the file register, but the working register Wis sometimes an option.

The instruction set can also organized by operational groups as shown inTable 2.1 – Table 2.3.

There are three basic categories: Byte-Oriented Instruction: F: File Register (or RAM) D: Destination

D=0: Destination W D=1: Destination File Register

Bit-Oriented Instruction: F: Register File where the Bit is located B: Bit Field

Literal and Control Operation: K: 8-bit constant

PIC16F84A Instruction set

PIC instruction set – description conventionTable 2.1- PIC instruction set :

Byte-oriented file register operations

6

Table 2.2 - PIC instruction set :Bit-oriented file register operations

Table 2.3 - PIC instruction set :

Literal and Control Operations

ADDLW ADDWF

7

8

9

10

11

12

13

14

Example

Describe briefly the operation of the followinginstructions:

CLRWMOVLW 0x05MOVWF 0x0CINCF 0x0C, 1MOVF 0x0C, 0ADDLW 0x32MOVWF 0x0D

15

Example

Write instructions to add the values stored in locations 0x10and 0x11 and store the result in location 0x12

1. Move value from location 0x10 into W register

MOVF 0 x10, 0

1. Add the value in location 0x11 to the value of the W register

ADDWF 0x11, 0

1. Move the result from W register to location 0x12

MOVWF 0x12

Example

Write instructions to perform the following calculation andstore the result in location 0x25

0x11 + 0x32 – 0x2E

MOVLW 0x11

ADDLW 0x32

MOVWF 0x25

MOVLW 0x2E

SUBWF 0x25, 1

Another Example

Write instructions to swap (exchange) the valuesstored in locations 0x20 and 0x21

move the value in location0x20 to a temporary location

move the value in location0x21 to location 0x20

move the value in thetemporary location tolocation 0x21

MOVF 0x20, 0MOVWF 0x23

MOVF 0x21, 0MOVWF 0x 20

MOVF 0x23, 0MOVWF 0x 21

Examples

Write instructions to perform the following:

1. add 15 to the value stored in memory location 0x29 and store theresult in the same location

2. select bank 1

3. Store 0x26 in register TRISB

4. Store the value of the STATUS register in location 0x40

5. Jump to program memory location 0x0132 if Z flag is 1 otherwisecontinue program execution

6. clear TRISB<3:0> and set TRISB<7:4> to ones

7. clear TRISB<3:0> without affecting the other bits of TRISB

8. If the value in location 0x35 is 16, set it to zero, otherwiseincrement it

16

Assembler

For the MCU to understand the instruction, it has to betranslated to machine language

Example: translate the following instruction to machinelanguage:

MOVLW 0xF3

11 0000 1111 0011 = 0x30F3

BSF 0x2E, 4

01 0010 0010 1110 = 0x122E

IORWF 0x3A , 1 ?

Assembler

Example: translate the following machine language instruction toassembly: 0x19C4

0x19C4 = 01 1001 1100 0100

BTFSC 0x44, 3

0x16B5

BSF 0x35, 5

0x2A3C

Example

What does the following code do ?

C L R F 0 x 0 6

l o o p I N C F 0 x 0 6 , 1

BTFSS 0x06 ,3

G O T O l o o p

C L R F 0 x 0 6

Assembler directives

Assembler directives are commands to the assembler

Directives are not part of the instruction set

So, they are not instructions for the PIC

They are not translated to machine language

They simplify writing the program

17

Assembler Directive: EQU

EQU: is an assembler directive used to define aconstant

for example: age EQU 0x0F

here the assembler will find and replace all

occurrences of 'age' with 0x0F

age EQU 0x0FMOVLW D'23'MOVWF age

equivalentinstructions

MOVLW D'23'

MOVWF 0x0F

Assembler Directive: ORG

ORG is a directive for the assembler to specify the location ofthe instruction in program memory

Example:

a g e E Q U 0 x 0 FORG 0x01E

MOVLW D'23'MOVWF age

After downloading

code to the PIC

Other Assembler directives

#include "name.h"

– replaces this line with the contents of the file name.h

– Microchip provides header files containing allregister names with their addresses

end

– after the last instruction in the program

instructions after “end” directive are not assembled

Example

Initialize W to zero, and keep adding 0x10 to the Wregister until the result in W is zero

ORG 0x000STATUS EQU 0x03Z EQU 2

CLRWl o o p A D D LW 0 x 1 0

BTFSS STATUS, ZG O T O l o o p

18

Example

Write a program that adds

the numbers from 1 to 10

and stores the result in

location 0x20

#include "p16f84a.inc"SUM EQU 0x20CNTR EQU 0x21ORG 0x000

GOTO STARTORG 0x005START MOVLW D'10'

MOVWF CNTRCLRW

LOOP ADDWF CNTR, 0DECFSZ CNTR, 1GOTO LOOPMOVWF SUM

END