Docsity

Logical Control Operations-Microcomputers-Lecture 04 Slides-Electrical and Computer Engineering, Slides of Microcomputers

Toggle sidebar
bg1
1
Microcomputers
Logical and Control Operations
Dr. Jeff Jackson Lecture 4-1Electrical & Computer Engineering – Microcomputers
CArithmetic operators
Operator Description
+
,
-
(
+
)
addition
,
(
–
)
subtraction
,
() ,(
)
++, -- (++) increment, (––) decrement
*, / (*) multiplication, (/) division
>>, << right shift (>>), left shift (<<)
&, |, ^ bitwise AND (&), OR (|), XOR (^)
~ bitwise complement
Dr. Jeff Jackson Lecture 4-2Electrical & Computer Engineering – Microcomputers
The above are Coperators that we would like to implement in
PIC24 assembly language. Multiplication and division will
be covered in a later lecture.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Logical Control Operations-Microcomputers-Lecture 04 Slides-Electrical and Computer Engineering and more Slides Microcomputers in PDF only on Docsity!

Microcomputers

Logical and Control Operations

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

C Arithmetic operators

Operator Description +,, - (( )+) addition, ( , ( )–) subtraction ++, -- (++) increment, (– –) decrement , / () multiplication, (/) division

, << right shift (>>), left shift (<<) &, |, ^ bitwise AND (&), OR (|), XOR (^) ~ bitwise complement

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

The above are C operators that we would like to implement in PIC24 assembly language. Multiplication and division will be covered in a later lecture.

Bit-wise Logical operations

Bitwise AND operation AND.{B} Wb,Ws,Wd (Wb)&(Ws)Wd j = k & i; AND.{B}AND.{B} ff (f)&(WREG)(f)&(WREG) ff j = j & k;j j & k; AND.{B} f, WREG (f)&(WREG) WREG j = j & k; AND.{B} #lit10,Wn lit10 & (Wn) Wn j = j & literal;

Bitwise Inclusive OR operation IOR.{B} Wb,Ws,Wd (Wb) | (Ws)Wd j = k | i; IOR.{B} f (f) | (WREG) f j = j | k; IOR {B} f WREG (f) | (WREG) WREG j j | k

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

IOR.{B} f, WREG (f) | (WREG) WREG j = j | k; IOR.{B} #lit10,Wn lit10 | (Wn) Wn j = j | literal;

Bit-wise Logical operations (cont.)

Bitwise XOR operation XOR.{B} Wb,Ws,Wd (Wb) ^ (Ws)Wd j = k ^ i; XOR {B}XOR.{B} ff (f) ^ (WREG)(f) ^ (WREG) ff jj = j ^ k;j ^ k XOR.{B} f, WREG (f) ^ (WREG) WREG j = j ^ k; XOR.{B} #lit10,Wn lit10 ^ (Wn) Wn j = j ^ literal;

Bitwise complement operation COM.{B} Ws,Wd ~ (Ws)Wd j = ~k; COM.{B} f ~(f) f j = ~j ;

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

COM.{B} f (f) f j j ; COM.{B} f, WREG ~(f) WREG j = ~k;

Setting a group of bits

Location contents (i) 0x0800 0x2C

Data Memory

Set bits b3:b1 of j (^) (i) 0x0800 0x2C

(j) 0x0801 0xB (k) 0x0802 0x8A

In C : uint8 j; j = j | 0x0E;

In PIC24 C assembly mov.b #0x0E, W0 ; W0 = mask

j = 0xB2 = 1011 0010 |||| |||| mask= 0x0E = 0000 1110

The ‘mask’

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

ior.b j ; j = j | 0x0E

**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

Complementing a group of bits

Location contents (i) 0x0800 0x2C

Data Memory

Complement bits b7:b6 of k (^) (i) 0x0800 0x2C

(j) 0x0801 0xB (k) 0x0802 0x8A

In C : uint8 k; k = k ^ 0xC0;

In PIC24 C assembly mov.b #0xC0, W0 ; W0 = mask

k = 0x8A = 1000 1010 ^^^^ ^^^^ mask= 0xC0 = 1100 0000

The ‘mask’

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

xor.b k ; k = k ^ 0xC

**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

Complementing all bits

Location contents

Data Memory

Complement all bits of k (i) 0x0800 0x2C (j) 0x0801 0xB (k) 0x0802 0x8A

In C : uint8 k; k = ~k ;

In PIC24 C assembly com.b k ; k = ~k

k = 0x8A = 1000 1010

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

After complement

result = 0111 0101 = 0x

Bit set, Bit Clear, Bit Toggle instructions

Can set/clear/complement one bit of a data memory location by using the AND/OR/XOR operations, but takes multiple instructions as previously seen. The bit clear ( bcf ), bit set ( bsf ), bit toggle ( btg ) instructions clear/set/complement one bit of data memory or working registers using one instruction.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Carry, Zero Flags

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

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

These flags are set as side-effects of particular instructions or can be set/cleared explicitly using the bset / bclr instructions.

How do you know if an instruction affects C, Z flags? Look at Table 19-2 in PIC24HJ32GP202 C datasheeet.– add affects all ALU flags, mov f only Z, N flags, and mov f, Wn no flags.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Mnemonic Syntax. Desc # of Instr Status words Cycles affected ADD |ADD f | f=f+WREG| 1 | 1 | C,DC,Z,OV,N MOV |MOV f,Wn | Wn=(f) | 1 | 1 | none MOV |MOV f | f = (f) | 1 | 1 | N,Z

Addition: Carry, Zero Flags

In addition, carry flag is set if there is a carry out of the MSbit. In byte (8-bit) mode, C=1 if sum > 255 (0xFF)

Zero flag is set if result is zero.

y ( ) ( ) In word (16-bit) mode, C=1 if sum > 65535 (0xFFFF)

0xF +0x


0x10 Z=0,

0x +0x


0x00 Z=1,

0x +0xFF


0x00 Z=1,

0x +0x7F


0xFF Z=

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

C=

C=

C=

0xFF Z 0, C=

Byte mode operations are shown.

Subtraction: Carry, Zero Flags

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

Zero flag is set if result is zero.

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

0xF

  • 0x

0x -0x


0 00 Z 1

0x -0xFF


0 02 Z 0

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

0xD0 Z=0, C=

0x00 Z=1, C=

0x02 Z=0, C=

For a subtraction, the combination of Z=1, C=0 will not occur. Byte mode operations are shown.

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 two’s complement of B (more on this later). The C flag is affected by the addition of A + (~B) + 1

0xF

  • 0x

0xF

  • 0xDF
  • 0x

0x20 = 0010 0000 ~0x20 = 1101 1111 = 0xDF

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

0xD0 Z=0, C=

0xD0 Z=0, C=

= 0xDF

No borrow, C=

Carry out of MSB, so C=

PIC24 Family Left Shifts

Cflag b7 b6 b5 b4 b3 b2 b1 b

Shift left 0 Cfl b15 b14 b1 b0 0

8-bit Cflag b15 b14..... b1 b0 0 16 bit 16 -bit

Descr: Syntax Operation Shift left f SL{.B} f f << 1  f SL{.B} f,WREG f << 1  WREG

Shift left Ws SL{.B} Ws,Wd Ws << 1  Wd Shift left by SL Wb, #lit4, Wd Wb << lit4  Wd

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

y , , short Literal Shift left by Ws SL Wb, Ws, Wd Wb << Ws  Wd The last two shifts can shift multiple positions in one instruction cycle (up to 15 positions), but only as word operations.

PIC24 Rotate Instructions

PIC24 has some rotate left and rotate right instructions as well:

Cflag msb........ lsb

rotate right thru carry

Cflag msb........ lsb

rotate left thru carry

msb........ lsb

rotate right no carry

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

msb........ lsb

rotate left no carry

We will not cover these instructions since their need is specialized; the shift instructions are sufficient for C language shift operations.

C Shift operations

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

It is sometimes more efficient to repeat a single position shift instruction performing a multi-bit shift.

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Mixed 8-bit, 16-bit operations

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Number Sequencing Task using btsc

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

mov.b wreg,out

C Conditional Tests

Operator Description == , != equal, not-equal

, >= greater than, greater than or equalh h l <, <= less than, less than or equal && logical AND | | logical OR ! logical negation

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

If an operator used in a C conditional test, such as an IF statement or WHILE statement, returns nonzero, then the condition test is TRUE.

Logical Negation vs. Bitwise Complement

!i (^) is not the same as (^) ~i

!(i) 0

i = 0xA

~(i) 0x5F

i = 0xA

Logical operations: !, &&, || always treat their operands as either being zero or non-zero, and the returned result is always either 0 or 1.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

always either 0 or 1.

Examples of C Equality, Inequality, Logical,

Bitwise Logical Tests

uint8 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 1 a_eq_b = (a == b); // a_eq_b result is 0 a_gt_b = (a > b); // a_gt_b result is 0 a_ne_b = (a != b); // a_ne_b result is 1

uint8 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

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

(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 0xF Copyright Delmar Cengage Learning 2008. All Rights Reserved.

C equality tests

A common C code mistake is shown below (= vs == ) if (i = 5) { if (i == 5) {

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

if (i = 5) { j = i + j; } //wrong

if (i == 5) { j = i + j; } // right

Always executes because i=5 returns 5

The test i == 5 returns a 1 only when i is 5. The

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

because i=5 returns 5, so conditional test is always non-zero, a true value. The = is the assignment operator.

1 only when i is 5. The == is the equality operator.

C Bitwise 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.zero.

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

If ( (i is nonzero) AND (j is nonzero) ) then do this.

if (i & j) { / do this /

If ( (i bitwise AND j) is nonzero) ) then do this.

is read as:

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

} nonzero) ) then do this.

(i && j) (i & j)

i = 0xA0, j = 0x0B; 1

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

C Bitwise 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.zero.

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

If ( (i is nonzero) OR (j is nonzero) ) { do...

if (i | j) { / do this /

If ( (i bitwise OR j) is nonzero) ) { do....

is read as:

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

} nonzero) ) { do....

(i || j) (i | j)

i = 0xA0, j = 0x0B; 1

i = 0xA0, j = 0x0B;

0xAB

Non-Zero Test

labels for SFRs defined in p24Hxxxx.inc; use ffor clarity!!!! l i !!!!

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

The mov k instruction just moves k back onto itself! Does no useful work except to affect the Z, N flags. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

General if-else form with branches

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Choose the branch instruction such that the branch is TAKEN when the condition is FALSE. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Equality Test (==)

Subtraction operation of k j performed to check equality;

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

Subtraction operation of k-j performed to check equality; if k == j then subtraction yields ‘0’, setting the Z flag. Does not matter if k-j or j-k is performed. Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.

Inequality tests using Z, C flags and subtraction

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

k>j test using k-j

The false condition of k>j is k<=j so use k<=j to skip around

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 4-

The false condition of k>j is k<=j, so use k<=j to skip around the if-body. For the k-j test, this is accomplished by C=0 or Z=1, requiring two branches.

Copyright Delmar Cengage Learning 2008. All Rights Reserved. From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.