Beruflich Dokumente
Kultur Dokumente
Engineering Faculty
Department of Computer Engineering
ECOM 3022: Embedded Systems Discussion
Chapter 2
PIC ARCHITECTURE & ASSEMBLY
LANGUAGE PROGRAMMING
The WREG (Working Register) Register is one of the most widely used registers of the
PIC.
8-bit register any data larger than 8 bits must be broken into 8-bits chunks before it is
processed.
There is only one.
MOVLW k
-
ADDLW k
-
Dedicated to specific functions such as ALU status, timers, serial communication, I/O
ports, ADC,
The function of each SFR is fixed by the CPU designer at the time of design
Group of RAM locations that are used for data storage and scratch pad
8-bit registers
Larger than SFR, difficult to manage them by using Assembly language, easier to handle
them by C Compiler.
It tells the CPU to complement the content of fileReg and places the results in WREG or
in fileReg.
Example:
MOVLW 55H WREG = 55H
MOVWF PORTB PortB = 55H
COMF PORTB, F PortB = AAH
COMF PORTB, W WREG = 55H
It tells the CPU to decrement the content of fileReg and places the results in WREG or in
fileReg.
Example:
MOVLW 3 WREG = 3H
MOVWF 12H 12H = 3H
DECF 12H, F 12H = 2H
DECF 12H, W WREG = 1H, but 12H = 2H
Example: get data from the SFRs of Port B and send it to the SFRs of PORT C:
MOVFF PORTB, PORTC PORTC = PORTB
Example: Show the status of the C, DC, Z flags after the following addition instruction
MOVLW 9CH
ADDLW 64H
9CH + 64H = 100H WREG = 00H
9CH 1001 1100
C=1
+ 64H 0110 0100
DC=1
100H 0000 0000
Z=1
There is one data type: 8 bits, it is the job of the programmer to break down data larger
8 bits
Data type can be positive or negative
Data format are:
o Hex (default in PIC) 12 or 0x12 or H'12' or 12H
If the value starts with the hex digits A-F, then it must be preceded with a zero.
MOVLW E5H; invalid, it must be MOVLW 0E5H
ADDLW C6; invalid, it must be ADDLW 0C6
o Binary B'00010010'
o Decimal .12 or D'12'
o ASCII A'c'
o H, B, D, A: capital or small
To define ASCII strings (more than one character), we use the DB (define byte) directive
Assembler Directives
-
The Instruction tell the CPU what to do, while Directives (pseudo instructions) give
direction to assembler.
Directives: EQU, SET, ORG (Origin), END, LIST
Directive dont generate any machine code and used by assembler.
EQU (equate)
o It is used to define a constant value or a fixed address.
o It associates a constant number with a data or an address label so that when the
label appears in the program, its constant will be substituted for the label.
o Example:
COUNT EQU 0x25
MOVLW COUNT WREG = 25H
SET
Its used to define a constant value or a fixed, its the identical to EQU, but the SET
may be reassigned later.
ORG (origin)
It is used to indicate the beginning of the address.
END
This indicates to assembler the end of the source (asm) file.
LIST
It indicate to the assembler the specific PIC chip for which the program should be
assembled
LIST P=18F458
#include
It tells the PIC assembler to use the libraries associated with the specific chip for
which we are compiling the program.
_config
It tells the assembler the configuration bits for the targeted PIC chip.
radix directive
It indicates whether the numbering system is hexadecimal or decimal. The default is
hex if we do not use the radix directive. If we use "radix dec", the default
representation will change to decimal.
Unique name
Alphabetic letters (upper, lower), digits (0-9), special char. (? . @_ $)
The first letter must be Alphabetic letters
Not a reserved word
Program Counter (PC) is used by the CPU to point to the address of the next instruction
to be executed
The wider the program counter, more the memory locations can be accessed
The program counter in the PIC 18 family is 21-bit.
Program addresses from 000000 to 1FFFFFH, a total of 221 = 2M of code.
With 4K of on-chip ROM memory space, we have 4096 bytes (4 x 1024 = 22 x 210 = 212 =
4096). PC is 12 bit, address locations from 0000 to 0FFFH.
Powering UP
-
10
11
PROBLEMS
7. Which of the following is (are) illegal?
(a) MOVLW 500 the operand exceeds allowed range.
(b) MOVLW 50
(c) MOVLW 00
(d) MOVLW 255H the operand exceeds allowed range.
(e) MOVLW 25H
(f) MOVLW F5H The operand should start with 0.
(g) MOVLW mybyte, 50H The syntax is wrong.
8. Which of the following is (are) illegal?
(a) ADDLW 300H the operand exceeds allowed range
(b)ADDLW 50H
(c) ADDLW $500 the operand exceeds allowed range
(d) ADDLW 255H the operand exceeds allowed range
(e) ADDLW 12H
(f) ADDLW 0F5H
(g) ADDWL 25H
12. True or false. We have many WREG registers in the PICI8.
13. PIC data RAM consists of (EEPROM, SRAM).
14. True or false. Data RAM in PIC is also called the file register.
17. True or false. All members of PICl8 family have the same size file register.
28. Show a simple code to load values 30H and 97H into locations 5 and 6 respectively.
MOVLW 30H
MOVWF 5H
MOVLW 97H
MOVWF 6H
29. Show a simple code to load value 55H into locations 0-8.
MOVLW 55H
MOVWF 0H
MOVWF 1H
MOVWF 2H
MOVWF 3H
MOVWF 4H
MOVWF 5H
MOVWF 6H
MOVWF 7H
MOVWF 8H
12
30. Show a simple code to load value 5FH into Port B SFR.
MOVLW 5FH
MOVWF PORTB
31. True or false. We can not load literal values into the scratch pad area directly.
35. Show a simple code to (a) load value 11H into locations 0-5, and (b) add the values together
and place the result in WREG as they are added.
(a)
MOVLW 11H
MOVWF 0H
MOVWF 1H
MOVWF 2H
MOVWF 3H
MOVWF 4H
MOVWF 5H
(b)
MOVLW 0x00
ADDWF 0H, W
ADDWF 1H, W
ADDWF 2H, W
ADDWF 3H, W
ADDWF 4H, W
ADDWF 5H, W
36. Repeat Problem 35, except place the result in location 5 after the addition is done.
MOVLW 11H
MOVWF 0H
MOVWF 1H
MOVWF 2H
MOVWF 3H
MOVWF 4H
MOVWF 5H
MOVLW 0x00
ADDWF 0H, W
ADDWF 1H, W
ADDWF 2H, W
ADDWF 3H, W
ADDWF 4H, W
ADDWF 5H, F
13
37. Show a simple code to (a) load value 15H into location 7, and (b) add it to WREG five times
and place the result in WREG as the values are added. WREG should be zero before the addition
starts.
(a)
MOVLW 15H
MOVWF 7H
(b)
MOVLW 0H
ADDWF 7H, W
ADDWF 7H, W
ADDWF 7H, W
ADDWF 7H, W
ADDWF 7H, W
38. Repeat Problem 37, except place the result in location 7 as numbers are being added
together.
(a)
MOVLW 15H
MOVWF 7H
(b)
MOVLW 0H
ADDWF 7H, F
ADDWF 7H, F
ADDWF 7H, F
ADDWF 7H, F
ADDWF 7H, F
42. Write a simple code to copy data from location 8 to PORTC (a) using WREG and (b) without
using WREG.
(a)
MOVF 8H, W
MOVWF PORTC
(b)
MOVFF 8H, PORTC
53. Show a simple code to (a) load value 11H into locations 0-5, and (b) add them together and
place the result in WREG as the values are added. Use EQU to assign the names R0-R5 to
locations 0-5.
R0 EQU 0H
R1 EQU 1H
R2 EQU 2H
R3 EQU 3H
R4 EQU 4H
R5 EQU 5H
14
(a)
MOVLW 11H
MOVWF R0
MOVWF R1
MOVWF R2
MOVWF R3
MOVWF R4
MOVWF R5
(b)
MOVLW 0H
ADDWF R0, W
ADDWF R1, W
ADDWF R2, W
ADDWF R3, W
ADDWF R4, W
ADDWF R5, W
75. A given PIC has 7FFFH as the address of the last location of its on-chip ROM. What is the size
of on-chip ROM for this PIC?
7FFF 15 bit
215 = 32768 bytes = 25 x 210 = 32KB
79. How wide is the ROM in the PICI8 chip?
16 bits
80. How wide is the data bus between the CPU and the program ROM in PICI8?
16 bits
91. In the instruction "GOTO target-addr" explain why the lowest bit of the program counter is
0.
Because the data bus between the CPU and ROM of PIC18 microcontrollers is 16 bits
wide. Whenever CPU fetches the data from ROM, it receives 2 bytes. Therefore data is
always fetched starting from even addresses and because of this, program counter
always holds even values and its LSB is 0.
92. Explain why the instruction "GOTO target-addr" will not land at an odd address.
Because the LSB of program counter is always 0.
93. In Question 92, explain why it should not.
Because there are only 2-byte and 4-byte instructions in PIC18 microcontrollers and all
instructions are stored in ROM starting from even addresses. If program counter could
get odd values, it would land at an odd address (in the middle of an instruction).
Best Wishes