chapter 3a pic18 assemblylanguageprogrammingparta(2)

Upload: andy-wo

Post on 13-Apr-2018

247 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    1/75

    Chapter 3

    PIC18 Assembly Language

    Programming

    1

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    2/75

    3.1 General Assembly Language

    Programming

    2

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    3/75

    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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    4/75

    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 anassemblerbe!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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    5/75

    Machine an# Assembly Language

    e.g. in PIC

    Machine Co#e2Opco#e

    Assembly Co#e

    (34 movl $34

    5(6 mov! $6' A

    (*7 movl $*7

    456 a##! $6' 7' A

    (3 movl $3

    5(5 mov! $5' A

    (69 movl $69

    445 a##!c $5' 7' A

    5

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    6/75

    "o "ypes o! Assembly tatements

    Directives 2pseudo-instruction ,se# to control the

    assembler

    *o not generate machineco#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# #irectives6

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    7/75

    .lst !ile

    7

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    8/75

    (lements o! an Assembly Language tatement

    1. Label 2Optional Must start in column 1

    4. Mnemonics

    Coul# either be an assembly instruction

    mnemonicor an assembler directive

    ($amples o! Mnemonics%

    false equ0: e-u is an assembler #irective loop: incf0x20, W, A: inc! is an instruction

    mnemonic

    8

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    9/75

    (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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    10/75

    Program Memory

    ,se# primarily in storing assemblyinstructions' 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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    11/75

    Program Memory

    (ach PIC18 member has a 41;bit a##ress bus.

    Can a##ress up to 441

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    12/75

    Placement o! Instructions in Program Memory

    Most instructions are 4 bytes. A !einstructions are bytes.

    12

    Program

    memory

    a##ress

    Opco#e

    Assembly

    Language Co#e

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    13/75

    Program Counter 2PC

    ,se# by the CP, to point to the a##resso! the instruction being !etche#.

    41;bit store# as 3 bytes PCL' PC/' PC,.

    ince most instructions are 4 bytes' PC

    increments by 4 in each instruction

    cycle.

    13

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    14/75

    7etching an# ($ecution in PIC18

    "he instruction fetchstage gets the ne$tinstruction machine co#e !rom program

    memory.

    "he eecutionstage #oes hatever themachine co#e calls !or.

    !ecution' hich involves interaction

    ith the #ata memory' #oes not inter!ereith fetchinginstruction !rom the

    program memory.

    14

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    15/75

    PIC18 Pipelining

    Instea# o! ta0ing to instruction cyclesto !irst fetchan# eecutean instruction'

    both can be accomplishe# in one

    instruction cycle. "his mechanism is calle#pipelining.

    15

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    16/75

    7etching an# ($ecution Cycles

    7etching Cycle Increment PC

    7etch instruction into the instruction

    register 2I& ($ecution Cycle

    *eco#e instruction

    &ea# operan#s !rom #ata memory Per!orm Arithmetic=Logic operation

    >rite the result to the #estination.

    16

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    17/75

    7etch;An#;($ecute% A Complete ?ie

    17

    mov! $46' >' A

    4

    a##l $

    Decode

    0x32

    0x32

    0x32

    mov! $45' A

    Decode

    0x04

    0x04

    0x36

    5

    mov! $49' A

    0x36

    0x360x36

    0x36

    Decode

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    18/75

    ome important #irectives% (+,' ("

    (+, @ associates a constant number ith ana##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"

    ##########

    mo$lw COU!: >&(G < $46 18

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    19/75

    >ith (+,First%e& '(U 0x0"

    )ec%e& '(U 0x0*

    mo$lw 0x22

    mo$wf First%e&,A

    mo$wf )ec%e&,A

    addwf First%e&, W,A

    addwf )ec%e&, W,A

    (+, an# (" *irectives

    >ithout (+, mo$lw 0x22

    mo$wf 0x0",A

    mo$wf 0x0*,A addwf 0x0", W,A

    addwf 0x0*, W,A

    e.g.' Move 44/ into to !ile registers itha##resses $6 an# $5' a## the contents o! all

    three registers an# put the result in >&(G%

    19

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    20/75

    CLOCB *irective

    *e!ines a list o! name# constants. 7ormat% c+locnumD

    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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    21/75

    ou shoul# be able to ...

    *escribe the #i!!erence beteen machine co#e an#assembly co#e

    *escribe the #i!!erence beteen #irectives an#

    assembly language instructions

    List the elements o! an assembly languagestatement

    ($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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    22/75

    3.4 ,nsigne# an# igne#

    Arithmetic' Logic Instructions

    22

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    23/75

    Outline

    ubtraction o! unsigne# number

    igne# number arithmetic

    C* a##ition

    Logic an# compare instructions

    23

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    24/75

    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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    25/75

    ubtraction !or unsigne# numbers

    e.g.'mo$lw 0x2.

    su+lw 0x.F

    37 11 1111 11 1111; 43 1 11 111 111 24Hs complement

    1C 1 1 11

    &ule% C < 1' orro < .

    25

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    26/75

    ubtraction !or unsigne# numbers

    /o about this% C;5(J

    i.e.'mo$lw 0x*'

    su+lw 0x/C

    "he anser is rong. >hyJ

    ou have a borro 2C

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    27/75

    e.g.' Compute C @ 5( by using the sub!instruction.

    1%e& equ 0x20

    mo$lw 0x/C

    mo$wf 1%e&,A

    mo$lw 0x*'

    su+wf 1%e&, W,A

    ubtraction using sub!

    C 1 11; 5( 11 1 24Hs comp *( 111 111

    27

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    28/75

    Multibyte ubtraction% sub!b

    ubtract 4616 by 1777 2he$ *o the L subtraction

    16 < 1 11

    ;77 ;D 4Hs complement < 1

    L &esult < 1 11

    )ote C < ' orro < 1

    *o the M subtraction

    46 < 1 11

    ;17 ;D 4Hs complement < 111 1

    &esult < 11 ince e orro < 1 in L subtraction

    M &esult < 11

    )ote C < 1' orro <

    7inal &esult < 6 1528

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    29/75

    Multibyte ubtraction% sub!b

    4616 @ 1777

    Assembly Co#e Pseu#oco#e

    movlw 0xFF

    sublw 0x15 [WREG] = 15-FF

    movwf FirstReg, A [FirstReg] = [WREG] (orrow=1!

    movlw 0x"5

    movwf #e$o%&Reg, A

    movlw 0x1F

    subwfb #e$o%&Reg, F, A [#e$o%&Reg] = "5-1F-orrow

    29

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    30/75

    &epresentation o! igne# )umber

    igne# 8;bit representation% *9 2M represents sign % 1 i! negative' i!

    positive

    *;*5 represent the magnitu#e o! the number.

    Positive number same as the unsigne# number representation

    ranges !rom to 49;1 because 9 bits are use#

    )egative number% 4Hs complement >rite the magnitu#e o! the number as a unsigne#

    8;bit number

    Invert each bit

    A## 1 to it 30

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    31/75

    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 8031

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    32/75

    ($ample

    32

    e.g.'mo$lw 0x2

    addlw 0x22

    ;145 1 1 24Hs comp 111 111 3 1 1

    ;K4 11 1 24Hs comp 11 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 O?<

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    33/75

    Over!lo problem in signe# operations

    33

    e.g.'mo$lw 345*4

    addlw 34604

    K5 11 9 1 11

    ;K 11 11

    7lags in "A", register ill be set as% ) < 1 2*9 < 1 O? < 1

    um < ;K

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    34/75

    Anser the !olloing -uestions%

    ho the status o! the ) an# O? !lag bits!or the !olloing co#e%

    2a movl $59

    a##l $KK

    2b movl $

    a##l $5

    2c movl $65

    a##l $87

    2# movl $77 a##l $74

    34

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    35/75

    inary Co#e# *ecimal 2C* ystem

    All a##itions are per!orme# in binary!ormat in PIC18 MC,

    *ecimal numbers can be enco#e# in

    binary;co#e# #ecimal 2C* !ormat

    C* #ecimal C* #ecimal

    11 6

    1 1 11 51 4 111 9

    11 3 1 8

    1 11 K

    35

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    36/75

    C* A##ition

    inary a##ition o! C* numbers oul# notbe correct i! one o! the !olloing occurs%1. I! a sum #igit is greater than K

    e.g.' 4 35 66

    59 9 99

    8 9* CC

    4. I! a sum #igit has a carry o! 1 to the higher #igit

    e.g.' 4K 9

    9

    36

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    37/75

    C* A##ition

    olution% A## $5 to these to types o!sum #igits.

    e.g.' 4 35 66 4K

    59 9 99 98 9* CC 9

    5 5 55 5

    K1 83 134 95

    dawoul# #o this !or you. mo$f 0x-0, W, A

    addwf 0x-/, W, A

    daw 37

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    38/75

    #a Instruction

    #a instruction a##s $5 to the upperor loer byte i! necessary.

    #a instruction or0s only on >&(G

    Loer nibble% I! the loer nibble 2 bitsis greater than K or i! *C < 1' a## $5

    to the loer nibble.

    ,pper nibble% I! the higher nibble 2 bitsis greater than K or i! C < 1' a## $5 to

    the upper nibble.38

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    39/75

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

    e.g.' 7in# the sum o! the values $88' $5K an# $K9. Put the sumin !ile register $6 an# $5.

    78+1te equ 0x0*

    98+1te equ 0x0"

    or& 0x00

    clrf 78+1te clrf 98+1te

    mo$lw 0x

    addlw 0x*5

    daw

    +nc 8-

    incf 78+1te, F, A8-: addlw 056

    daw

    +nc O$er

    incf 78+1te, F,A

    O$er:mo$wf 98+1te,A

    end 39

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    40/75

    Multiplication

    PIC18 has to instructions !or 8;bitmultiplication% mul! ! an# mull 0.

    "he pro#ucts are store# in the

    P&O*/%P&O*L register pair. "he !olloing instruction se-uence

    per!orms 8;bit multiplication operation%

    mo$f 0x-0,W,Amulwf 0x--,A

    40

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    41/75

    Logic Instructions

    PIC18 provi#es instructions to per!ormlogic 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&egxo!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&eg41

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    42/75

    Compare instructions

    Compare instructions compare a valuein the !ile register ith the contents o!

    the >&(G register.

    "hree instructions !or compareoperations%

    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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    43/75

    ($ample% cp!slt

    e.g. >rite a program to !in# the greater o! the

    ?AL1 an# ?AL4 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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    44/75

    ($ample

    e.g.' uppose a value " is store# in the

    register $4. Put a value to the

    register $41 accor#ing to the

    !olloing scheme%I! " < 96' then E$41F < 1

    I! " D 96' then E$41F < 4

    I! " 96' then E$41F <

    44

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    45/75

    ($ample

    File%e& equ 0x20%esult equ 0x2-

    ain: mo$lw 3

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    46/75

    3.3 Looping an# ranching

    46

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    47/75

    Looping in PIC

    Loop @ repeating a se-uence o! instructionsa certain number o! times.

    e.g.' "o a## 3 to >&(G !ive times' I coul# #o%mo$lw 0x00

    addlw 0x0.

    addlw 0x0.

    addlw 0x0.

    addlw 0x0.

    addlw 0x0.

    *isa#vantage% "oo much co#e space is

    nee#e# i! repeating 1 times ,se looping

    47

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    48/75

    Looping using #ec!s instruction

    #ec!s stan#s !or *(Crement !ileregister an# 0ip i! Nero.

    7ormat% decfs> f, d

    here fis the 8;bit a##ress o! the !ileregister an# # in#icates the #estination o!

    #ecremente# value% > @ >&(G' 7 @ 7ile

    &egister

    48

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    49/75

    Looping using #ec!s instruction

    e.g.' >rite a program to 2a initialie >&(G to 2ba## $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

    Addore:addlw 0x0"

    decfs> count, F, A

    &oto Addore

    mo$wf @%O39

    49

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    50/75

    Looping using bn instruction

    bn stan#s !or branch i! the N !lag in thestatus register is ero

    7ormat% bn label

    e.g.' Implement the loop in the last sli#eusing bn.

    50

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    51/75

    Comparison beteen #ec!s an# bn

    #ec!scount equ 0x00

    mo$lw d?-04

    mo$wf count, A

    mo$lw 0x00

    Addore:addlw 0x0"

    decfs> count, F, A

    &oto Addoremo$wf @%O39

    bncount equ 0x00

    mo$lw d?-04

    mo$wf count, A

    mo$lw 0x00

    Addore: addlw

    0x0"

    decf count, F, A

    +n> Addore

    mo$wf @%O39

    51

    O h #i i l

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    52/75

    Other con#itional umps

    "hese branching instructions umpshen 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

    ( l ! b # N

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    53/75

    ($amples o! umps base# on N

    e.g.' >rite a program to #eterminehether !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 0x00mo$wf file%e&

    ext: ###

    53

    ( l ! b # C

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    54/75

    ($amples o! umps base# on C

    e.g.' 7in# the sum o! the values $9K' $76 an# $(4. Putthe sum in !ile register $6 an# $5.

    78+1te equ 0x0*

    98+1te equ 0x0"

    or& 0x0000 clrf 78+1te

    clrf 98+1te

    mo$lw 0x65

    addlw 0xF"

    +nc 8- incf 78+1te, F

    8-: addlw 0'2

    +nc O$er

    incf 78+1te, F

    O$er:mo$wf 98+1te

    end54

    h t

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    55/75

    hort umps

    "he instruction+nc la+elis interprete# as+nc n by theassembler' here n is a signe# 1;byte number ithin ;148 to

    149.

    nis 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 short "umps.55

    P M ! th b ( l

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    56/75

    Program Memory o! the bnc ($ample

    "he label )1 is one instruction aay 2at

    8 !rom the current PC 25 hen

    branching occurs. Opco#e < (31ump 1 instruction 2or 4

    bytes !rom 5 to 8 i! branching

    occurs. 56

    L

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    57/75

    Long umps

    gotospeci!ies the absolute a##ress o!the branching #estination an# can ump

    to anyhere in the program memory.

    goto is a ;byte instruction.

    57

    ( l ! b t i t ti GO"O

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    58/75

    ($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 organie# as4;byte bloc0s. L

    must be .

    58

    M hi # ! GO"O

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    59/75

    Machine co#e o! GO"O

    "he goto Main line in our previouse$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

    ( l ! th t i t ti

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    60/75

    ($ample !or the goto instruction

    e.g.' In our program' e ant to branchto a##ress $4. >hat is the

    machine co#e o! the goto instructionJ

    60

    L

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    61/75

    Long umps

    bra allocate# 11 bits !or storing the relative

    a##ress o! the targeting instruction.

    bra can ump !orar# !or a ma$. o! 143instructions an# bac0ar# !or a ma$. o!

    14 instructions.

    61

    ( ercise on bra a##ress enco#ing

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    62/75

    ($ercise on bra a##ress enco#ing

    62

    'alc(la%e %)e !ela%i*e add!e$$e$ "a!,ed / encoded in %)e

    o#code $)own elow.

    Comparison among 3 types o!

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    63/75

    p g yp

    branching instructions

    Con#itionalranching ,ncon#itional ranching

    +c, +nc, +>, +n>,+n, +nn, +o$, +no$ +ra &oto

    !anc) acco!ding %o %)ew)e%)e! a flag in %)e55 !egi$%e!ed i$!ai$ed.

    !anc) (ncondi%ionall

    5)e o#e!and of %)e$e in$%!(c%ion$ $#ecifie$ %)eadd!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(%eadd!e$$ of %)e

    de$%ina%ionle %o ("# -128 %o+127 line$ wi%) !e$#ec% %oc(!!en% in$%!(c%ion

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

    le %o ("# %o anw)e!ein %)e #!og!a" "e"o!

    63

    ($ercise on &elative=Absolute A##resses

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    64/75

    ($ercise on &elative=Absolute A##resses

    &OM Machine LI)( O,&C(A##ress Co#e

    44 CLOCB $4 43 inary 4 "ens 46 ,nits 45 ()*C 49

    48 O&G $ (7JJ 7JJJ 4K goto Main 3 O&G $44 (* 31 Main% movl #Q99Q44 5(4 34 mov! inary' A4 5A41 33 in4C*% clr! "ens' A

    45 5A44 3 clr! ,nits' A48 64 36 mov! inary' >' A4A 775 35 Loop% a##l ;#Q1Q4C (3JJ 39 bnc )e$t4( 4A41 38 inc! "ens' 7' A3 *JJJ 3K bra Loop34 7A )e$t% a##l #Q1Q3 5(44 1 mov! ,nits' A 4 ()*

    64

    '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".

    ranch=ubroutine Instruction "iming

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    65/75

    ranch=ubroutine Instruction "iming

    ranch instructions are those that changethe program counter 2PC 2i.e.' all

    branching an# call instructions

    ince the !etch stage incorrectly gets themachine co#e !or the ne$t se-uential

    instruction hen branching' this machine

    co#e must be #umpe#.

    65

    bra Instruction in *ata heet

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    66/75

    bra Instruction in *ata heet

    ,ncon#itionalbranch

    Pre;!etche#

    instruction must be

    #umpe#.

    )othing can be

    e$ecute# in the

    secon# cycle

    66

    ranch=ubroutine Instruction "iming

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    67/75

    ranch=ubroutine Instruction "iming

    "his #umping results in ta0en branches2inclu#ing uncon#itional branches

    re-uiring to instruction cloc0s to

    process. ranches that are not ta0en re-uire only

    one machine cycle.

    67

    N as #escribe# in *ata heet

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    68/75

    N as #escribe# in *ata heet

    I! ump' instructionis !etche# incorrectly

    an# must be

    #umpe#.

    )othing can be

    e$ecute# in the

    secon# cycle.

    68

    ,sing Program Loops to Create "ime *elays

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    69/75

    ,sing Program Loops to Create "ime *elays

    Consi#er the !olloing co#e !ragment%3ela19oop:decfs> 3'9A89

    +ra 3ela19oop

    ecause the *(LAL 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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    70/75

    ,sing Program Loops to Create "ime *elays

    3ela19oop:decfs> 3'9A89 +ra 3ela19oop

    decfs> 3'9A87

    +ra 3ela19oop e.g.' *(LAL < $' *(LA/ < $4

    "he !irst #ec!s instruction ill e$ecute !or 465 times.

    *(LAL ill become a!ter the 465th#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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    71/75

    ,sing Program Loops to Create "ime *elays

    "he PIC18 uses a crystal oscillator togenerate the cloc0 signal nee#e# to

    control its operation.

    "he instruction e$ecution time ismeasure# 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

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    72/75

    PIC Multistage ($ecution Pipeline

    ($ecution ta0es cloc0 perio#s o! theoscillator.

    e.g.' Cloc0 cycle < M/: Instruction cycle 3'9A89 1 instruction cycle 24 i! s0ip+ra 3ela19oop 4 instruction cycles

    decfs> 3'9A87 1 instruction cycle 24 i! s0ip

    +ra 3ela19oop 4 instruction cycles

    74

    ou shoul# be able to

  • 7/26/2019 Chapter 3A PIC18 AssemblyLanguageProgrammingPartA(2)

    75/75

    ou shoul# be able to .....

    Co#e PIC assembly language instructions to createloops.

    Co#e PIC assembly language con#itional branch

    instructions.

    ($plain ho status bits a!!ect the behaviour o!con#itional branch instructions.

    Compare the properties o! long ump an# short ump

    instructions.

    Co#e long ump instructions !or uncon#itional ump. Calculate the target a##resses !or con#itional

    branch instructions.

    ($plain in hat situation neste# loop is re-uire# in