Lecture-3-PIC Assembly
Lecture-3-PIC Assembly
Lecture 3
PIC Assembly
CSE 3442/5442
Embedded Systems I
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
RISC: Reduced Instruction
Set Computer
1. Fixed instruction size (2 and 4 bytes in PIC ;
ADD, GOTO)
2. Many registers (no need for large stack)
3. Small instruction set – longer code
4. Small clock cycle/instruction
5. Usually Harvard architecture
6. No microcoding; instructions are internally
hardwired – can result in 50% reduction in the
number of transistors
7. No cross operations between GFR registers
2
PIC uses Harvard Architecture
3
PIC18F452 Pin Diagram
4
Example - Powering Up
PIC18F458
5
Example - Powering Up
PIC18F458
6
Example - Powering Up
PIC18F458
7
Programs in ROM
8
Recap
9
Recap
• Register
– A place inside the PIC that can be written to,
read from, or both (8-bit numbers)
10
Dec, Hex, Bin
11
Assembler/Compiler
Data Formats
• Data Byte Representation
– hex, decimal, binary, ASCII
Format .ASM .C
12
Assembler/Compiler
Directives
• Instructions (MOVLW, ADDLW, etc.) tell CPU what to do
• Directives give directions to the Assembler/Compiler
– “pseudo-instructions”
• Assembler directives:
– EQU (defining constants), (SET is similar but can be reset)
– ORG (origin - explicit address offset operand must be hex)
– END (tells assembler that this is end of code)
– LIST (indicates specific controller, e.g., LIST P=18F452)
– #include (to include libraries associated)
– _config directives – tell assembler what the configuration (stored at
300000H) bits of the target PIC should be
– radix (e.g., radix dec will change to decimal notation; default is hex)
13
Configuration Registers
15
Assembly Language
Structure
[label] mnemonic [operand1, operand2] [;comment]
optional
• Label: Can now refer to a line of code by name
• Mnemonic (instruction): ADDLW, BNZ, etc.
• Operand(s): Literal, file register location, variable
that is manipulated, used, or acted upon
• Comment: starts with ; and is ignored by assembler
16
Instruction Format
17
Instruction Format
18
Instruction Set Info.
19
Opcode
20
Status Flags
21
Assembly Programming
Sample
SUM EQU 0F7H
ORG 0H
HERE MOVLW 0
MOVWF SUM
MOVLW 25H ;25H WREG
ADDLW 0x34 ;+ 34H
ADDLW 11H ;+ 11H
ADDLW 0C1H ;+ C1H
ADDLW 25 ;+25H
ADDLW D'18' ;+ 18 decimal
ADDLW B'00000110‘ ;+6 dec
MOVWF SUM
MOVLW SUM
GOTO HERE
END 22
Assembly Code Assembled an
Linked
23
Program Counter (PC)
ORG 0
…
MOVLW 5
ADDLW 4
…
END
24
PIC18 Program Counter
2 MB
25
PIC18 On-Chip Program ROM
Address Range
4k
16 k
32 k
(452)
Figure 2-10
26
PIC18 Program
ROM Space
27
Assembly Programming
sample
SUM EQU 0F7H
ORG 0H
MOVLW SUM
GOTO HERE
END 28
PIC18 Program ROM Width
29
ROM Contents
Opcode
0x00 MOVLW 25H 0x0E
0x06
0x08 Opcode
… 0x0F
30
ROM Contents
25H MOVLW
0x00 MOVLW 25H
34H ADDLW
0x02 ADDLW 34H 11H ADDLW
0x06
0x08
…
25H 0EH
34H 0FH
11H 0FH
31
ROM Contents
32
GOTO and the PC
21 bit PC 33
Assembly Programming
sample
SUM EQU 0F7H
0x00 ORG 0H
MOVLW SUM
GOTO HERE ;GOTO 0x00
END 34
Note
35