Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

Share Embed Donate


Short Description

2004...

Description

Chapter 3 PIC18 Assembly Language Programming

1

3.1 General Assembly Assembly Language Programming

2

3.1 General Assembly Assembly Language Programming

2

Outline •







Machine vs. assembly language tructure o! assembly language "he program counter an# program memory in PIC ome e$ample #irectives% O&G' ()*' (+, an# ("

3

Machine an# Assembly Language •

Machine Language (opcode)  –

 –



A se-uence o! s an# 1s that can be e$ecute# by the processor  /ar# to un#erstan#' program' an# #ebug !or human being

 Assembly Language (Source Code)  –

 –

 –

*e!ine# by assembly instructions Assembly programs must be translate# by an assembler  be!ore it can be e$ecute# re!erre# to as low-level language because the program nee#s to 0no the #etaile# interaction ith the CP, 4

Machine an# Assembly Language •

e.g. in PIC Machine Co#e 2Opco#e

Assembly Co#e

(34

movl $34

5(6

mov! $6' A

(*7

movl $*7

456

a##! $6' 7' A

(3

movl $3

5(5

mov! $5' A

(69

movl $69

445

a##!c $5' 7' A 5

"o "ypes o! Assembly tatements •

Directives 2 pseudo-  instruction  –

 –

 –



,se# to control the assembler  *o not generate machine co#e e.g.' O&G $ an# ()*

 Assembly language instructions  –

 –

*o generate machine co#e to per!orm various operations e.g.' all lines e$cept the comments an# #irectives

6

.lst !ile

7

 (lements o! an Assembly Language tatement

1. Label 2Optional • Must start in column 1

4. Mnemonics  – Coul# either be an assembly instruction mnemonic  or an assembler directive  – ($amples o! Mnemonics% •



false equ 0 : e-u is an assembler #irective loop: incf 0x20, W, A  : inc! is an instruction mnemonic 8

 (lements o! an Assembly Language tatement

3. Operan#s 2Optional •



false equ 0 loop: incf 0x20, W, A 

. Comment 2Optional •



decf 0x20, F, A ;decrement the loop count ; the whole line is comment

9

Program Memory •









,se# primarily in storing assembly instructions' although it can be use# to store !i$e# #ata 2loo0up table later eparate !rom #ata memory 41;bit a##ress bus' 15;bit #ata bus A##ress up to 441' A A $45' mov! $49'

0x32 0x04

4   5 0x32 0x32 0x36 0x36

Decode 17

ome important #irectives% (+,' (" (+, @ associates a constant number ith an a##ress label. e.g.' COU! equ 0x2" ##########  mo$lw COU!: >&(G < $46 (" @ i#entical to (+,' but value assigne# by (" can be reassigne# later. e.g.' COU! set 0x00   ###########  COU! set 0x2"   ########## 18  mo$lw COU!: >&(G < $46 •







(+, an# (" *irectives e.g.' Move 44/ into to !ile registers ith a##resses $6 an# $5' a## the contents o! all three registers an# put the result in >&(G% >ithout (+, First%e&  mo$lw 0x22 )ec%e& mo$wf 0x0", A  mo$lw mo$wf 0x0*, A  mo$wf addwf 0x0", W, A  mo$wf addwf 0x0*, W, A  addwf

>ith (+, '(U 0x0" '(U 0x0* 0x22 First%e&, A  )ec%e&, A  First%e&, W, A  addwf )ec%e&, W, A  19

CLOCB *irective *e!ines a list o! name# constants. 7ormat% c+loc numD constant labelD E%incDF   endc e.g. 1' c+loc 0x"0 test-, test2, test., test/   endc ?alues Assigne#% test1 < $6' test4 < $61' test3 < $64' test < $63. •







20

 ou shoul# be able to ... •













*escribe the #i!!erence beteen machine co#e an# assembly co#e *escribe the #i!!erence beteen #irectives an# assembly language instructions List the  elements o! an assembly language statement ($plain the program memory architecture in PIC18 ($amine program in the program memory in#o o! MPLA *escribe the role o! the program counter  *escribe the pipelining !etching an# e$ecution mechanism in PIC18 21

3.4 ,nsigne# an# igne# Arithmetic' Logic Instructions

22

Outline •







ubtraction o! unsigne# number  igne# number arithmetic C* a##ition Logic an# compare instructions

23

ubtraction •



ubtraction uses 4Hs complement In PIC18' !our instructions are available !or subtraction% Instructions

7unction

subl B

>&(G < B ; >&(G

sub ! !ile&eg' #' a

ubtract >&(G !rom !ile&eg

sub !b !ile&eg' #' a

ubtract >&(G !rom !ile&eg ith orro

sub! b !ile&eg' #' a

ubtract !ile&eg !rom >&(G ith orro 24

ubtraction !or unsigne# numbers •

 mo$lw 0x2. 0x2. e.g.' mo$lw su+lw 0x.F 37 ; 43 1C



11 11 11 1111 11 1 11

11 11 11 1111 11 111 11 1 11 111 1 24Hs complem complement ent 1 1 11 11 

&ule% C < 1' orro < .

25

ubtraction !or unsigne# numbers •



/o about this% C;5(J i.e.' mo$lw 0x*' su+lw 0x/C C ; 5( *(





1 11 11 1 24Hs comp 111 111

"he anser is rong. >hyJ  ou have a borro 2Crite the magnitu#e o! the number as a unsigne# 8;bit number  Invert each bit 30 A## 1 to it

8;bit igne# )umber &epresentation *ecimal

inary

/e$

+127

0111 1111

7F

.....

.....

.....

+2

0000 0010

02

+1

0000 0001

01

0

0000 0000

00

-1

1111 1111

FF

-2

1111 1110

FE

....

....

....

-127

1000 0001

81

-128

1000 0000

31

80

($ample •

e.g.' mo$lw 0x2 addlw 0x22 ;145  3 ;K4

1 1 1 1 11 1

24Hs comp 111 111 24Hs comp 11 11

7lags in "A", register ill be set as%  –

 –

 –

) < 1 2*9 < 1 O? <  2)o over!lo. &esult is correct. &ule% )egative number  Positive number &(G Loer nibble% I! the loer nibble 2 bits is greater than K or i! *C < 1' a## $5 to the loer nibble. ,pper nibble% I! the higher nibble 2 bits is greater than K or i! C < 1' a## $5 to the upper nibble. 38

($ample% C* A##ition o! 3 numbers •

e.g.' 7in# the sum o! the values $88' $5K an# $K9. Put the sum in !ile register $6 an# $5. 78+1te equ 98+1te equ or& clrf clrf  mo$lw addlw daw +nc incf  8-: addlw daw +nc incf O$er:mo$wf end 

0x0* 0x0" 0x00 78+1te 98+1te 0x 0x*5 878+1te, F, A  056 O$er 78+1te, F, A  98+1te, A 

39

Multiplication •





PIC18 has to instructions !or 8;bit multiplication% mul! ! an# mull 0. "he pro#ucts are store# in the P&O*/%P&O*L register pair. "he !olloing instruction se-uence per!orms 8;bit multiplication operation%  mo$f  mulwf

0x-0,W,A  0x--,A  40

Logic Instructions •

PIC18 provi#es instructions to per!orm logic operations such as A)*' O&' ($clusive;O& an# complement.

Instructions

7unction

andlw K

A)* B ith >

andwf fileReg d a

A)* > ith !ile&eg

io!lw

K

Or B ith >

io!wf

fileReg d a

Or > ith !ile&eg

xo!lw K

($clusive Or B ith >

xo!wf fileReg d a

($clusive Or > ith !ile&eg

co"f

fileReg d a

Complement !ile&eg

negf

fileReg a

Pro#uce 4Hs complement o! !ile&eg 41

Compare instructions •



Compare instructions compare a value in the !ile register ith the contents o! the >&(G register. "hree instructions !or compare operations% Instructions

7unction

c#f$g% fileReg a

Compare ! ith >&(G' s0ip i! !ile&eg D >&(G

c#f$e& fileReg a

Compare ! ith >&(G' s0ip i! !ile&eg < >&(G

c#f$l% fileReg a

Compare ! ith >&(G' s0ip i! !ile&eg  >&(G 42

($ample% cp!slt •

e.g. >rite a program to !in# the greater o! the ?AL1 an# ?AL4 registers an# place it in !ile register location $4.

 1%e& equ 0x20  A98- equ 0x00  A982 equ 0x0 mo$f A98-, W, A  cpfslt A982, A   mo$f A982, W, A   mo$wf 1%e&, A 

43

($ample •

e.g.' uppose a value " is store# in the register $4. Put a value to the register $41 accor#ing to the !olloing scheme% I! " < 96' then E$41F < 1 I! " D 96' then E$41F < 4 I! "  96' then E$41F < 

44

($ample File%e& equ 0x20 %esult equ 0x2 ain: mo$lw 3 f, d here f is the 8;bit a##ress o! the !ile register an# # in#icates the #estination o! #ecremente# value% > @ >&(G' 7 @ 7ile &egister  •



48

Looping using #ec!s instruction •

e.g.' >rite a program to 2a initialie >&(G to  2b a## $6 ten times to >&(G an# put the result in the P&O*L register  count equ 0x00  mo$lw d?-04  mo$wf count, A   mo$lw 0x00  Addore:addlw 0x0" decfs> count, F, A  &oto Addore  mo$wf @%O39

49

Looping using bn instruction •





bn stan#s !or branch i! the N !lag in the status register is ero 7ormat% bn label e.g.' Implement the loop in the last sli#e using bn.

50

Comparison beteen #ec!s an# bn •

#ec!s count equ 0x00

 mo$lw d?-04  mo$wf count, A   mo$lw 0x00  Addore:addlw 0x0" decfs> count, F, A  &oto Addore  mo$wf @%O39



bn count equ 0x00  mo$lw d?-04  mo$wf count, A   mo$lw 0x00  Addore: addlw 0x0" decf count, F, A   +n> Addore  mo$wf @%O39 51

Other con#itional umps •

"hese branching instructions umps hen C an# N in the "A", register are 1 or   –

 –

 –

 –

bn% ump i! N <  2previously #escribe# b% ump i! N < 1 bnc% ump i! C <  bc% ump i! C < 1

52

($amples o! umps base# on N •

e.g.' >rite a program to #etermine hether !ile register $3 contains a value other than $. I! not' put $ in it. file%e& equ 0x.0

 mo$f file%e&, F  +> ext  mo$lw 0x00  mo$wf file%e&  ext: ### 53

($amples o! umps base# on C •

e.g.' 7in# the sum o! the values $9K' $76 an# $(4. Put the sum in !ile register $6 an# $5. 78+1te equ 98+1te equ or& clrf clrf  mo$lw addlw +nc incf  8-: addlw +nc incf O$er:mo$wf

0x0* 0x0" 0x0000 78+1te 98+1te 0x65 0xF" 878+1te, F 0'2 O$er 78+1te, F 98+1te

54

hort umps •



"he instruction +nc la+el is interprete# as +nc n by the assembler' here n is a signe# 1;byte number ithin ;148 to 149. n is the relative a##ress o! the branching #estination ith respect to the current PC. n is e$presse# in number o! instructions 2i.e.' relative a##ress < 4n i! e$presse# in a##ress

• ecause the target must be ithin 465 instructions o! the current PC' con#itional branches are 55short "umps

Program Memory o! the bnc ($ample





"he label )1 is one instruction aay 2at 8 !rom the current PC 25 hen branching occurs. Opco#e < (31  ump 1 instruction 2or 4 bytes !rom 5 to 8 i! branching occurs. 

56

Long umps •



goto speci!ies the absolute a##ress o! the branching #estination an# can ump to anyhere in the program memory. goto is a ;byte instruction.

57

($ample o! a ;byte instruction% GO"O •

>hy ;byteJ  –



"he machine co#e must contain the a##ress o! the #estination in the program memory 241;bit

Only the most signi!icant 4 bits are inclu#e#. >hyJ Program memory are organie# as 4;byte bloc0s. L must be .

 –

58

Machine co#e o! GO"O "he goto Main line in our previous e$ample !orces PC to $' here the Main label is. "he machine co#e is 'F02 F000 "hus' the program goes to     1  in binary 2i.e.' $ in he$







59

($ample !or the goto instruction •

e.g.' In our program' e ant to branch to a##ress $4. >hat is the machine co#e o! the goto instructionJ

60

Long umps •

bra  –

 –

allocate# 11 bits !or storing the relative a##ress o! the targeting instruction. bra can ump !orar# !or a ma$. o! 143 instructions an# bac0ar# !or a ma$. o! 14 instructions.

61

($ercise on bra a##ress enco#ing 'alc(la%e %)e !ela%i*e add!e$$e$ "a!,ed  / encoded in %)e o#code $)own elow.

62

Comparison among 3 types o! branching instructions Con#itional ranching  +c, +nc, +>, +n>,  +n, +nn, +o$, +no$ !anc) acco!ding %o %)e w)e%)e! a flag in %)e 55 !egi$%e!ed i$ !ai$ed.

,ncon#itional ranching

 +ra

&oto

!anc) (ncondi%ionall

5)e o#e!and of %)e$e in$%!(c%ion$ $#ecifie$ %)e add!e$$ of %)e de$%ina%ion of %)e ("# !ela%i*e %o %)e #o$i%ion of %)e nex% in$%!(c%ion

5)e o#e!and of go%o $#ecifie$ %)e a$ol(%e add!e$$ of %)e de$%ina%ion

 le %o ("# -128 %o  le %o ("# -1024 %o +127 line$ wi%) !e$#ec% %o +1023 line$ wi%) !e$#ec% c(!!en% in$%!(c%ion %o c(!!en% in$%!(c%ion

 le %o ("# %o anw)e!e in %)e #!og!a" "e"o!

63

($ercise on &elative=Absolute A##resses 'alc(la%e %)e !ela%i*e9a$ol(%e add!e$$e$ "a!,ed  / encoded in %)e o#code $)own in :ine$ 2; 37 3; in %)e following #!og!a". &OM A##ress

Machine Co#e

LI)(

   4 44 4 45 48 4A 4C 4( 3 34 3

(7JJ 7JJJ (* 5(4 5A41 5A44 64 775 (3JJ 4A41 *JJJ 7A 5(44 4

()*

O,&C(

44 CLOCB $4 43 inary 4 "ens 46 ,nits 45 ()*C 49 48 O&G $ 4K goto Main 3 O&G $4 31 Main% movl #Q99Q 34 mov! inary' A 33 in4C*% clr! "ens' A 3 clr! ,nits' A 36 mov! inary' >' A 35 Loop% a##l ;#Q1Q 39 bnc )e$t 38 inc! "ens' 7' A 3K bra Loop  )e$t% a##l #Q1Q 1 mov! ,nits' A 64

ranch=ubroutine Instruction "iming •



ranch instructions are those that change the program counter 2PC 2i.e.' all branching an# call instructions ince the !etch stage incorrectly gets the machine co#e !or the ne$t se-uential instruction hen branching' this machine co#e must be #umpe#.

65

bra Instruction in *ata heet •





,ncon#itional branch Pre;!etche# instruction must be #umpe#. )othing can be e$ecute# in the secon# cycle

66

ranch=ubroutine Instruction "iming •



"his #umping results in ta0en branches 2inclu#ing uncon#itional branches re-uiring to instruction cloc0s to process. ranches that are not ta0en re-uire only one machine cycle.

67

N as #escribe# in *ata heet •



I! ump' instruction is !etche# incorrectly an# must be #umpe#. )othing can be e$ecute# in the secon# cycle.

68

,sing Program Loops to Create "ime *elays •

Consi#er the !olloing co#e !ragment% 3ela19oop: decfs> 3'9A89  +ra 3ela19oop







ecause the *(LAL register is 8;bit' the loop can be repeate# a ma$imum o! 465 times. /o #o e get a longer #elayJ olution% Looping ithin a loop or #ested Loop 69

,sing Program Loops to Create "ime *elays 3ela19oop: decfs> 3'9A89 +ra 3ela19oop decfs> 3'9A87 +ra 3ela19oop •













e.g.' *(LAL < $' *(LA/ < $4 "he !irst #ec!s instruction ill e$ecute ! or 465 times. *(LAL ill become  a!ter the 465 th #ecrement. "he bra line ill be s0ippe#. *(LA/ ill be #ecremente# to 1. )ot ero. ranch bac0 to the start. "he !irst #ec!s instruction ill e$ecute ! or 465 times. *(LA/ ill be #ecremente# to . (n# program. In total' the !irst #ec!s instruction e$ecutes 4$465 times.

70

,sing Program Loops to Create "ime *elays









"he PIC18 uses a crystal oscillator to generate the cloc0 signal nee#e# to control its operation. "he instruction e$ecution time is measure# by using the instruction cycle cloc0. 1 instruction cycle <  cloc0 cycles. A #esire# time #elay is create# by repeating a certain set o! instructions using loops. 71

PIC Multistage ($ecution Pipeline •











($ecution ta0es  cloc0 perio#s o! the oscillator. e.g.' Cloc0 cycle < M/: Instruction cycle < Cloc0 cycle= < 1M/. +1 @ *eco#e the instruction that has been !etche# +4 @ Operan# is !etche# !rom the !ile register  +3 @ "he operation is per!orme# + @ "he result is ritten in the #estination register  72

PIC Multistage ($ecution Pipeline

73

,sing Program Loops to Create "ime *elays •

uppose *(LAL <  an# *(LA/ < 4. 3ela19oop:

decfs> 3'9A89  +ra 3ela19oop decfs>

3'9A87

 +ra 3ela19oop •









1 instruction cycle 24 i! s0ip 4 instruction cycles 1 instruction cycle 24 i! s0ip 4 instruction cycles

"he !irst to lines ill run !or 465 times' lasting 466R34 < 959 instruction cycles #uring each loop. "he last to lines ill run !or 4 times' lasting 34 < 6 instruction cycles. "here are 4 loops 2set by *(LA/. "he hole #elay loop ill run !or 959R46 < 163K instruction cycles. I! cloc0 !re-uency < M/ 2instruction !re-. < 1M/' the hole loop lasts 1.63K msec

74

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF