280 likes | 431 Views
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.
E N D
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. • 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.
CCR N Z N & Z flags change Result back to accumulator Memory 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 Accumulator AND, OR, EOR operation Logic Instructions – Block Diagram
AND Instructions • 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”
OR Instructions • 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”
EOR Instructions • 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”
Complement Instructions • Example: • 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”.
Example: Complement Instructions MNEMONICSCOMMENTS 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.
Bit Instructions • 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”.
Example: Bit Instructions Let value in memory ($1000) = %1100 0000 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.
Bit Clear and Bit Set Instructions • 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”.
Example: Bit Clear and Bit Set Instructions 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 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”.
Shift & Rotate Instructions • Consist of Logical Shift , Arithmetic Shiftand Rotateinstructions. • 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.
“0” B7 B6 B5 B4 B3 B2 B1 B0 C Logical Shift Left Instructions Example: Shift Left : LSL , LSLA , LSLB Memory or Accumulator • 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 B0 C Logical Shift RightInstructions 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. Memory or Accumulator • 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 “0” C Arithmetic Shift Left Instructions Example: Shift Left : ASL , ASLA , ASLB , ASLD Memory or Accumulator • ArithmeticShift Left: • - Operation same as Logical shift left .
Example: Arithmetic Shift Left instructions ArithmeticShift Leftis same as Logical Shift Left .
B7 B6 B5 B4 B3 B2 B1 B0 C Arithmetic Shift Right Instructions Example: Shift Right : ASR , ASRA , ASRB • ArithmeticShift 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. Memory or Accumulator
B7 B6 B5 B4 B3 B2 B1 B0 C Rotate Left Instructions Example: Rotate Left : ROL , ROLA , ROLB 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 instructions MnemonicsC-flagAccumulator 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
B7 B6 B5 B4 B3 B2 B1 B0 C Rotate Right Instructions Example: Rotate Right : ROR , RORA , RORB 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.
MnemonicsAccumulator BC-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 Example: Rotate Right instructions