TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE Prepared
TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010 E 4160 – Microprocessor & Microcontroller System
Learning Outcomes 2 At the end of this topic, students should be able to: Differentiate high level, assembly and machine language Define the terms : opcode/mnemonic, operand, label, comment, machine code, pseudo-operation Classify PIC 16 F 877 instruction set into the following groups: data movement, arithmetic, logic, branch and other instructions. Explain the function of PIC 16 F 877 status register and the changes to the flags when related instructions are executed. Explain the stack process according to LIFO & FIFO
Programming Language 3 The lowest-level language is called Machine languages. It means those languages which are closer to the understanding of machine rather than human beings. A machine language thus comprises a string of binary O’s and 1’s. Machine language is actually a coded set of instructions for a particular CPU (Central Processing Unit), and it is also known as a machine code. A machine language is designed to be used by a computer without the need of translation.
Machine Language 4 Disadvantage : 1. It is a machine dependent programming language. Machine dependent means the program designed in one type of machine or computer could not be run on other type of computer or machine. So programs designed in the machine language in one computer are not easily portable to other computers. 2. It is a very difficult language to understand learn. If there is any problem in the program, written in machine language, then it is very difficult to find out the correct mistake.
Assembly language 5 Assembly language is one level above the machine language. (Both Machine and Assembly language are considered low-level language) It uses certain predefined symbolic codes instead of binary codes. These symbolic codes are called mnemonics. Assembly language programs are translated into machine language by a program called an assembler. Assembler Process
High-level language 6 High-Level Language overcomes the limitation of writing a program in Machine and Assembly language as it is difficult and time consuming. In High-Level Language, the programs can be written using simple English words. Examples of High-Level Language are BASIC, Fortran, COBOL, C, C++. Programs written in high-level languages are translated into machine language by a
Assembly language syntax 7 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 8 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.
Assembler directive 9 Assembler directives are instructions that are directed to the assembler to do a specific thing. It is not translated into machine code. (Assembler directives are executed by the assembler at assembly 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.
Example of assembler directives 10 EQU - Equate ORG - Origin INCLUDE END Assigns a value to a symbol (same as = ) e. g. TRISA EQU 0 x 85 Sets the current origin to a new value. This is used to set the program or register address during assembly. For example, ORG 0 x 00 tells the assembler to assemble all subsequent code starting at address 0000 H. An 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, to add the standard header file for the PIC 16 F 877 device to your assembly code, use: #INCLUDE P 16 F 877. INC This directive is the last statement in an assembly language program. The END directive terminates an assembly language program.
PIC Assembly Code 11 Consists of 4 fields: 1 2 3 operand 4 f = Source : name of special-purpose register or RAM variable F(W) = Destination : F – destination is f W – destination is Working Register
Instruction format – Label 12 A label is used to represent a line or group of code, or a constant value. It is needed for branching instructions. Labels should start in column 1. They may be followed by a colon (: ), 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 bar and the question mark. Labels must not: begin with two leading underscores, e. g. __temp begin with a leading underscore and number. e. g. _2 NDLOOP be an assembler reserved word (mnemonic, directive, etc. ).
Instruction format – Label 13 Labels may be up to 32 characters long. By default they are case sensitive, but case sensitivity may be overridden by a commandline option (/c). If a colon is used when defining a label, it is treated as a label operator and not part of the label itself. Example: Here NOP
Instruction format - Opcode 14 This field consists of a symbolic operation code, known as op-code. The opcode describes the operation. Symbolic op-codes (known as mnemonic) are translated into machine language opcode. Mnemonics are not case sensitive. Example: BSF (Bit Set f) CLRW (Clear W)
Instruction format - Operand Field 15 Operands give information to the instruction on the data that should be used and the storage location for the instruction. Operands must be separated from mnemonics by one or more spaces, or tabs. It may has one, two or no operands at all. Multiple operands must be separated by commas. Examples of instructions with different operand fields NOP ; Instruction with no operand ANDLW 0 x 34 ; Instruction with one operand ADDWF FSR, 1 ; Instruction with two operand
16 Instruction format – Comment Field Comments are text explaining the operation of a line or lines of code. A semicolon (; ) marks the beginning of a comment A semicolon in the beginning of a line makes it all a comment line. All characters following the semicolon are ignored through the end of the line. Good programming practice dictates the use of a comment on almost every line. Example: ; Statement line with a comment field BSF Port. A, 0 ; set pin 0 of Port. A
17 Example MPASM Assembler Source Code
PIC 16 F 877 A Instruction set 18 Each microcontroller family has its own set of instructions, which carry out essentially the same set of operations, but using different syntax. The PIC 16 F 877 only has 35 instructions. Each instruction is 14 -bit words. This instruction set organized by functional groups is listed in Table 1.
Table 1: PIC instruction set by functional 19 groups Instruction Type Definition Examples MOVE The contents of a register are copied to another. MOVF, MOVWF, MOVLW REGISTER Register operations affect only a single 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 binary ADDWF, ADDLW, gives the same result as in SUBWF, SUBLW decimal or hex. . LOGIC Logic operations are carried out on bit pairs in two numbers to give the result which would be obtained if they were fed to the corresponding logic gate ANDWF, ANDLW, IORWF, IORLW, XORWF, XORLW TEST, SKIP & JUMP make decisions (conditional program branches) which depend on some input condition or the result of a calculation BTFSC, BTFSS, DECFSZ, INCFSZ, GOTO, CALL, RETURN, RETLW, RETFIE
PIC 16 F 877 A Instruction set 20 Some instructions with alternate result destinations. The default destination for the result of an operation is the file register, but the working register W is sometimes an option. The instruction set can also organized by operational groups as shown in Table 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
21 Table 2. 1 - PIC instruction set : Byte-oriented file register operations
22 Table 2. 2 - PIC instruction set : Bit-oriented file register operations
23 Table 2. 3 - PIC instruction set : Literal and Control Operations
PIC instruction set – description 24 convention
ADDLW 25
ADDWF 26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Stack 58 A stack is a Last In, First Out ( LIFO ) data structure Stacks are used to temporarily store items in memory (RAM) / Pull When an item enters the stack it is said to be pushed on the stack. When an item leaves the stack, it is said to be pulled off the stack. 58
59 In contrast to the Queue (Q): only one pointer to top element. PUSH and POP/PULL in different directions. wrap-around is not needed. (PUSH) Direction of PULL operation 59
60 The two fundamental operations: 1. Push - push an element on the top - PUSH should first decrement SP, $00 FB $00 FC then store the data, $00 FD $00 FE - Instruction used : $00 FF - Example: Note: [ ] means the location pointed by SP 60
2. Pull 61 - pull the top element from the stack - removes an item from the top of the list, and returns this value to the caller. - Instruction used : - PULL should first read the data, 10 X = $10, SP=[SP]+1 67 08 then increment SP. - Example: Note: [ [ ] ] means the content of location pointed by SP 61
FIFO 62 FIFO is an acronym for First In, First Out. This expression describes the principle of a queue (Q) : what comes in first is handled first, what comes in next waits until the first is finished, etc. It is a very common data structure used for I/O interfacing Example : The ASCII codes are PUT in a FIFO whenever you hit the key. A FIFO is also used when you ask the computer to print a file.
FIFO Concept 63 FIFO/Queue operation: Needs two pointers – PUTPT and GETPT in same directions. wrap-around (items go on the rear and come off of the front of the queue. ) is needed.
References 64 PIC Instruction Set and Some Tips for Programming, Dr. Charles J. Kim, Howard University Microchip PIC 16 F 87 X Data Sheet Getting Started with Development Tools, Microchip. com Stack, http: //en. wikipedia. org
- Slides: 64