+ All Categories
Home > Documents > Chap4 Logctrl Ops

Chap4 Logctrl Ops

Date post: 08-Apr-2018
Category:
Upload: koko-mimo-bobo
View: 234 times
Download: 0 times
Share this document with a friend

of 49

Transcript
  • 8/7/2019 Chap4 Logctrl Ops

    1/49

    V 0.9 1

    CArithmetic operators

    bitwise complement (~i)~

    Right shift (i > 1)>>,

  • 8/7/2019 Chap4 Logctrl Ops

    2/49

    V 0.9 2

    Bit-wise Logical operations

    Bitwise AND operation(w AND f) andwf floc, d d (floc) & (w) j = j & i;

    (w AND literal) andlw k w (0xkk) & (w) j = j & 0xkk;

    Bitwise OR operation

    (w OR f) iorwf floc, d d (floc) | (w) j = j | i;

    (w OR literal) iorlw floc, d d 0xkk| (w) j = j | 0xkk;

    Bitwise XOR operation

    (w XOR f) xorwf floc, d d (floc) ^ (w) j = j ^ i;

    (w XOR literal) xorlw floc, d d 0xkk^ (w) j = j ^ 0xkk

    Bitwise complement operation;

    (~ f) comf floc, d d ~ (floc) j = ~ i ;

  • 8/7/2019 Chap4 Logctrl Ops

    3/49

    V 0.9 3

    Clearing a group of

    bits

    Data Memory

    Location contents

    (i) 0x020 0x2C

    (j) 0x021Clear upper four bits of i .

    In C:

    i = i & 0x0f;

    In PIC18 assembly

    movf 0x020, w ; w = i

    andlw 0x0f ; w = w & 0x0f

    movwf 0x020 ; i = w

    i = 0x2C = 0010 1100

    &&&& &&&&mask= 0x0F = 0000 1111

    ---------result = 0000 1100

    = 0x0C

    AND: mask bit = 1, result bit is same as operand.

    mask bit = 0, result bit is cleared

    The mask

    0xB2

    (k) 0x022 0x8A

  • 8/7/2019 Chap4 Logctrl Ops

    4/49

    V 0.9 4

    Setting a group of

    bits

    Data Memory

    Location contents

    (i) 0x020 0x2C

    (j) 0x021Set bits b3:b1 of j

    In C:

    j = j | 0x0E;

    In PIC18 assemblymovf 0x021, w ; w = j

    iorlw 0x0E ; w = w | 0x0E

    movwf 0x021 ; j = w

    j = 0xB2 = 1011 0010

    |||| ||||mask= 0x0E = 0000 1110

    ---------result = 1011 1110

    = 0xBE

    OR: mask bit = 0, result bit is same as operand.

    mask bit = 1, result bit is set

    The mask

    0xB2

    (k) 0x022 0x8A

  • 8/7/2019 Chap4 Logctrl Ops

    5/49

    V 0.9 5

    Complementing a

    group of bits

    Data Memory

    Location contents

    (i) 0x020 0x2C

    (j) 0x021Complement bits b7:b6 of k

    In C:

    k = k ^ 0xC0;

    In PIC18 assemblymovf 0x022, w ; w = k

    xorlw 0xC0 ; w = w ^ 0xC0

    movwf 0x022 ; k = w

    k = 0x8A = 1000 1010

    ^^^^ ^^^^mask= 0xC0 = 1100 0000

    ---------result = 0100 1010

    = 0x4A

    XOR: mask bit = 0, result bit is same as operand.

    mask bit = 1, result bit is complemented

    The mask

    0xB2

    (k) 0x022 0x8A

  • 8/7/2019 Chap4 Logctrl Ops

    6/49

    V 0.9 6

    Complementing all

    bits

    Data Memory

    Location contents

    (i) 0x020 0x2C

    (j) 0x021 0xB2Complement all bits of k

    In C:

    k = ~k ;

    In PIC18 assemblycomf 0x022, f ; k = ~k

    (k) 0x022 0x8A

    k = 0x8A = 1000 1010

    After complement

    result = 0111 0101= 0x75

  • 8/7/2019 Chap4 Logctrl Ops

    7/49

    V 0.9 7

    Bit set, Bit Clear, Bit Toggle instructionsCan set/clear/complement one bit of a data memory location by using the

    AND/OR/XOR operations, but takes three instructions as previously

    seen.

    The bit clear (bcf), bit set (bsf), bit toggle (btg) instructions

    clear/set/complement one bit of data memory using one instruction.

    bcf floc, b [,a] 1 0 0 1 b b b a f f f f f f f f

    B B B B B B B B B B B B B B B B1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 05 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0

    bsf floc, b [,a] 1 0 0 0 b b b a f f f f f f f f

    0 1 1 1 b b b a f f f f f f f fbtg floc, b [,a]

    fffffff lower 7-bits of memory location

    bbb three bit value that specifies affected bit

  • 8/7/2019 Chap4 Logctrl Ops

    8/49

    V 0.9 8

    Data MemoryBit clear/set/toggle

    examples

    Location contents

    (i) 0x020 0x2C

    (j) 0x021 0xB2Clear bit 7 of k, Set bit 2 of j,

    complement bit 5 of i.

    In C:

    k = k & 0x7F;

    j = j | 0x04;i = i ^ 0x20;

    In PIC18 assembly

    bcf 0x022, 7 ; k = bitclear(k,7)

    bsf 0x021, 2 ; j = bitset(j,2)

    btg 0x020, 5

    (k) 0x022 0x8Abbbb bbbb7654 3210

    k = 0x8A = 1000 1010

    bcf k,7k = 0x0A = 0000 1010

    j = 0xB2 = 1011 0010

    bsf j,2j = 0xB6 = 1011 0110

    i = 0x2C = 0010 1100btg i,5i = 0x0C = 0000 1100

  • 8/7/2019 Chap4 Logctrl Ops

    9/49

    V 0.9 9

    Conditional Execution using Bit Test

    The bit test f, skip if clear (btfsc) and bit test f, skip if set

    (btfss) instructions are used for conditional execution.

    btfsc floc, b ; skips next instruction is bit b offloc is clear (0)

    btfss floc, b ; skips next instruction is bit b offloc is set (1)

    Bit test instructions used on status flags implements tests

    such as equality (==), inequality (!=), greater than (>), less

    than (

  • 8/7/2019 Chap4 Logctrl Ops

    10/49

    V 0.9 10

    (1) CBLOCK 0x0(2) loc,out ;byte variables(3) ENDC

    (4) org 0

    (5) goto main(6) org 0x0200(7) main(8) ;movlw 0 ;uncomment for loc=0(9) movlw 1 ;uncomment for loc=1(10) movwf loc ;initialize loc

    (11) Ltop(12) btfsc loc,0 ; skip next if loc(0) is '0'(13) goto loc_lsb_is_1(14) ;LSb of loc = 0 if reach here(15) movlw 3 ; W 3

    (16) movwf out ; out

    (W)(17) movlw 2 ; W 2(18) movwf out ; out (W)(19) movlw 4 ; W 4(20) movwf out ; out (W)(21) loc_lsb_is_1

    (22) movlw 8 ; W 8(23) movwf out ; out (W)(24) movlw 5 ; W 5(25) movwf out ; out (W)(26) movlw 6 ; W 6(27) movwf out ; out (W)(28) movlw 1 ; W 1(29) movwf out ; out (w)(30) goto Ltop ; loop forever(31) end

    Skip goto loc_lsb_is_1

    location if loc = 1.

    Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

    Number Sequencing

    Task using btfsc

  • 8/7/2019 Chap4 Logctrl Ops

    11/49

    V 0.9 11

    status Register

    The STATUSregister is a special purpose register (like the wregister). The lower 5 bits of the status register are one bit flags

    that are set/cleared as side effects of an instruction execution.

    7 6 5 4 3 2 1 0

    CDCZOVNuuu

    u unimplemented

    N negative flag (set if MSB of result = 1)

    OV 2s complement overflow flag

    Z zero flag (set if result = 0)DC decimal carry (carry/(~borrow) from bit 3 to bit 4)

    C carry flag (carry/(~borrow) out of MSB

    We will not discuss the DC flag, it is used in Binary CodedDecimal arithmetic.

  • 8/7/2019 Chap4 Logctrl Ops

    12/49

    V 0.9 12

    Carry, Zero Flags

    Bit 0 of the status register is known as the carry (C) flag.

    Bit 2 of the status register is known as the zero (Z) flag.

    These flags are set as side-effects of particular instructions orcan be set/cleared explicitly using the bsf/bcfinstructions.

    How do you know if an instruction affects C,Z flags?

    Look at Table 20-2 in PIC18Fxx2 datasheeet.addwfaffects

    all flags, movfonly Z,N flags.

    Mnemonic Desc. Instr Mach. Code StatusCycles affected

    ADDWF f,d,a |ADD WREG and F| 1|0010 01da ffff ffff|C,DC,Z,OV,NMOVF f,d,a |Move f | 1|0101 00da ffff ffff| Z,N

  • 8/7/2019 Chap4 Logctrl Ops

    13/49

    V 0.9 13

    Addition: Carry, Zero Flags

    Zero flag is set if result is zero.

    In addition, carry flag is set if there is a carry out of the MSB

    (unsigned overflow, result is greater > 255)

    0xF0

    +0x20

    --------0x10 Z=0,

    C=1

    0x01

    +0xFF

    --------

    0x00 Z=1,

    C=1

    0x00

    +0x00

    --------

    0x00 Z=1,

    C=0

    0x80

    +0x7F--------

    0xFF Z=0,

    C=0

  • 8/7/2019 Chap4 Logctrl Ops

    14/49

    V 0.9 14

    Subtraction: Carry, Zero Flags

    Zero flag is set if result is zero.

    In subtraction, carry flag is cleared if there is a borrow from the

    MSB (unsigned underflow, result is < 0, larger number subtractedfrom smaller number). Carry flag is set if no borrow occurs.

    0x01-0xFF

    --------

    0x02 Z=0,

    C=0

    0x00-0x00

    --------

    0x00 Z=1,

    C=1

    0xF0- 0x20

    --------

    0xD0 Z=0,C=1

    For a subtraction, the combination of Z=1, C=0 will not

    occur.

  • 8/7/2019 Chap4 Logctrl Ops

    15/49

    V 0.9 15

    How do you remember setting of C flag

    for Subtraction?Subtraction of A B is actually performed in hardware as

    A + (~B) + 1

    The value (~B) + 1 is called the twos complement of B(more on this later). The C flag is affected by the addition

    of A + (~B) + 1

    0xF0

    - 0x20

    --------

    0xD0 Z=0,

    C=1

    0xF0+ 0xDF

    + 0x01

    --------0xD0 Z=0,

    C=1

    0x20 = 0010 0000~0x20 = 1101 1111

    = 0xDF

    No borrow, C=1

    Carry out of MSB,

    so C=1

  • 8/7/2019 Chap4 Logctrl Ops

    16/49

    V 0.9 16

    CConditional Tests

    logical AND&&

    logical negation!

    logical OR| |

    less than, less than or equal=

    equal, not-equal== , !=DescriptionOperator

    If an operator used in a Cconditional test, such as an IF

    statement or WHILE statement, returns nonzero, then the

    condition test is TRUE.

  • 8/7/2019 Chap4 Logctrl Ops

    17/49

    V 0.9 17

    Logical Negation vs. Bitwise Complement!i ~iis not the same as

    i = 0xA0 i = 0xA0

    !(i) 0 ~(i) 0x5F

    Logical operations: !, &&, || always treat their operands aseither being zero or non-zero, and the returned result is

    always either 0 or 1.

  • 8/7/2019 Chap4 Logctrl Ops

    18/49

    V 0.9 18

    Examples ofCEquality, Inequality,

    Logical, Bitwise Logical Testsunsigned char a,b,a_lt_b, a_eq_b, a_gt_b, a_ne_b;

    a = 5; b = 10;

    a_lt_b = (a < b); // a_lt_b result is 1a_eq_b = (a == b); // a_eq_b result is 0a_gt_b = (a > b); // a_gt_b result is 0a_ne_b = (a != b); // a_ne_b result is 1

    unsigned char a_lor_b, a_bor_b, a_lneg_b, a_bcom_b;

    (2) a = 0xF0; b = 0x0F;(3) a_land_b = (a && b); //logical and, result is 1

    (4) a_band_b = (a & b); //bitwise and, result is 0(5) a_lor_b = (a || b); //logical or, result is 1(6) a_bor_b = (a | b); //bitwise or, result is 0xFF(7) a_lneg_b = (!b); //logical negation, result is 0(8) a_bcom_b = (~b); //bitwise negation, result is 0xF0

    Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    19/49

    V 0.9 19

    if{} Statement Format in C

    if_body and else_body can contain multiple

    statements.

    else_body is optional.

  • 8/7/2019 Chap4 Logctrl Ops

    20/49

    V 0.9 20

    Czero/non-zero tests

    A Cconditional test is true if the result is non-zero; false ifthe result is zero.

    The ! operator is a logical test that returns 1 if the operator is

    equal to 0, returns 0 if the operator is non-zero.if (i) {// do this if i non-zero

    j = i + j;}

    if (!i) {// do this if i zero

    j = i + j;}

    Could also write:

    if (i != 0) {// do this if i non-zero

    j = i + j;}

    if (i == 0) {// do this if i zero

    j = i + j;}

  • 8/7/2019 Chap4 Logctrl Ops

    21/49

    V 0.9 21

    Cequality tests

    == is the equality test in C; = is the assignment operator.

    A common C code mistake is shown below (= vs == )

    if (i = 5) {

    j = i + j;} //wrong

    if (i == 5) {

    j = i + j;} // right

    The test i == 5 returns a

    1 only when i is 5. The

    == is the equalityoperator.

    Always executes

    because i=5 returns 5,

    so conditional test isalways non-zero, a true

    value. The = is the

    assignment operator.

  • 8/7/2019 Chap4 Logctrl Ops

    22/49

    V 0.9 22

    CBitwise logical vs. Logical AND

    The & operator is a bitwise logical AND. The && operator

    is a logical AND and treats its operands as either zero or non-

    zero.

    is read as:If ( (i is nonzero) AND

    (j is nonzero) ) then do

    this.

    if (i && j) {/* do this */}

    is read as:if (i & j) {/* do this */}

    If ( (i bitwise AND j) is

    nonzero) ) then do this.

    i = 0xA0, j = 0x0B; i = 0xA0, j = 0x0B;

    (i && j) 1 (i & j) 0x0

  • 8/7/2019 Chap4 Logctrl Ops

    23/49

    V 0.9 23

    CBitwise logical vs. Logical OR

    The | operator is a bitwise logical OR. The || operator is

    a logical OR and treats its operands as either zero or non-

    zero.

    is read as:If ( (i is nonzero) OR (j

    is nonzero) ) { do...

    if (i || j) {/* do this */}

    is read as:if (i | j) {/* do this */}

    If ( (i bitwise OR j) is

    nonzero) ) { do....

    i = 0xA0, j = 0x0B; i = 0xA0, j = 0x0B;

    (i || j) 1 (i | j) 0xAB

    Non-Zero Test labels for SFRs and

  • 8/7/2019 Chap4 Logctrl Ops

    24/49

    V 0.9 24

    Non Zero Test labels for SFRs andbit fields defined in

    pic18f242.inc; usefor clarity!!!!

    The movfinstruction just moves i back onto itself! Does nouseful work except to affect the Z flag.

    Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    25/49

    V 0.9 25

    Conditional Execution using branches

    A branch functions as a conditional goto based upon thesetting of a single flag

    bc (branch if carry), bnc (branch if not carry)bov (branch on overflow), bnov (branch if no overflow)

    bn (branch if negative), bnn (branch if not negative)

    bz(branch if zero), bnz(branch if not zero)

    bra (branch always)

    Using branch instructions instead of btfsc/btfss generallyresults in fewer instructions, and improves code clarity.

    Non-Zero Test

  • 8/7/2019 Chap4 Logctrl Ops

    26/49

    V 0.9 26

    Non Zero Test

    The bz(branch if Zero, Z=1) replaces the btfsc/goto combination.

    For a non-zero test if(!i){} replace bzwith bnz

    Copyright Thomson/Delmar Learning 2005. All Rights Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    27/49

    V 0.9 27

    Equality Test (==)

    Subtraction operation of i-j performed to check equality;

    if i == j then subtraction yields 0, setting the Z flag. Does not

    matter if i-j or j-i is performed.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    i h S i C

  • 8/7/2019 Chap4 Logctrl Ops

    28/49

    V 0.9 28

    switch Statement in C

    A switch statement is a shorthand version of an if-else chain

    where the same variable is compared for equality against

    different values.Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    switch Statement in assembly language

  • 8/7/2019 Chap4 Logctrl Ops

    29/49

    V 0.9 29

    switch Statement in assembly language

    A bra is a

    branch always

    unconditionalbranch. Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    30/49

    V 0.9 30

    Unsigned greater-than Test (>)

    If i > j, then j-i will result in a borrow (C=0). Subtraction

    operation of j-i performed so test on C flag could be done.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    31/49

    V 0.9 31

    Unsigned greater-than Test (>) Variation

    In this case, i-j is performed instead ofj-i. This requires checkingtwo flag conditions as the Z flag must be checked in case i is

    equal to j. Obviously, the code is more complex because of the

    need for two branches, so avoid doing the > comparison in thisway.Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

  • 8/7/2019 Chap4 Logctrl Ops

    32/49

    V 0.9 32

    Unsigned greater-than-or-equal Test (>=)

    If (i >= j), then i j will produce no borrow if i > j or i == j.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    Unsigned Comparison Summary

  • 8/7/2019 Chap4 Logctrl Ops

    33/49

    V 0.9 33

    Unsigned Comparison Summary

    Z = 0

    if i != k

    i k OR i - kif ( i != k) {}

    Z = 1

    if i = = k

    i k OR k - iif (i = = k) {}

    C=1 (no borrow)

    if i>= k

    i kif (i >= k) {}

    C = 0 (borrow)

    if i > k

    k iif (i > k) {}

    If true, thenOperationComparison

    If you do i-k for the i>k comparison, then will have to test both

    C and Z flags. If you do k-i for the i>=k comparison, then will

    have to test both C and Z flags.

    PIC18 Comparison Instructions

  • 8/7/2019 Chap4 Logctrl Ops

    34/49

    V 0.9 34

    PIC18 Comparison Instructions

    The PIC18 has three instructions that directly implement ==,> (unsigned), and < (unsigned).

    cpfseq floc ; iffloc == w, skip next instruction

    cpfsgt floc ; iffloc > w, skip next instruction

    cpfslt floc ; iffloc < w, skip next instruction

    You can use these to implement 8-bit, unsigned comparisons infewer instructions than using the subtract/flag test approach.

    HOWEVER, the subtract/flag test is a more general approach,

    and is useful for any comparison type ( 8-bit unsigned/signed,

    16-bit signed/unsigned, etc). The subtract/flag test is emphasized

    in this class because it can be generally applied; the comparison

    instructions only work for 8-bit unsigned comparisons.

    Comparison example (==) using cpfseq

  • 8/7/2019 Chap4 Logctrl Ops

    35/49

    V 0.9 35

    Comparison example (==) using cpfsequnsigned char i,j;

    if (i == j) {j = i + j;

    }

    using cmpfseqsubtract/Z flag test

    movf j,w ; w jsubwf i,w ; w i jbnz end_if ;skip if Z=0

    movf i,w ; w iaddwf j,f ; j j + wend_if..do stuff..

    movf i,w ; w icpfseq j ; j == i?bra end_if ; i != j

    addwf j,f ; j

    j + iend_if..do stuff..

    Ccode

    did j==i so that w already had

    i in it to reduce instr. count

    The cmpfseq approach takes fewer instructions. Use this approach

    if you want to, but you will still have to understand the

    subtract/flag test approach for other comparison types later.

    Comparison example (>) using cpfsgt

  • 8/7/2019 Chap4 Logctrl Ops

    36/49

    V 0.9 36

    Comparison example (>) using cpfsgt

    using cpfsgtsubtract/C flag test

    movf j,w ; w jcpfsgt i ; i > j?bra end_if ; i j) {j = i + j;

    }

    movf i,w ;wisubwf j,w ; wjibc end_if ; skip if C=1

    movf i,w ; wiaddwf j,f ; jj + w

    end_if

    ..do stuff..

    No advantage the subtract/flag test is more general and

    you will need to understand it as it is used for other

    comparison types later.

    Unsigned Literal Comparison

  • 8/7/2019 Chap4 Logctrl Ops

    37/49

    V 0.9 37

    Unsigned Literal Comparison

    Careful! The sublw instruction performs literal w,

    not w literal.

    This is useful for comparing w to a literal.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    while loop

  • 8/7/2019 Chap4 Logctrl Ops

    38/49

    V 0.9 38

    p

    Observe that at the end of the loop, there is a jump back to

    loop_top after body is performed. The body of a while

    loop may not execute if loop test is initially false.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    do-while loop

  • 8/7/2019 Chap4 Logctrl Ops

    39/49

    V 0.9 39

    In do-while loop, body is always executed at least once.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    Aside: for loops in C

  • 8/7/2019 Chap4 Logctrl Ops

    40/49

    V 0.9 40

    Aside: forloops in C

    A forloop is just another way to write a while loop.

    Typically used to implement a counting loop (a loop that is

    executed a fixed number of times).

    unsigned char i,j;

    i = 0;

    while (i != 10) {k = k + j;i++;

    }/* ..do stuff..*/

    unsigned char i,j;

    for (i = 0; i!= 10; i++){k = k + j;

    }/* do stuff */

    These statements executed 10 times. Both code blocks

    are equivalent.

    executed once,

    before test.

    executed eachloop iteration

    after body

    loop test

    Decrement/Increment skip if 0 !0

  • 8/7/2019 Chap4 Logctrl Ops

    41/49

    V 0.9 41

    Decrement/Increment, skip if 0, !0

    For simple counting loops, where goal is to execute a block ofstatements a fixed number of times, the decrement/increment,

    skip if 0 instructions can be useful.

    decfsz floc ; decrement floc, skips next instruction if result == 0

    dcfsnz floc ; decrement floc, skips next instruction if result != 0

    incfsz floc ; increment floc, skips next instruction if result == 0

    infsnz floc ; increment floc, skips next instruction if result != 0

    Can use these for counting loops; replaces multiple instructionswith single instruction. The reason to use these instructions

    would be to save code space, and decrease loop execution time.

    Counting Loop Example

  • 8/7/2019 Chap4 Logctrl Ops

    42/49

    V 0.9 42

    Counting Loop Example

    Can be used for an end-of-loop action and test in some cases.

    Usage of incfsz/icfsnz/decfsz/dcfsnz is optional as other

    instruction sequences can accomplish the same thing. In this

    case the decfsz/bra can be replaced by decf/bnz

    combination that is just as efficient.Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    C i d Shift L ft Shift Ri ht

  • 8/7/2019 Chap4 Logctrl Ops

    43/49

    V 0.9 43

    Cunsigned Shift Left, Shift Right

    unsigned Shift right i >> 1

    all bits shift to right by one, 0 into MSB.

    b7 b6 b5 b4 b3 b2 b1 b0 original value

    i >> 1 (right shift by one)0 b7 b6 b5 b4 b3 b2 b1

    unsigned Shift left i

  • 8/7/2019 Chap4 Logctrl Ops

    44/49

    V 0.9 44

    PIC18 Rotate InstructionsPIC18 has rotate right through carry, rotate right, rotate left through

    carry, rotate leftinstructions

    rrcf floc, d ; d shifted to right by 1, MSB gets C flag,

    LSB goes into C flag

    b7 b6 b5 b4 b3 b2 b1 b0Cflagrotate

    right thru

    carry

    rlcf floc, d ; d shifted to right by 1, LSB gets C flag,

    MSB goes into C flag

    b7 b6 b5 b4 b3 b2 b1 b0Cflagrotateleft

    thru

    carry

    PIC18 Rotate Instructions (cont)

  • 8/7/2019 Chap4 Logctrl Ops

    45/49

    V 0.9 45

    ( )

    rrncf floc, d ; d rotated to right by 1

    rotate

    right, no

    carryb7 b6 b5 b4 b3 b2 b1 b0

    rlncf floc, d ; d rotated to left by 1

    rotate

    left, nocarry

    b7 b6 b5 b4 b3 b2 b1 b0

    C Shift operations using Rotates thru Carry

  • 8/7/2019 Chap4 Logctrl Ops

    46/49

    V 0.9 46

    C Shift operations using Rotates thru Carry

    For multiple shift, repeat single shift. Must clearCarry flag as

    status is unknown usually.

    Copyright Thomson/Delmar Learning 2005. All Rights

    Reserved.

    Wh Shift?

  • 8/7/2019 Chap4 Logctrl Ops

    47/49

    V 0.9 47

    Why Shift?

    Shift right by 1 is divide by 2 ( i >> 1 == i / 2 )

    Shift left by 1 is multiply by 2 ( i

  • 8/7/2019 Chap4 Logctrl Ops

    48/49

    V 0.9 48

    y

    The PIC18 instruction set has gotten cluttered with instructions asnew generations of PICmicro microcontrollers have been produced.

    New instructions were added, and old instructions were kept in an

    effort to keep assembly source compatibility (so that compilerscould be ported more easily).

    There are many ways for accomplishing the same thing using

    different instructions sequences.

    Which method do you use?

    The method that you understand......(and have not MEMORIZED).

    I will never take off points for inefficient code.

    I will always take off points for incorrect code close does not

    count.

    What do you need to know?

  • 8/7/2019 Chap4 Logctrl Ops

    49/49

    V 0.9 49

    What do you need to know?

    Logical operations (and,or,xor, complement)

    Clearing/Setting/Complementing groups of bits

    Bit set/clear/test instructions ==, !=, >, =, >), Shift Right (


Recommended