Sei sulla pagina 1di 119

EC501 EMBEDDED

SYSTEM
APPLICATIONS
CHAPTER 2 MICROCONTROLLER ARCHITECTURE
& ASSEMBLY LANGUAGE PROGRAMMING
Explain the file register, instruction sets, status register,
data format and directives. Describe and apply the MPLAB
IDE compiler in the assembly language programming.
hafriz.fikrie@gmail.com

Pg 1

2.1 Understand the PIC18


Architecture
2.1.1 Describe PIC18 Microcontroller families
2.1.2 Explain PIC18 architecture with the aid of block
diagram
2.1.3 Explain the data RAM file Register
a. WREG register
b. File register (SFRs and GPR)
c. Status Register
d. Data format and directives

Pg 2

2.1.1 Describe PIC18 Microcontroller


families

Pg 3

2.1.1 Describe PIC18 Microcontroller


families

Part 1 of 2
Pg 4

2.1.1 Describe PIC18 Microcontroller


families

Part 2 of 2
Pg 5

2.1.1 Describe PIC18 Microcontroller


families
PIC is a family of modified Harvard architecture
microcontrollers made by Microchip Technology, derived
from the PIC1650 originally developed by General
Instrument's Microelectronics Division. The name PIC initially
referred to "Peripheral

Interface

Controller".

Pg 6

2.1.1 Describe PIC18 Microcontroller


families

http://www.mi
kroe.com/chap
ters/view/74/

Pg 7

2.1.2 Explain PIC18 architecture with the


aid of block diagram

ference: PIC18 Microcontroller and


mbedded Systems using Assembly & C

Pg 8

2.1.2 Explain PIC18 architecture with the


aid of block diagram

Pg 9

2.1.3 Explain the data RAM file Register


a. WREG register
b. File register (SFRs and GPR)
c. Status Register
d. Data format and directives

Pg 10

2.1.3 Explain the data RAM file Register


CPUs use many registers to store data temporarily.
To program assembly language, we must understand the
register and architecture of a given CPU and the role play
in processing data.
A register is the lowest level of data storage in a
computer. Registers also have the lowest read/write time
of all types of memory in your computer, making them
idea to perform small, repeated calculations.

Pg 11

2.1.3 Explain the data RAM file Register


Central Processing Unit
(CPU)
Mostly the PIC's CPU (8Bit) is manufactured with
in RISC technology an
important factor when
deciding which
microprocessor to use.

Pg 12

2.1.3 Explain the data RAM file Register


Central Processing Unit (CPU)
RISC (Reduced Instruction Set Computer), gives two great
advantages:
The CPU can recognizes only 35 simple instructions for PIC16
series and 75 instructions for PIC18 series. (In order to
program some other microcontrollers it is necessary to know
more than 200 instructions by heart).
The execution time is the same for all instructions except two
and lasts 4 clock cycles (oscillator frequency is stabilized by a
quartz crystal). The Jump and Branch instructions execution
time is 2 clock cycles. It means that if the microcontrollers
operating speed is 20MHz, execution time of each instruction
will be 200nS, i.e. the program will be executed at the speed
of 5 million instructions per second!
Reference:
http://www.mikroe.com/chapters/view/2/chapter-1-pic16f887-microcontroller-device-overview/ &
PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 85

Pg 13

2.1.3 Explain the data RAM file Register


ROM Memory
ROM memory is used to permanently save the program
being executed.
This is why it is often called program memory.
Since this ROM is made with FLASH technology, its contents
can be changed by providing a special programming voltage
EEPROM Memory
Similar to program memory, the contents of EEPROM is
permanently saved, even the power goes off. However,
unlike ROM, the contents of the EEPROM can be changed
during operation of the microcontroller.
That is why this memory is a perfect one for permanently
saving results created and used during the operation.
Pg 14

2.1.3 Explain the data RAM file Register


RAM Memory
This is the third and the most complex
part of microcontroller memory. In this
case, it consists of two parts: generalpurpose registers (GPR) and specialfunction registers (SFR).
Even though both groups of registers
are cleared when power goes off and
even though they are manufactured in
the same way and act in the similar
way, their functions do not have many
things in common.
Pg 15

2.1.3 Explain the data RAM file Register:


a. WREG register
In the CPU, registers are used to store information
temporarily.
PIC C have many register for arithmetic and logic
operations. Among them is the WREG Register.
The vast majority of PIC registers are 8-bit
registers. In the PIC there is only one data type: 8bit.

Pg 16

2.1.3 Explain the data RAM file Register:


a. WREG register
With an 8-bit data type, any data larger than 8
bits must be broken into 8-bit chunks before it is
processed.

Pg 17

2.1.3 Explain the data RAM file Register:


a. WREG register
The 8-bit WREG register is the most widely used
register in the PIC micro controller.
WREG stands for working register, as there is only
one. The WREG register is the same as the
accumulator in other microprocessors.
The WREG register is used for all arithmetic and
logic instructions. To understand the use of the
WREG register, we will show it in the context of
two simple instructions: MOVLW and ADDWL.

Pg 18

2.1.3 Explain the data RAM file Register:


b. File register (SFRs and GPR)
Special Function Register (SFR)
Special-Function registers are also RAM memory
locations, their purpose is predetermined during
manufacturing process and cannot be changed.
Since their bits are physically connected to
particular circuits on the chip (A/D converter,
serial communication module, etc.), any change of
their contents directly affects the operation of the
microcontroller or some of its circuits.

Slide 15

Pg 19

2.1.3 Explain the data RAM file Register:


b. File register (SFRs and GPR)
General Purpose Register (GPR)
General-Purpose registers are used for storing
temporary data and results created during
operation.
..it is necessary to specify the address of some
general purpose register and assign it a new
function.

Pg 20

2.1.3 Explain the data RAM file Register:


b. File register (SFRs and GPR)

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 45


Pg 21

2.1.3 Explain the data RAM file Register:


b. File register (SFRs and GPRs)
SFR and GPR
For PIC18 family have start from 256
bytes and up to maximum 4096 (4K)
bytes file register. Address vary 000FFF H.
All 16 Banks. One bank have 256
bytes.

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 46


Pg 22

2.1.3 Explain the data RAM file Register:


b. File register (SFRs and GPRs)

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 33


Pg 23

2.1.3 Explain the data RAM file Register:


c. Status Register
One of the most important register in PIC. Most of
PIC have this register.
8-Bit register. Sometime referred as flag register.
Only 5 bit used

PIC18 Microcontroller and Embedded


Systems using Assembly & C: Pg. 57

Pg 24

2.1.3 Explain the data RAM file Register:


c. Status Register
C, Carry flag (carry bit)
This flag is set whenever there are carry out
from the D7 bit.
Affected after an addition or subtraction.

Pg 25

2.1.3 Explain the data RAM file Register:


c. Status Register
DC, Digital Carry flag
If there is a carry from D3 to D4 during ADD
or SUB operation, this bit is set ( 1 )
otherwise it is cleared ( 0 ).
Usually to perform BCD arithmetic.

Pg 26

2.1.3 Explain the data RAM file Register:


c. Status Register
Z, Zero flag
Result of an arithmetic or logic operation.
If result operation is zero, Z-Bit = 1.
Otherwise it is Z = 0

Pg 27

2.1.3 Explain the data RAM file Register:


c. Status Register
OV, the overflow flag.
Is set (1) when the result of a signed number
operation is too large.
In general, C used to detect errors in
unsigned arithmetic operation.
In general, OV used to detect errors in
signed arithmetic operation.

Pg 28

2.1.3 Explain the data RAM file Register:


c. Status Register
N, the negative flag.
Binary representation of signed numbers
uses D7 as the signed bit.
Reflects as the result of an arithmetic
operation.
If D7 result is 0, then N-flag if 0.
If D7 result is 1, then N-flag is 1.

Pg 29

2.1.3 Explain the data RAM file Register:


d. Data format and directives
PIC data type.
Has only one data type. 8-bits. Size of register
also 8-bits ( 00-FF, or 0-255)
Data format representation.
Hex,
Binary,
Decimal,
ASCII
PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 61
Pg 30

2.1.3 Explain the data RAM file Register:


d. Data format and directives
HEX.
Use h, or H right after the number. Example
MOVLW 12H
Put 0x. Example MOVLW 0x12;
Put nothing. Eg. MOVLW 12
Put h in front of number. MOVLW h12

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 61


Pg 31

2.1.3 Explain the data RAM file Register:


d. Data format and directives
Binary
Only one way to represent binary numbers,
Example: MOVLW B00010010
1

in hex

Decimal.
Two ways to represent. MOVLW D12 , (in hex
0x0C)
Or MOVLW .12
PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 62
Pg 32

2.1.3 Explain the data RAM file Register:


d. Data format and directives
ASCII.
MOVLW A@. Or in hex

**PLEASE search the ASCII Table on the GOOGLE

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 63


Pg 33

2.1.3 Explain the data RAM file Register:


d. Data format and directives
ASSEMBLER Directive
Directive give direction to he assembler..
Example EQU, ORG, END, #INCLUDE, LIST,
_CONFIG
EQU(equate)
Used to define constant or fixed address.
Associates a constant number / address label
Eg.
COUNT EQU
0x25
MOVLW

PIC18 Microcontroller and Embedded Systems using Assembly & C: Pg. 65


COUNT
Pg 34

2.1.3 Explain the data RAM file Register:


d. Data format and directives
ORG
Indicate the beginning of the address.
Example ORG 0x00
END
important to indicate End of code, last line

Pg 35

2.1.3 Explain the data RAM file Register:


d. Data format and directives
LIST
Indicates to the assembler the specific PIC chip for which
the program should be assembled. Eg. LIST P=18F458
#INLCUDE
Tell the assembler to use the libraries associated with
the specific chip
_CONFIG
To configure bits for the targeted PIC. Read during the
power-up, wrong configuration makes PIC unusable.
Pg 36

2.1.3 Explain the data RAM file Register:


d. Data format and directives
Example code:

Pg 37

2.1.3 Explain the data RAM file Register:


Example on WREG..
MOVLW Instruction
The MOVLW instruction moves 8-bit data into the WREG register. It
has the following format:
MOVLW

;move literal value K into WREG

K is an 8-bit value that can range from 0-255 in decimal, or 00-FF in


hex.
The L stands for literal, which means, literally, a number must be
used.
In other words, if we see the word literal in any instruction, we are
dealing with an actual value that must be provided right there with
the instruction.
Pg 38

2.1.3 Explain the data RAM file Register:


Example on WREG..
MOVLW Instruction
Notice that in MOVLW, the letter L (literal) comes
first and then the letter W (WREG), which means
"move a literal value to WREG, " the destination.
The following instruction loads the WREG register
with a literal value of 25H (i.e., 25 in hex).
MOVLW 25H
(WREG = 25H)

; move value 25H into WREG

Pg 39

2.1.3 Explain the data RAM file Register:


Example on WREG..
ADDLW Instruction
The ADDLW instruction has the following format:
ADDLW

; ADD literal value K to WREG

The ADD instruction tells the CPU to add the literal


value K to register WREG and put the result back in the
WREG register. Notice that in ADDLW, first comes the
letter L (literal) and then the letter W (WREG), which
means "add a literal value to WREG," the destination.
Pg 40

2.1.3 Explain the data RAM file Register:


Example on WREG..
ADDLW Instruction
To add two numbers such as 25H and 34H, one can do
the following:
MOVLW

25H

; load 25H into WREG

ADDLW
34H)

34H

; add value 34 to W (W = W +

Executing the above lines results in WREG = 59H (25H


+ 34H = 59H)
Pg 41

2.1.3 Explain the data RAM file Register:


Example on WREG..
When programming the WREG register of the PIC
microcontroller with a literal value, the following points
should be noted:
Values can be loaded directly into the WREG register. There
is no need for a preceding pound sign or dollar sign to
indicate that a value is an immediate value as is the case
with some other microcontrollers.
If values 0 to F are moved into an 8-bit register such as
WREG, the rest of the bits are assumed to be all zeros. For
example, in "MOVLW 5H" the result will be WREG = 05H;
that is, WREG = 00000101 in binary.
Pg 42

2.1.3 Explain the data RAM file Register:


Example on WREG..
Moving a value larger than 255 (FF in hex) into the
WREG register will truncate the upper byte and
cause a warning in the .err file.

Pg 43

TO BE CONTINUED..

Pg 44

2.2 Know PIC Assembly Language


fundamental
2.2.1 Describe the instruction set of PIC18
2.2.2 Code assembly language using PIC18 instruction set
2.2.3 Identify assembling and linking process in PIC18
Program
2.2.4 Assemble, debug and execute program code using
MPLAB
2.2.5 Explain the role of Program Counter and ROM Memory
map in PIC18
2.2.6 Apply to placing code in program ROM
2.2.7 Examine MPLAB Simulator to view register and
memory content
Pg 45

2.2 Know PIC Assembly Language fundamental


2.2.1 Describe the instruction set of PIC18
PIC18F2455/2550/4455/4550 devices incorporate the
standard set of 75 PIC18 core instructions.
The instruction set is highly orthogonal and is grouped
into four basic categories:
Byte-oriented

operations
Bit-oriented operations
Literal operations
Control operations

Pg 46

2.2.1 Describe the instruction set of


PIC18
An Assembly language instruction consists of four fields:
[label] mnemonic [operands]
[; comment]

Pg 47

2.2.1 Describe the instruction set of


PIC18
An Assembly language instruction consists of four fields:
[label] mnemonic [operands]
[; comment]
label
We can name it freely but must follow certain rules:
Cannot start with number,
Cannot start with certain symbols &,*,@ ect,
Usually start with character..

Pg 48

2.2.1 Describe the instruction set of


PIC18
An Assembly language instruction consists of four fields:
[label] mnemonic [operands]
[; comment]
mnemonic
Is the instruction dedicated for the CPU, such as MOVLW,
MOVWF, ADDLW, etc..
Work with operands.
Eg. MOVLW
0x55. means move literal 55h into working
register.
Also known as OPCODE (Operation Code)
Pg 49

2.2.1 Describe the instruction set of


PIC18
An Assembly language instruction consists of four fields:
[label] mnemonic [operands]
[; comment]
Comments
Must begin with ;
Eg ;Ini Komen saya

Pg 50

2.2.1 Describe the instruction set of


PIC18

Datasheet PIC18F458

MOVWF
F is stands for file register. While W stand for working
register..
Tells the CPU to copy W file reg.
Eg. MOVWF PORTA ;Means copy 8-bit data in w into PORTA
file reg.
MOVLW
MOVWF

0x05 ; in binary 0000 0101


PORTA ; now PORTA = 0000 0101

*assume in PORTA have no data before..


*and address for PORTA is ?

Pg 51

2.2.1 Describe the instruction set of


PIC18
MOVWF
F is stands for file register. While W stand for working
register..
Tells the CPU to copy W file reg.
Eg. MOVWF PORTA ;Means copy 8-bit data in w into PORTA
file reg.
MOVLW
ADDLW
MOVWF

D10 ; in W = ??
D01
PORTA ; now PORTA = ??

*assume in PORTA have no data before..

Pg 52

2.2.1 Describe the instruction set of


PIC18
MOVWF

Pg 53

2.2.1 Describe the instruction set of


PIC18
MOVWF

Pg 54

2.2.1 Describe the instruction set of


PIC18
ADDWF
Group of arithmetic. Involve WREG & file Reg (eg PORTB,
etc..) any GPR or SFR
the OPCODE/mnemonic is
ADDWF
filereg,d
; filereg means SFR or GPR
(PORTB..etc)
; d is destination. means d = 0, the
; result will go back to WREG, if
; d = 1, the result will stored back into
; filereg
Pg 55

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 56

2.2.1 Describe the instruction set of


PIC18
ADDWF

Formula:
Destination = filereg + WREG
Pg 57

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 58

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 59

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 60

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 61

2.2.1 Describe the instruction set of


PIC18
ADDWF

Pg 62

2.2.1 Describe the instruction set of


PIC18

Refer Datasheet

Pg 63

2.2.1 Describe the instruction set of


PIC18

Refer Datasheet

Pg 64

2.2.1 Describe the instruction set of


PIC18
COMF
Means COMPLEMENT filereg.

Pg 65

2.2.1 Describe the instruction set of


PIC18
COMF
Means COMPLEMENT filereg.

Pg 66

2.2.1 Describe the instruction set of


PIC18
DECF
Means decrement filereg. by 1.

Formula:
Destination = filereg - 1

Pg 67

2.2.1 Describe the instruction set of


PIC18
DECF
Means decrement filereg. by 1.

Formula:
Destination = filereg - 1

Pg 68

2.2.1 Describe the instruction set of


PIC18
INCF
Means increment filereg. by 1.
MOVLW
MOVWF
INCF

0x02 ; Load WREG with 0x02, W=02


SUM ; SUM = 02
SUM,F ; SUM = SUM +1, SUM = 3

Formula:
Destination = filereg + 1

Pg 69

2.2.1 Describe the instruction set of


PIC18
MOVF
To create instruction to perform MOVFW, means data from
filereg to WREG. If D = 0, the destination is WREG.

Pg 70

2.2.1 Describe the instruction set of


PIC18
MOVF
To create instruction to perform MOVFW, means data from
filereg to WREG. If D = 0, the destination is WREG.

Pg 71

2.2.1 Describe the instruction set of


PIC18
MOVFF
Move data from filereg1 to filereg2.

Pg 72

2.2.1 Describe the instruction set of


PIC18
Byte Oriented
Operations

MOVWF
ADDWF
COMF
DECF & INCF
MOVF
MOVFF

Literal Operations
MOVLW
ADDLW

Pg 73

2.2.2 Code assembly language using


PIC18 instruction set

Pg 74

2.2.2 Code assembly language using


PIC18 instruction set

Pg 75

2.2.3 Identify assembling and linking


process in PIC18 Program
First we use a text editor to type in a
program. PIC microcontrollers, we use
the MPLAB IDE, which has a text editor,
assembler, linker, simulator, and much
more all in one software package.
Source file has the extension "asm". The
"asm" extension for the source file is
used by an assembler in the next step.
The assembler converts the instructions
into machine code. The assembler will
produce an object file and an error file.
Pg 76

2.2.3 Identify assembling and linking


process in PIC18 Program
The extension for the object file is "0". The
extension for the error file, which contains
any syntax errors and their line numbers, is
"err".
Assemblers require a third step called
linking. The link program takes one or more
object files and produces a hex file, a list file,
a map file, an intermediate object file, and a
debug file.
After a successful link, the hex file is ready
to be burned into the PIC's program ROM
and is downloaded into the PIC Trainers

Pg 77

2.2.3 Identify assembling and linking


process in PIC18 Program
PIC assembler provides us the
error file with the extension of "err"
and this is the file we examine to
see the nature of syntax errors.
The 1st (list) and map files are
very useful to the programmer.
The list shows the binary and
source code. The map file shows
the memory layout of used and
unused memory locations.

Pg 78

2.2.4 Assemble, debug and execute


program code using MPLAB
Practical Work 1.

Pg 79

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
Another important register is PC microcontroller.
Used by CPU to point to the address of the next
instruction to be executed.
As CPU fetches the OPCODE from the program ROM, the
program counter is incremented automatically.
the wider the PC, more the memory locations a CPU can
be accessed.
0000
OPCODE here

14-bit counter can accessed 16K of code.


214 -1 = 16363 = 3FFF

3FFF
Pg 80

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
16-bit counter can accessed 16K of code.
216 1 = 65535

0000
OPCODE here
FFFF

00000

21-bit counter can accessed 16K of code.


OPCODE here

221 1 = 2097151 = 2M
1FFFF
F

Pg 81

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
ROM memory map in the PIC18F
Must be noted the first location of the ROM is 0x00
Last location depend on the size ROM on chip
PIC18 Microcontroller and Embedded Systems
using Assembly & C: Pg. 74

Pg 82

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
ROM memory map in the PIC18F
Must be noted the first location of the ROM is 0x00
Last location depend on the size ROM on chip
PIC18 Microcontroller and Embedded Systems
using Assembly & C: Pg. 74

Pg 83

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
18F series has 21-bit PC (program
counter), but depend on the size ROMon-chip PIC. It can varies from 4K to 2M
ram.
PIC18 Microcontroller and Embedded Systems
using Assembly & C: Pg. 73

Pg 84

2.2.5 Explain the role of Program Counter


and ROM Memory map in PIC18
Remember, when PIC start, or powerup it goes to address 0x00 first.
Mean, PC has the value of 0x00.
Thats why it expected the first
OPCODE must be burn at 0x00.
And we must use ORG 0x00 at the
first place.

PIC18 Microcontroller and Embedded Systems


using Assembly & C: Pg. 73
Pg 85

2.2.6 Apply to placing code in program


ROM
Usually after process of build in
MPLAB, it will generate the lst (List
file).
The OPCODE & OPERAND can be
seen at left hand side.
After we burn the code in
PIC18F452/458, the OPCODE &
OPERAND started at address 0x00.
Noticed, all instruction (OPCODE
AND OPERAND) consist 2-byte
instructions.

address

data (opcode & operand)


Pg 86

2.2.6 Apply to placing code in program


ROM
For PIC18F, internal data bus is 16bit size, thats why we see almost
instructions is 2-bytes size.
Every instruction is fetch by the
CPU, it will take 1 instruction cycle.

16-bit
size data
bus

Pg 87

2.2.6 Apply to placing code in program ROM

Von Neumann
Architecture

Harvard Architecture

The computer has


single storage
system(memory) for
storing data as well as
program to be
executed.

The computer has two


separate memories for
storing data and
program.

Processor needs two clock


cycles to complete an
instruction.

Processor can complete an


instruction in one cycle.

Need a set of address bus


and a set of data bus.

Need 2 set address bus for


ROM and RAM, and 2 sets
data bus for ROM and RAM.

Pg 88

2.2.6 Apply to placing code in program ROM


(Instructions size of the PIC18)
Recall back, the instructions size of PIC18F is 16-bit,
which is 8-bit for OPCODE and 8-bit for OPERAND.
address

MOVLW
2-byte instruction.
MOVLW = 0E 1-byte
k = any literal number 1-byte

data (opcode & operand)


Pg 89

2.2.6 Apply to placing code in program ROM


(Instructions size of the PIC18)
ADDLW

address

2-byte instruction.
ADDLW = 0F 1-byte
k = any literal number 1-byte
MOVWF
2-byte instruction.
MOVWF= 6E 1-byte
k = any literal number 1-byte

data (opcode & operand)


Pg 90

2.2.7 Examine MPLAB Simulator to view


register and memory content

Pg 91

2.3 Understand arithmetic and logic


Instruction
2.3.1 Describe Arithmetic Instruction and Operation
2.3.2 Illustrate code in Assembly language for arithmetic
operation
a. Addition and subtraction
b. Multiplication and division
c. Addition of BCD data
2.3.3 Describe logic instruction and bit manipulation
2.3.4 Code PIC rotates instruction and data serialization.
2.3.5 Illustrate code logic for bit manipulation
Pg 92

2.3.1 Describe Arithmetic Instruction and


Operation
Involve the addition, subtraction, multiplication, and
division.
Addition, ADDLW, ADDWF, ADDWFC..
Subtraction, SUBLW, SUBWF, SUBWFB..
Multiplication, MULLW
Division, no specific instruction..

Pg 93

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
. ADDLW & ADDWF can be reviewed on slide 40 and 55.

Chapter 5: page 156


Pg 94

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction

Pg 95

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction

Pg 96

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
SUBLW
Means L-WREG. And the result is placed in WREG. W=L-W

Pg 97

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
SUBLW
Means L-WREG. And the result is placed in WREG.

Pg 98

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
SUBLW
N=1 (or C=0) result is negative.
N=0 (or C=1) result is positive.
Normally result is left in 2s complement.
We can use NEGF to convert it back!!

Pg 99

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
SUBWF[ destination = filereg WREG ]

Pg 100

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction

Pg 101

Page 162

2.3.2 Illustrate code in Assembly


language for arithmetic operation
a. Addition and subtraction
SUBWFB

[ destination = filereg W borrow ]

SUBFWB

[ destination = W - filereg borrow ]

Pg 102

2.3.2 Illustrate code in Assembly


language for arithmetic operation
b. Multiplication and division
MULLW

;[Multiply literal with WREG]

The PIC supports byte-by-byte multiplication only. The


bytes are assumed to be unsigned data. The syntax is as
follows:
MULLW K
;W x K and 16-bit is result is in
PRODH:PRODL
In byte-by-byte multiplication, one of the operands must
be in the WREG register, and the second operand must be a

Pg 103

2.3.2 Illustrate code in Assembly


language for arithmetic operation
b. Multiplication and division
MULLW

Data sheet
PIC18F458 pg 9

;[Multiply literal with WREG]

After multiplication, the result is in the special function


registers PRODH and PRODL; the lower byte is in PRODL,
and the upper byte is in PRODH.
Example:

Pg 104

2.3.2 Illustrate code in Assembly


language for arithmetic operation
b. Multiplication and division
Division.
We can write a program to perform division by repeated
subtraction. In dividing a byte by a byte, the numerator is
placed in a fileReg and the denominator is subtracted
from it repeatedly.
The quotient is the number of times we subtracted and
the remainder is in fileReg upon completion. See the
following example.

Pg 105

2.3.2 Illustrate code in Assembly


language for arithmetic operation
b. Multiplication and division
Division.

Pg 106

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
BCD stands for binary coded decimal. BCD is needed
because in everyday life we use the digits 0 to 9 for
numbers, not binary or hex numbers.
Binary representation of 0 to 9 is called BCD . In
computer literature, one encounters two terms for BCD
numbers: (i) unpacked BCD, and (ii) packed BCD.

Pg 107

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
In unpacked BCD, the lower 4 bits of the number represent the
BCD number, and the rest of the bits are O. Example: "0000 100 I"
and "0000 0101" are unpacked BCD for 9 and 5, respectively.
Unpacked BCD requires I byte of memory, or an S-bit register, to
contain it.
In packed BCD, a single byte has two BCD numbers in it: one in the
lower 4 bits, and one in the upper 4 bits. For example, "0101 1001"
is packed BCD for 59H. Only I byte of memory is needed to store the
packed BCD operands. One reason to use packed BCD is that it is
twice as efficient in storing data.
Pg 108

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
There is a problem with adding BCD numbers, which must
be corrected. The problem is that after adding packed BCD
numbers, the result is no longerBCD. Look at the following.
MOVLW Ox17
ADDLW Ox28
Adding these two numbers gives 0011 IIII B (3FH), which is
not BCD. A BCD number can only have digits from 0000 to
1001 (or 0 to 9).
Pg 109

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
The result above should have been 17 + 28 = 45 (01000101). To correct
this problem, the programmer must add 6 (0 II 0) to the low digit: 3F +
06 = 45H.
The same problem could have happened in the upper digit (for example,
in 52H + 87H = D9H). Again, 6 must be added to the upper digit (D9H +
60H = 139H) to ensure that the result is BCD (52 + 87 = 139).
This problem is so pervasive that most microprocessors such as the PIC
18 have an instruction to deal with it. In the PIC 18 instruction "DAW" is
designed to correct the BCD addition problem.
Pg 110

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
The DAW (decimal adjust WREG) instruction in the PIC 18
is provided to correct the aforementioned problem
associated with BCD addition.
The mnemonic "DAW" works only with an operand in the
WREG register. The DAW instruction will add 6 to the
lower nibble or higher nibble if needed; otherwise, it will
leave the result alone. The following example will clarify
these points
Pg 111

2.3.2 Illustrate code in Assembly


language for arithmetic operation
c. Addition of BCD data
MOVLW
ADDLW
DAW

0x47
0x25

;
; WREG = 6C
; WREG = 72

Pg 112

2.3.3 Describe logic instruction and bit


manipulation
Apart from I/O and arithmetic instructions, logic
instructions are some of most widely used instructions.
Boolean logic instructions such as AND, OR, Exclusive-OR
(XOR), and complement.
Logical operations are useful for looking for array
elements with certain properties (e.g., divisible by power
of 2) and manipulating I/O pin values (e.g., set certain
pins to high, clear a few pins, toggle a few signals, and so
on).
Pg 113

2.3.3 Describe logic instruction and bit


manipulation

Pg 114

2.3.3 Describe logic instruction and bit


manipulation

Pg 115

2.3.3 Describe logic instruction and bit


manipulation

Pg 116

2.3.3 Describe logic instruction and bit


manipulation

Pg 117

2.3.4 Code PIC rotates instruction and


data serialization.

Pg 118

Thank you..

hafriz.fikrie@gmail.com

Pg 119

Potrebbero piacerti anche