Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
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
Be careful:
while ADD Wx, Wy, Wz gives the same result as ADD Wy, Wx, Wz
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
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
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
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
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
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
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
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).
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
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
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
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
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
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.
1/ frequency = period
1/80 MHz = 12 5 ns (1 ns = 1.0e-9 s)
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
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
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
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
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