microcontroller fundamentals & programming

28
1 Microcontroller Fundamentals & Programming Logical Instructions

Upload: boris-ross

Post on 30-Dec-2015

50 views

Category:

Documents


2 download

DESCRIPTION

Microcontroller Fundamentals & Programming. Logical Instructions. Logic Instructions. Consist AND , OR, EOR (XOR) and COM instructions. Contents of accumulator operated logically, bit by bit, with corresponding bit in the memory. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Microcontroller Fundamentals & Programming

1

Microcontroller Fundamentals &

Programming

Logical Instructions

Page 2: Microcontroller Fundamentals & Programming

2

Logic Instructions

Consist AND, OR, EOR (XOR) and COM instructions.

Contents of accumulator operated logically, bit by bit, with corresponding bit in the memory.

The result after logic instruction will be placed back in accumulator.

C flag will not change. Only N and Z flags will change according to the result.

Page 3: Microcontroller Fundamentals & Programming

3

Logic Instructions – Block Diagram

Accumulator

Result back to accumulator Memory

0 0 1 1 1 1 0 0

0 0 0 0 1 1 1

1

AND, OR, EOR operation

CCRN Z

N & Z flags change

Page 4: Microcontroller Fundamentals & Programming

4

ANDA , ANDB

AND operation is used to clear or mask off any bit to “0” with other bits unchanged.

Example: To clear bit 0 to bit 3 of ACCA

LDAA #$5A $5A = 0101 1010

ANDA #$F0 ; $F0 = 1111 0000 AND Operation

; $50 = 0101 0000 Results go to ACCA

Result: ACCA = $50 ; Flag bits : N = “0” , Z = “0”

AND Instructions

Page 5: Microcontroller Fundamentals & Programming

5

ORAA , ORAB

OR operation is used to set any bit to “1” with other bits unchanged.

Example: To set bit 0 and 7 of ACCB

LDAB #$1A ; $1A = 0001 1010

ORAB #$81 ; $81 = 1000 0001 OR Operation

; $9B = 1001 1011 Results to ACCB

Result: ACCB = $9B ; Flag bits : N = “1” , Z = “0”

OR Instructions

Page 6: Microcontroller Fundamentals & Programming

6

EORA , EORB (Exclusive OR)

EOR operation is used to invert any bits from a “1” to “0” and vice versa in accumulators A or B.

Example: Invert Bit-0 ~ 3 of ACCB

LDAB #$5A ; $5A = 0101 1010

EORB #$0F ; $0F = 0000 1111 EOR Operation

; $55 = 0101 0101 Results to ACCB

Result: ACCB =$55 ; Flag bits : N = “0” , Z = “0”

EOR Instructions

Page 7: Microcontroller Fundamentals & Programming

7

Complement InstructionsExample:

COMA, COMB, COM

Instruction will invert all the 8-bit contents in accumulator A or B or memory location.

N and Z flag bits will change according to the result.

C flag will set to “1”.

Page 8: Microcontroller Fundamentals & Programming

8

MNEMONICS COMMENTS

CLRA ; ACCA = $00

COMA ; ACCA = $FF

LDAB #$F0 ; ACCB = $F0

COMB ; ACCB = $0F

CLR $8000 ; contents of memory location

; ($8000) = $00

COM $8000 ; contents of memory location

; ($8000) = $FF.

Example: Complement Instructions

Page 9: Microcontroller Fundamentals & Programming

9

Example:

BITA , BITB

An 8-bit logical test instructions.

Bit instructions operate similar to AND operation.

After executing Bit instructions :

• no result will be saved.

• only the flags in CCR are updated.

• N and Z flag bits will change according to the result.

• V flag will automatically set to “0”.

Bit Instructions

Page 10: Microcontroller Fundamentals & Programming

10

Example: Bit Instructions

LDAA $1000 ; the 8-bit data in memory location

; ($1000) = %1100 0000

BITA #%00110011 ; %11000000 AND with ; %00110011 ; Results = (%00000000) ; N = “0” , Z = “1”

BNE READKEY ; result not equal zero ; branch to READKEY routine.

Let value in memory ($1000) = %1100 0000

Page 11: Microcontroller Fundamentals & Programming

11

Bit Instructions

Instructions Addressing modes

Comments

BITA #$35 IMM ACCA AND with $35

BITB #%00001100 IMM ACCB AND with $0C

BITB $90 DIR ACCB AND with data in memory $90

BITA $8000 EXT ACCA AND with data in memory ($8000)

BITA $10,X IND,X ACCA AND with data in memory ($10 + X)

Page 12: Microcontroller Fundamentals & Programming

12

Example:

BCLR, BSET

Bit clear instructions will clear any 8-bit directly

in memory location.

Bit set instructions will set any 8-bit directly in

memory location.

N and Z flag bits will change according to the

result.

V flag will set to “0”.

Bit Clear and Bit Set Instructions

Page 13: Microcontroller Fundamentals & Programming

13

Instructions Addressing Comments Modes

LDX #$2000 IMM IX = $2000

BCLR $10,$30 DIR bit 4,5 of memory ($10) are cleared to “0”.

BSET $20,$0C DIR bit 2,3 of memory ($20) are set to “1”

BCLR $10,X,$30 IND,X bit 4,5 of memory ($10+X)

are cleared to “0”.

Example: Bit Clear and Bit Set Instructions

Page 14: Microcontroller Fundamentals & Programming

14

Instructions Addressing Comments Modes

LDX #$2000 IMM IX = $2000

BCLR $10,%00110000 DIR bit 4,5 of memory ($10) are cleared to “0”.

BSET $20,%00001100 DIR bit 2,3 of memory ($20) are set to “1”

BCLR $10,X,%00110000 IND,X bit 4,5 of memory ($10+X)

are cleared to “0”.

Example: Bit Clear and Bit Set Instructions

Page 15: Microcontroller Fundamentals & Programming

15

Shift & Rotate Instructions Consist of Logical Shift , Arithmetic Shift

and Rotate instructions.

Move 8-bit values in accumulator or memory, including the C-flag, by 1-bit to the left or right.

N, Z and C flags in CCR will change according to result.

Types of addressing modes : INH, EXT, INDX & INDY.

Page 16: Microcontroller Fundamentals & Programming

16

Example: Shift Left : LSL , LSLA , LSLB

Logical Shift Left:

shift 8-bit value 1 place to the left. Bit-7 is shifted into C-bit .a “0” is placed into bit-0.

Shifting binary number 1 bit to the left each time will multiply the original number by 2.

“0”B7 B6 B5 B4 B3 B2 B1

B0C

Memory orAccumulator

Logical Shift Left Instructions

Page 17: Microcontroller Fundamentals & Programming

17

Example: Logical Shift Left instructionsMnemonics C-flag ACCA Decimal

Value

LDAA #$22 0 0010 0010 3410

LSLA 0 0100 0100 6810

LSLA 0 1000 1000 13610

LSLA 1 0001 0000 1610

LSLA 0 0010 0000 3210

Page 18: Microcontroller Fundamentals & Programming

18

Example: Shift Right : LSR , LSRA , LSRB

Logical Shift Right:

• shift the 8-bit value 1 place to the right. •Bit-0 is shifted into C-bit •a “0” is placed into bit-7.

Shifting binary number 1 bit to the right

each time will divide the original number by

2.

B7 B6 B5 B4 B3 B2 B1 B0

C“0”

Memory orAccumulator

Logical Shift Right Instructions

Page 19: Microcontroller Fundamentals & Programming

19

Example: Logical Shift Right instructionsMnemonics ACCA C-flag Decimal Value

LDAA #$24 0010 0100 0 3610

LSRA 0001 0010 0 1810

LSRA 0000 1001 0 0910

LSRA 0000 0100 1 0410

LSRA 0000 0010 0 0210

Page 20: Microcontroller Fundamentals & Programming

20

Arithmetic Shift Left Instructions

Arithmetic Shift Left:

- Operation same as Logical shift left .

Example:

Shift Left : ASL , ASLA , ASLB , ASLD

“0”B7 B6 B5 B4 B3 B2 B1

B0C

Memory orAccumulator

Page 21: Microcontroller Fundamentals & Programming

21

Example: Arithmetic Shift Left instructions

Mnemonics Accumulator A

LDAA #$32 0 0 1 1 0 0 1 0

ASLA 0 1 1 0 0 1 0 0

Mnemonics Accumulator A

LDAA #$32 0 0 1 1 0 0 1 0

LSLA 0 1 1 0 0 1 0 0

Arithmetic Shift Left is same as Logical Shift Left .

Page 22: Microcontroller Fundamentals & Programming

22

Arithmetic Shift Right Instructions

Arithmetic Shift Right: shift 8-bit data (accumulator or memory) 1 place to the right.

Bit-0 data is shifted into C-bit.

The sign bit or bit-7 remains unchanged.

Example:

Shift Right : ASR , ASRA , ASRB

B7 B6 B5 B4 B3 B2 B1 B0

C

Memory orAccumulator

Page 23: Microcontroller Fundamentals & Programming

23

Example: Arithmetic Shift Right instructions

Mnemonics Accumulator B C-flag

LDAB #$C2 1 1 0 0 0 0 1 0 0

ASRB 1 1 1 0 0 0 0 1 0

ASRB

1 1 1 1 0 0 0 0 1

ASRB 1 1 1 1 1 0 0 0 0

Page 24: Microcontroller Fundamentals & Programming

24

Rotate Left Instructions

Rotate Left :

shift 8-bit value (accumulator or memory) 1 place to the left.

Bit-7 is shifted into C-bit and

C-bit is placed into bit-0.

Example:

Rotate Left : ROL , ROLA , ROLB

B7 B6 B5 B4 B3 B2 B1 B0

C

Page 25: Microcontroller Fundamentals & Programming

25

Example: Rotate Left instructions

Mnemonics C-flag Accumulator A

LDAA #$F0 0 1 1 1 1 0 0 0 0

ROLA 1 1 1 1 0 0 0 0 0

ROLA 1 1 1 0 0 0 0 0 1

ROLA 1 1 0 0 0 0 0 1 1

Page 26: Microcontroller Fundamentals & Programming

26

Rotate Right Instructions

Rotate Right :

shift 8-bit value (accumulator or memory) 1 place to the right.

Bit-0 is shifted into C-bit and

C-bit is placed into bit-7.

C

B7 B6 B5 B4 B3 B2 B1 B0

Example:

Rotate Right : ROR , RORA , RORB

Page 27: Microcontroller Fundamentals & Programming

27

Example: Rotate Right instructions

Mnemonics Accumulator B C-flag

LDAB #$0F 0 0 0 0 1 1 1 1 0

RORB 0 0 0 0 0 1 1 1 1

RORB 1 0 0 0 0 0 1 1 1

RORB 1 1 0 0 0 0 0 1 1

Page 28: Microcontroller Fundamentals & Programming

28

Thank You