Sei sulla pagina 1di 57

UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 1

COE 115
Lecture 3
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 2

Indirect Addressing
MOV with indirect Addressing:
mov{.b} [Wso], [Wdo] ((Wso)) → (Wdo)

[] (brackets) indicate indirect addressing.


Source Effective Address (EAs) is the content of Wso, or (Wso). Destination
Effective Address (EAd) is the content of Wdo, or (Wdo).

The MOV instruction copies the content of the Source Effective Address to
the Destination Effect Address, or:
(EAs) → EAd
which is:
((Wso)) → (Wdo)
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 3

Indirect Addressing
MOV Example

Indirect Addressing
MOV E Example
ample
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 4

Why Indirect Addressing?


The instruction:
mov [W0], [W1]
Allows us to do a memory-memory copy with one instruction!

The following is illegal:


mov 0x1000, 0x1002

Instead, would have to do:


mov 0x1000, W0
mov W0, 0x1002
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 5

Indirect Addressing Coverage


• There are six forms of indirect addressing
• The need for indirect addressing makes the most sense when
covered in the context of C pointers
• register indirect the simplest form of indirect addressing, which is
as shown on the previous slides.
• Most instructions that support register direct for an operand, also
support indirect addressing as well for the same operand
• However, must check PIC24 datasheet and book to confirm.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 6

ADD {.B} Wb, Ws, Wd Instruction


• Three operand addition, register-to-register form:
ADD{.B} Wb, Ws, Wd (Wb) + (Ws) → Wd
Wb, Ws, Wd are any of the 16 working registers W0-W15

ADD W0, W1, W2 (W0) + (W1) → W2


ADD W2, W2, W2 W2 = W2 + W2 = W2*2

ADD.B W0, W1, W2 Lower 8 bits of W0, W1 are added


and placed in the lower 8 bits of W2
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 7

ADD {.B} Wb,ADD{.B}


Ws, Wd Wb,
Instruction
Ws, Wd Execution
( ) Execute:
(a) E t add
dd W0
W0,W1,W2
W1 W2
W0 0x1AF3 0x1AF3 W0 0x1AF3
W1 0x8B1A + 0x8B1A W1 0x8B1A
W2 0x64DE W2 0xA60D Modified
W3 0xFB90 0xA60D W3 0xFB90
ADD{.B} Wb, Ws, WdBefore
Execution After
( ) Execute:
(a) E t add
dd W0
W0,W1,W2
W1 W2
W0 0x1AF3 0x1AF3 W0 0x1AF3
W1 0x8B1A (b) Execute:
+ 0x8B1A
add.b
W1 0x8B1A
W0,W1,W2
W2 0x64DE W2 0xA60D Modified
W3 0xFB90 W0 W3
0xA60D 0x1A F3
0xFB90 0xF3 W0 0x1A23
Before W1 0x8BAfter
1A + 0x1A W1 0x8B1A
(b) Execute: add.b W0,W1,W2 W2 0x64DE W2 0x64 0D Modified
0xF3 W3 0 FB90
0xFB90 0x0D W3 0 FB90
0xFB90
W0 0x1A F3 W0 0x1A23
W1 0x8B 1A + 0x1A W1 0x8B1A Result limited
W2 0x64DE Before
W2 0x64 0D to 8-bits!
Modified After
W3 0 FB90
0xFB90 0x0D W3 00xFB90
FB90
Result limited
Before (c) Execute:
to 8-bits! add W2,W2,W2
After

(c) Execute: add W2,W2,W2


W0 0x1AF3 0x64DE W0 0x1AF3
W0 0x1AF3 0x64DE W0 0x1AF3
W1 0x8B1A W1
+ 0x64DE
0x8B1A
W1 0x8B1A + 0x64DE W1 0x8B1A
W2 0x64DE
0xC9BC
W2 0x64DE
W2 0xC9BC Modified W2 0xC9BC Modified
W3 0xFB90
W3
W3 0xFB90
0xFB90 0xC9BC W3 0xFB90
Before After
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
V 0.2 Before 36 After
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 8

SUB{.B} Wb, Ws, Wd Instruction


Three operand subtraction, register-to-register form:
SUB{.B} Wb, Ws, Wd (Wb) – (Ws) → Wd
Wb, Ws, Wd are any of the 16 working registers W0-W15.

Be careful:
while ADD Wx, Wy, Wz gives the same result as ADD Wy, Wx, Wz

The same is not true for


SUB Wx, Wy, Wz versus SUB Wy, Wx, Wz
SUB W0, W1, W2 (W0) – (W1) → W2
SUB W1,W0, W2 (W1) – (W0) → W2

SUB.B W0, W1, W2 Lower 8 bits of W0, W1 are subtracted and placed in the
lower 8-bits of W2
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 9

SUB{.B} Wb, Ws, Wd Wb,


SUB{.B} Instruction
Ws, WdExecution
Execution
SUB{.B} Wb, Ws, Wd Execution

Copyright Delmar Cengage Learning 2008. All Rights Reserved.


V 0.9 38
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 10

Subtraction/Addition with Literals


• Three operand addition/subtraction with literals:
ADD{.B} Wb, #lit5, Wd (Wb) – #lit5 → Wd
SUB{.B} Wb, #lit5, Wd (Wb) – #lit5 → Wd

#lit5 is a 5-bit unsigned literal; the range 0-31. Provides a convenient


method of adding/subtracting a small constant using a single instruction
Examples

ADD W0,#4,W2 (W0)+4 →W2


SUB.B W1,#8, W3 (W1) – 8 → W3
ADD W0, #60, W1 illegal, 60 is greater than 31!
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 11

ADD {.B} f {,WREG} Instruction


Two operand addition form:
ADD{.B} f (f) + (WREG) → f
ADD{.B} f, WREG (f) + (WREG) → WREG

WREG is W0, f is limited to first 8192 bytes of memory.


One of the operands, either f or WREG is always destroyed!

ADD 0x1000 (0x1000) + (WREG) → 0x1000


ADD 0x1000,WREG (0x1000) + (WREG) → WREG
ADD.B 0x1001, WREG (0x1001) + (WREG.lsb) → WREG.lsb
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 12

Assembly Language Efficiency


• The effects of the following instruction:
ADD 0x1000 (0x1000) + (WREG) → 0x1000

Can also be accomplished by:

MOV 0x1000 , W1 (0x1000) → W1


ADD W0, W1, W1 (W0) + (W1) → W1
MOV W1, 0x1000 (W1) → 0x1000

This takes three instructions and an extra register. However, in this class we
are only concerned with the correctness of your assembly language, and
not the efficiency. Use whatever approach you best understand!!!!!
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 13

ADD {.B} f {,WREG}


ADD{.B}Instruction
f {,WREG} Execution
Execution

V 0.2 42
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
V 0.2 42
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 14

SUB{.B} f {,WREG} Instruction


• Two operand subtraction form:
SUB{.B} f (f) – (WREG) → f
SUB{.B} f, WREG (f) – (WREG) → WREG

WREG is W0, f is limited to first 8192 bytes of memory.


One of the operands, either f or WREG is always destroyed!

SUB 0x1000 (0x1000) – (WREG) → 0x1000


SUB 0x1000,WREG (0x1000) – (WREG) → WREG
SUB.B 0x1001, WREG (0x1001) – (WREG.lsb) → WREG.lsb
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 15

Increment
Increment operation, register-to-register form:
INC{.B} Ws, Wd (Ws) +1 → Wd
Increment operation, memory to memory/WREG form:
INC{.B} f (f)+1→f
INC{.B} f, WREG (f) + 1 → WREG
(f must be in first 8192 locations of data memory)
Examples:
INC W2, W4 (W2) + 1 → W4
INC.B W3,W3 (W3.lsb)+1→W3.lsb
INC 0x1000 (0x1000) +1 → 0x1000
INC.B 0x1001,WREG (0x1001)+1 → WREG.lsb
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 16

Decrement
Decrement operation, register-to-register form:
DEC{.B} Ws, Wd (Ws) – 1 → Wd
Increment operation, memory to memory/WREG form:
DEC{.B} f (f) –1→f
DEC{.B} f, WREG (f) – 1 → WREG
(f must be in first 8192 locations of data memory)

Examples:
DEC W2, W4 (W2) – 1 → W4
DEC.B W3, W3 (W3.LSB) – 1 → W3.LSB
DEC 0x1000 (0x1000) – 1→ 0x1000
DEC.B 0x1001,WREG (0x1001) – 1 → WREG.LSB
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 17

How is the
Howinstruction loaded? register loaded?
is the instruction
D M
Data Mem
Program Counter 24
Inst. Reg
23 16 address
D t
Data
16
address
Program Memory,
16 x 16
non-volatile,
non volatile, up to 16
Working
ki
4M words (4M x 24)
Reg array 16
DOUT
16
16
The Program counter contains the program
memory address of the instruction that will be ALU
loaded into the instruction register
g . After
reset, the first instruction fetched from location
16
0x000000 in program memory, i.e., the
program counter is reset to 0x000000. 17 x 17 Multiplier
p
not shown
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 18

Program Memory
ProgramOrganization
Memory Organization

An instruction is 24 bits ((3 bytes).


y ) Program
g memory
y should be
An instruction is 24 bits (3 bytes). Program memory should be viewed as
viewed as words (16-bit addressable), with the upper byte of
words (16-bit addressable), with the upper byte of the upper word of an
the upper word of an instruction always reading as ‘0’.
instruction always
Instructions reading
must aseven-word
start on ‘0’. Instructions must start
boundaries.
boundaries on even-word
Instructions
boundaries. Instructions
are addressed are addressed
by the Program counterby the Program counter (PC).
(PC).
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
V 0.2 Figure adapted with permission of the copyright owner, Microchip 47
V 0.2 42 Technology, Incorporated. All rights reserved.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 19

Goto location (goto)


Goto location (goto)
How can the program counter be changed?

A GOTO iinstruction
t ti iis an unconditional
diti l jump.
j
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
Copyright
Copyright Delmar Cengage Learning Delmar
2008. All Rights Cengage
Reserved. Learning 2008. All Rights Reserved.
V 0.2
V 0.2 42
48
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 20

Valid addressing
Validmodes
addressing modes.
• What
Whatare
arevalid
validaddressing
addressingmodes
modesfor for
instructions?
instructions
Complete
The information
definitive can be
answer can be found
found in Table
table 19-2
19-2ofofthe
thePIC24H32GP202
datasheet.
PIC24H32GP202
ADD{.B} Wb, Ws, datasheet.
datasheet
Wd Execution
( ) Execute:
(a) E t add
dd W0
W0,W1,W2
W1 W2
W0 0x1AF3 0x1AF3 W0 0x1AF3
W1 0x8B1A + 0x8B1A W1 0x8B1A
W2 0x64DE W2 0xA60D Modified
W3 0xFB90 0xA60D W3 0xFB90
Before After

(b) Execute: add.b W0,W1,W2


W0 0x1A F3 0xF3 W0 0x1A23
W1 0x8B 1A + 0x1A W1 0x8B1A
W2 0x64DE W2 0x64 0D Modified
W3 0 FB90
0xFB90 0x0D W3 0 FB90
0xFB90
Result limited
Before to 8-bits! After

(c) Execute: add W2,W2,W2


W0 0x1AF3 0x64DE W0 0x1AF3
W1 0x8B1A + 0x64DE W1 0x8B1A
W2 0x64DE W2 0xC9BC Modified
W3 0xFB90 0xC9BC W3 0xFB90
Before After
Copyright Delmar Cengage Learning 2008. All Rights Reserved.
V 0.2 36
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 21

What does ‘Wso’, ‘Wsd’, ‘Wn’ etc. mean?


Wso, Wsd, Wn
MOV Wso, Wdo
• MOV Wso, Wdo
Table
bl 19-1:used
Symbols Symbols
b opcode
in l usedddescriptions
in
i opcoded descriptions
d i i (partial
( i l list)
li )
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 22

ADD forms
ADD forms
ADD
• ADD Wb, Ws, Wb, Ws, Wd
Wd

• Legal: Legal:
ADD W0,ADD
W1,W0,
W2W1, W2
ADD W0,ADD W0,[W4]
[W1], [W1], [W4]

• Illegal Illegal:
Ill l
ADD [W0],W1,W2 ;first operand illegal!
ADD [W0],W1,W2 ;first operand illegal!

V 0.2 51
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 23

Simple Program (example)


A Simple Program
• Sample programs written in C, translated (compiled) to PIC 24uC
assemblyInlanguage
this class, will present programs in C form, then translate
(compile) them to PIC24 μC assembly language.
C Program equivalent A uint8 variable is
#define avalue 100 8 bits (1 byte)
uint8
u t8 i,j,k;
,j, ;

i = avalue; // i = 100
i = i + 1; // i++, i = 101
j = i; // j is 101
j = j - 1; // j--, j is 100
k = j + i; // k = 201

V 0.2 53
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 24

Where are variables stored?


When writing assembly language, can use any free data memory location
to store values, it your choice.

A logical place to begin storing data in the first free location in data
memory, which is 0x0800 (Recall that 0x0000-0x07FF is reserved for SFRs).

Assign i to 0x0800, j to 0x0801, and k to 0x0802. Other choices could be


made.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 25

C to PIC24 Assembly
C to PIC24 Assembly

Comments:
Comments: The assembly
The assembly language
language program
program operation
operation is very
is not not very clear.
clear. Also, multiple
assembly language
Also, statements
multiple are needed
assembly language for oneare
statements C language
needed forstatement.
one C Assembly
languagelanguage
l is morestatement.
primitive
t t t (operations
Assembly
A l less powerful)
bl language is than
i itiC. (operations
i more primitive ( ti less
l
powerful) than C.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 26

PIC24 Assembly to PIC24 Machine Code


• Could perform this step manually by determining the instruction
format for each instruction from the data sheet.
• Much easier to let a program called an assembler do this step
automatically
• The MPLAB Integrated Design Environment (IDE) is used to
assemble PIC24 programs and simulate them
• Simulate means to execute the program without actually loading it into a
PIC24 microcontroller
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 27

mptst_byte.s
.include "p24Hxxxx.inc"
.global __reset
.bss
i:
;reserve space for variables
.space 1
mptst_byte.s
p _ y
j: .space 1
k: .space 1
.text ;Start of Code section
__reset: ; first instruction located at __reset label
mov #__SP_init,
# S i it W15 15 ;;initialize
i iti li stack
t k pointer
i t
mov #__SPLIM_init,W0
mov W0,SPLIM ;;initialize Stack limit reg.
avalue = 100
; i = 100; This file can be assembled
mov.b #avalue, W0 ; W0 = 100 by the MPLAB™
mov.b WREG,i ; i = 100
assembler into PIC24
; i = i + 1;
inc.b i ; i = i + 1 machine code and
; j = i simulated.
mov.b i,WREG ; W0 = i
mov.b WREG,j ; j = W0
Labels used for memory
; j = j – 1; locations 0x0800 (i),
dec b
dec.b j ; j= j – 1 0 0801(j) 0x0802(k)
0x0801(j), 0 0802(k) to
t
; k = j + i
mov.b i,WREG ; W0 = i increase code clarity
add.b j,WREG ; W0 = W0+j (WREG is W0)
mov.b WREG,k ; k = W0
done:
goto done ;loop forever
V 0.2 57
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 28

mptst_byte.s (cont.) Include file that defines


mptst
p _ byte.s
y (cont.)
( ) Include file that defines various labels
for a particular processor.
processor ‘.include
various labels include’ is
mptst
p _byte.s
y ((cont.)) for a particular processor.
.include "p24Hxxxx.inc"
an assembler
processor directive.
‘.include
include’ is
an assembler directive.
.include "p24Hxxxx.inc"
.global
g __reset Declare the __reset label as
Declare the __reset global
label– as
it is is needed by linker
.global
g __reset
for defining
global – it is is needed by linkerprogram start
.bss ;reserve spacefor
for variables
defining program start
i
i: .space
space 1
.bss ;reserve space for variables
j: .space 1 The .bss assembler directive
i
i: .space
space 1 indicates the following should be
j:
k:
.space 1
.space 1 The .bss assembler directive
k: .space 1
placedshould
indicates the following in databe
memory. By
An assembler directive isplaced default,By
not ain data memory. variables are placed
An assembler PIC24 is not a but andefault, variablesbeginning
instruction,
directive are placedat the first free
location,
beginning at the first free 0x800. The .space
instruction
PIC24 instruction, but an to the assembler assembler
bl didirective
ti reserves
location, 0x800. The .space
program.
instruction to the assemblerAssembler directives
assembler
bl di ti space
directive in bytes for the named
reserves
have a directives
program. Assembler leading ‘.’ period,space
and inarebytes forvariables.
the named i, j, k are labels, and
have a leadingnot case sensitive
sensitive.
‘.’ period, and are variables. i, j, klabels are case-sensitive
are labels, and and
must beand
labels are case-sensitive followed by a ‘:’ (colon).
not case sensitive
sensitive. V 0.2 58
must be followed by a ‘:’ (colon).
V 0.2 58
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 29

mptst_byte.s (cont.) (cont.)


mptst_byte.s
.text
__reset: mov #__SP_init, W15 ‘.text’ is an assembler directive that says
mov #__SPLIM_init,W0 what follows is code. Our first
mov W0,SPLIM
W0 SPLIM instruction must be labeled as ‘__reset’.
These move instruction initializes the
stack pointer and stack limit registers –
this will be discussed in a later chapter.

avalue = 100
Assembler directive that equates a label to a value
Could also be written as
The equal
avaluesign
= 100 is an assembler directive
that equates a label to a value.

V 0.2 59
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 30

mptst byte s (cont.)


mptst_byte.s
mptst_byte.s (cont.) (cont ) The use off labels
Th l b l andd
; i = 100;
comments greatly improves
mov.b #avalue, W0 ; W0 = 100 the clarityy of the pprogram.
g
mov.b WREG,i ; i = 100
It is hard to over-comment
an assembly language
; i = i + 1;
inc.b i ; i = i + 1 program if you want to be
; j = i able to understand it later.
mov.b
mov b i,WREG
i WREG ; W0 = i
mov.b WREG,j ; j = W0
Strive
S i for
f at lleast a
; j = j – 1; comment every other line;
dec.b j ; j= j – 1 refer to lines
; k = j + i
mov.b i,WREG ; W0 = i
add.b j,WREG ; W0 = W0+j (WREG is W0)
mov.b WREG,k ; k = W0

V 0.2 60
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 31

mptst_byte.s (cont.)
mptst
p _byte.s
y ((cont.)) A label that is the target
mptst
p _byte.s
y ((cont.)) of a goto instruction.
A label that is the target
Lablesof are caseinstruction.
a goto sensitive
done: (instruction
Lables mnemonics
are case sensitive
goto
done: done ;loop forever
and assembler directives
(instruction mnemonics
goto done ;loop forever
are not
andcase sensitive.
assembler directives
are not case sensitive.

.end A comment
.end A comment
An assembler directive specifying
p y g the end of
the program in this directive
An assembler file. specifying
p y g the end of
the program in this file.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 32

Alternate Solution An Alternate Solution


C Program equivalent ;Assign variables to regis
;Move variables into regis
#define avalue 100
;use register-to-register
uint8 i,j,k;
computations;
i = avalue; // i = 100
;write variables back to m
i = i + 1; // i++, i = 101
j = i;
; // j is 101 ;assign
; g i to W1,
, j to W2,
,
j = j - 1; // j--, j is 100
k = j + i; // k = 201
mov #100,W1 ; W1 (i
inc.b W1,W1 ; W1 (i
mo b W1,W2
mov.b W1 W2 ; W2 (j
Previous approach took 9 dec.b W2,W2 ; W2 (j
add.b W1,W2,W3 ; W3 (k
instructions, this one took ;;write variables to mem
;Move va
UP EEEI #define
CoE 115 Lecture avalue1s1920)
3 (Rosales 100 33 ;use reg
uint8 i,j,k;
computat
i = avalue; // i = 100
;write v

Alternate Solution
i = i + 1; // i++, i = 101

Alternate Solution j
j
k
=
=
=
i;
;
j - 1;
j + i;
//
//
//
j is 101
j--, j is 100
k = 201
;assign
; g

mov #1
inc.b
;Assign variables to registers
mo b
mov.b
;Move variables into registers.
;use register-to-register operations for
Previous approach took 9 dec.b
add.b
computations; instructions, this one took ;;writ

01
;write variables back to memory 11 instructions.
instructions Use mov.b
mov.b
;assign
; g i to W1,
, j to W2,
, k to W3 whatever approach that mov.b
mov.b
100 you best understand. mov.b
mov b
mov.b
mov #100,W1 ; W1 (i) = 100
inc.b W1,W1 ; W1 (i) = W1 (i) + 1
V 0.2
mo b W1,W2
mov.b W1 W2 ; W2 (j) = W1 (i)
9 dec.b W2,W2 ; W2 (j) = W2 (j) -1
add.b W1,W2,W3 ; W3 (k) = W1 (i) + W2 (j)
k ;;write variables to memory
mov.b W1,W0 ; W0 = i
mov.b WREG,i ; 0x800 (i) = W0
mov.b W2,W0 ; W0 = j
mov.b WREG,j ; 0x801 (j) = W0
mov.b W3,W0 ; W3 = k
mov b WREG,k
mov.b WREG k ; 0x802 (k) = W0
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 34

Clock Cycles vs. Instruction Cycles


The clock signal used by a PIC24 μC to control instruction execution can be generated by an off-chip oscillator or
crystal/capacitor network, or by using the internal RC oscillator within the PIC24 μC.

For the PIC24H family, the maximum clock frequency is 80 MHz.

An instruction cycle (FCY) is two clock (FOSC) cycles.

A PIC24 instruction takes 1 or 2 instruction (FCY) cycles, depending on the instruction (see Table 19-2,
PIC24HJ32GP202 data sheet). If an instruction causes the program counter to change (i.e, GOTO), that instruction
takes 2 instruction cycles.

An add instruction takes 1 instruction cycle.


How much time is this if the clock frequency (FOSC) is 80 MHz ( 1 MHz = 1.0e6 = 1,000,000 Hz)?

1/ frequency = period
1/80 MHz = 12 5 ns (1 ns = 1.0e-9 s)

1 Add instruction @ 80 MHz takes 2 clocks * 12.5 ns = 25 ns (or 0.025 us).

By comparison, an Intel Pentium add instruction @ 3 GHz takes 0.33 ns (330 ps). An Intel Pentium could emulate a
PIC24HJ32GP202 faster than a PIC24HJ32GP202 can execute! But you can’t put a Pentium in a toaster, or buy one
from Digi-key for $5.00.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 35

How long does mpstst_bytes.s take to execute


How long does mptst_byte.s take to execute?
• Beginning at the __reset label, and ignoring the goto at the end,
Beginning at the __reset label, and ignoring the goto at the end,
takes 12takes
instruction i cycles,
k 12 instruction
i l which
cycles, which i is
hi h is 24 24
l clock
clock l cycles.
k cycles.
Instruction
Cycles
mov #__SP_init, W15 1
mov #__SPLIM_init,W0 1
mov W0,SPLIM 1
mov.b #avalue, W0 1
mov.b WREG,i 1
inc b
inc.b i 1
mov.b i,WREG 1
mov.b WREG,j 1
d
dec.b
b j 1
mov.b i,WREG 1
add.b j,WREG 1
mov.b WREG,k 1
V 0.2 Total 12 65
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 36

What if we used 16-bit variables instead


What if we used
of16-bit
8-bitvariable instead of 8-bit variables?
variables?
C Program equivalent
A uint16 variable is
#define avalue 2047
16 bits (1 byte)
uint16 i,j,k;

i = avalue; // i = 2047
i = i + 1; // i++, i = 2048
j = i; // j is 2048
j = j - 1; // j--, j is 2047
k = j + i; // k = 4095

V 0.2 66
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 37
What if we used 16-bit variables instead
What if we usedof16-bit
8-bitvariable instead of 8-bit variables?
variables?
C Program equivalent
A uint16 variable is
#define avalue 2047
16 bits (1 byte)
uint16 i,j,k;

i = avalue; // i = 2047
i = i + 1; // i++, i = 2048
j = i; // j is 2048
j = j - 1; // j--, j is 2047
k = j + i; // k = 4095

V 0.2 66
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 38

.include "p24Hxxxx.inc"
.global __reset Reserve 2 bytes for each
.bss ;reserve space for variables variable. Variables are now
i:
.include .space 2
"p24Hxxxx.inc"
j
j: .space 2
.global __reset stored at2 0x0800,
Reserve 0x0800
bytes for 0x0802,
0x0802
each
k:
.bss .space 2
;reserve space for variables 0x0804 Variables are now
variable.
i:
.text .space 2 ;Start of Code section
j__reset: .space
j: 2 instruction located at __reset
; first
stored at 0x0800,
label
0x0800 0x0802,
0x0802
k: .space
SP i 2it w15
mov #__SP_init,
# 15 ;initialize
i iti li stack 0x0804
t k pointer
i t
.textmov #__SPLIM_init,W0 ;Start of Code section
mov W0,SPLIM
__reset: ; first instruction;initialize
located at stack limit
__reset reg
label
avalue
mov # SP = 2048
#__SP_init,
i it w15
15 ;initialize
i iti li stack
t k pointer
i t
; i mov #__SPLIM_init,W0
= 2048;
mov W0,SPLIM
mov #avalue, W0 ;initialize
; W0 = 2048 stack limit reg Instructions now
avalue = 2048
mov WREG,i
;; ii == 2048;
; i = 2048
perform WORD (16-bit)
mov
i + 1;
inc #avalue,
i W0 ;; W0i == i
2048
+ 1
Instructions
operations
p now.b
((the
mov
; j = i WREG,i ; i = 2048
perform
qualifierWORD (16-bit)
is removed).
; i =mov i + 1;i,WREG ; W0 = i
mov
inc i WREG,j ;; ij == iW0+ 1 operations
p ((the .b
; j
; j = i=
dec
mov
j – 1;
j
i,WREG ; j=
W0 j = –
i 1
qualifier is removed).
; k mov
= j + i WREG,j ; j = W0
; j =mov j – 1;i,WREG ; W0 = i
add
dec jj,WREG ;; j=
W0 j= –W0+j
1 (WREG is W0)
mov
; k = j + i WREG,k ; k = W0
done:mov i,WREG ; W0 = i
goto j,WREG
add done ;loop;forever
W0 = W0+j (WREG is W0)
V 0.2 67
mov WREG,k ; k = W0 Copyright Delmar Cengage Learning 2008. All Rights Reserved.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 39

An alternate
An Solution
An Alternate (16-bit
Solution
Alternate variables)
((16-bit
Solution variables)
((16-bit ) )
variables)
An Alternate Solution ((16-bit variables))
C Program
C Program equivalent
equivalent ;Assign;Assign
variablesvariables to registers
to registers
C Program equivalent ;Move
;Assign
;Move to
variables
variables
variables into registers.
into registers.
registers
#define #define avalue 2047
avalue 2047
;Move;use ;use register-to-register
register-to-register
variables into registers. operations
operations for for
#define uint16
uint16avalue
i,j,k; i,j,k;
2047 computations;operations for
computations;
;use register-to-register
uint16 i,j,k;i = avalue; // i = 2047
i = avalue; // i = 2047 computations;;write variables back to memory
i i= =avalue;i = i
// +
i 1;
= 2047 // i++, ;write variables back to memory
i = 2048
i + 1; // i++, i = 2048 ;write variables back to memory
+;1; j = //
i j= =i i; i;
; //
i++,j iis= // j is 2048 ;
2048
2048 ;assigng;;assign
i tog W1,i, to W1,
j to ,W2,
j,tok W2,
to, W3
k to W3
j = ;j - j1;= //
j = i; j -//
j 1;
j--, //
is 2048 j--,
j is j is g2047
;assign
;
2047 i to W1,
, j to W2, , k to W3
j = j - 1; k = //j +j--,
i; j is //2047
k = 4095
k = j + i; // //
k = j + i;
k = 4095
k = 4095 mov #2047,W1 ; W1 (i) = 2047
mov #2047,W1
mov #2047,W1 ; =W12047
(i) = 2047
inc; W1,W1
W1 (i) ; W1 (i) = W1 (i) + 1
inc W1,W1; W1 (i)
inc W1,W1 ; =W1W1(i) = W1 1 (i) + 1
mo W1,W2
mov W1 W2 ;(i)
W2 +(j) = W1 (i)
Previous
Previous approach
approach took 9took 9
Previous approach took 9
W1mov
moW2 W1,W2
mo W1,W2
mov W1 W2; W2 (j)
dec W2,W2
; W2 (j) = W1 (i)
dec W2,W2 = W1 ;(i)
; W2W2(j)
W2 (j) = W2 (j) -1
(j)=-1 W2 (j)
dec W2,W2 ; W2 (j)
add W1,W2,W3 =
; W3 (k) = W1-1(i) + W2 (j)
instructions, this
instructions, this one took
instructions, this one one took
took
add W1,W2,W3
;;write
;
add W1,W2,W3 W3 (k) = W1 (i)
; W3 (k) to
;;write variables
variables to memory
+
= W1 W2 (j)
(i) + W2 (j)
memory
;;write variables to memory
8 instructions.
instructions
8 instructions.
8 instructions.
instructions In thisIn this
instructions In this mov W1,i
mov W1,i
mov; W1,i
mov
0x800;(i)
; 0x800 (i) = W1
= W1(i) = W1
0x800
mov W2,j ; 0x802 (j) = ;W20x802 (j) = W2
W2,j
case, this approach is
case, this approach is
mov W2,j
mov W3,k mov; W3,k ; 0x802 (j) = W2
0x804 (k) = ;W30x804 (k) = W3
case, this approach is mov W3,k ; 0x804 (k) = W3
more efficient!
more efficient!
more efficient!
V 0.2 V 0.2 68 68
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 40

HowHow
longlong
doesdoes
mptst_word.s
mptst_word.stake
take to execute?
to execute?
Ignoring
g g the g
goto at the end,, takes 12 instruction cycles,
y , which
is 24 clock cycles. Instruction
Cycles
mov #__SP_init,
__ _ W15 1
mov #__SPLIM_init,W0 1
mov W0,SPLIM 1
mov #avalue, W0 1
mov WREG,i 1
inc i 1
mov i
i,WREG
WREG 1
mov WREG,j 1
dec j 1
mov i,WREG 1
add j,WREG 1
mov WREG,k 1
Total 12
V 0.2 69
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 41

16 bit operations vs. 8 bit operations


The 16-bit version of the mptst program requires the same number of
instruction bytes and the same number of instruction cycles as the 8-bit
version.

This is because the PIC24 family is a 16-bit microcontroller; its natural


operation size is 16 bits, so 16-bit operations are handled as efficiently as 8-
bits operations

On an 8-bit processor, like the PIC18 family, the 16-bit version would take
roughly double the number of instructions and clock cycles as the 8-bit version.

On the PIC24, a 32-bit version of the mptst program will take approximately
twice the number of instructions and clock cycles as the 16-bit version. We will
look at 32-bit operations later in the semester.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 42

Review: Units Review: Units


In this class, units are always used for physical quantity:
Time Frequency
milliseconds (ms = 10-3 s) kilohertz (kHz = 103 Hz)
microseconds (μs = 10-6 s) megahertz (MHz = 106 Hz)
nanoseconds (ns = 10-9 s) gigahertz (GHz = 109 Hz)

When a time/frequency/voltage/current quantity is asked for, I


For a frequency of 1.25
will always kHz,
ask for what
it in someisunits.
the period
units in μs?
Values for these quantities in
datasheets
period = 1/f = 1/(1.25aree3)
ALWAYS
= 8.0 egiven in units.
–4 seconds
Unit conversion= 8.0e-4of(s)
For a frequency * (1e6
1.25 what is the(s)
kHz, μs)/1.0 = 8.0e2
period μs = 800 μs
in μs?
period = 1/f = 1/(1.25 e3) = 8.0 e –4 seconds
0e 4 (s) * (1e6 μs)/1.0
Unit conversion= 88.0e-4 μs)/1 0 (s) = 8.0e2
8 0e2 μs = 800 μs
V 0.2 71
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 43

PIC24H Family
• Microchip has an extensive line of PICmicro® microcontrollers, with
the PIC24 family introduced in 2005.
• The PIC16 and PIC18 are older versions of the PICmicro® family,
have been several previous generations.
• Do not assume that because something is done one way in the
PIC24, that it is the most efficient method for accomplishing that
action.
• The datasheet for the PIC24 is found in the class UVLE site.
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 44

PICmicro Survey
PIC16F87x PIC18F242 PIC24H
Instruction width 14 bits 16 bits 24 bits
Program Memory 8K instruction 8K instructions ~10K instructions
Data Memory 386 bytes 1537 bytes 2048 bytes
Clock Speed Max 20 MHz Max 40 MHz Max 80 MHz
4 clks = 1 instruction 4 clks = 1 instruction 2 clks = 1 instruction

The PIC24H can execute about 6x faster than the PIC18F242


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 45

Inside the PIC24FJ64GB002.inc


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 46

Inside the PIC24FJ64GB002.inc


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 47

Inside the PIC24FJ64GB002.gld


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 48

Inside the PIC24FJ64GB002.gld


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 49

Inside the PIC24FJ64GB002.h


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 50

Inside the PIC24FJ64GB002.h


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 51

Inside the PIC24FJ64GB002.h


UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 52

Summary
• Understand the PIC24 basic architecture (program and data
memory organization)
• Understand the operation of mov, add, sub, inc, dec, goto
instructions and their various addressing mode forms
• Be able to convert simple C instruction sequences to PIC24
assembly language
• Be able to assemble/simulate a PIC24 μC assembly language program in
the MPLAB IDE
• Understand the relationship between instruction cycles and
machine cycles
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 53
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 54
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 55
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 56
UP EEEI CoE 115 Lecture 3 (Rosales 1s1920) 57

Potrebbero piacerti anche