Arithmetic and Logic Instructions Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.tw
Find the sum of the values from 40H to 43H. Put the sum in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x0 MOVWF H_BYTE 0x40 ADDLW 0x41 BNC N_2 INCF H_byte, F N_2 ADDWF 0x42, W N_3 ADDLW 0x43,W BNC N_4 INCF H_byte, F N_4 MOVWF L_Byte 2
Add 16-bit numbers of 3CE7H and 3B8DH. Put the sum in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x8D MOVWF L_BYTE 0x3B MOVWF H_BYTE ADDWF ADDWFC 0xE7 L_BYTE, F 0x3C H_BYTE, F 3
BCD (binary coded decimal) number system Unpacked BCD 59 is represented by 0000 0101 and 0000 1001 Packed BCD 59 is represented by 59H 0101 1001 Efficient in storing data 4
DAW instruction 0x47 ; WREG = 47H ADDLW 0x25 ; WREG = 6CH, DC = 0, C = 0 DAW ; Adjust for BCD addition by adding 6 (WREG=72H) C =0 0x09 ; WREG = 09H ADDLW 0x08 ; WREG = 11H, DC = 1, C = 0 DAW ; Adjust for BCD addition by adding 6 (WREG=17H) C =0 0x52 ; WREG = 52H ADDLW 0x87 ; WREG = D9H, DC = 0, C =0 DAW ; Adjust for BCD addition by adding 60H (WREG=39H) C =1 0x57 ; WREG = 57H ADDLW 0x77 ; WREG = CEH, DC = 0, C =0 DAW ; Adjust for BCD addition by adding 66H (WREG=34H) C =1 Add 0110 to lower 4 bits if lower nibble > 9 or if DC = 1 Add 0110 to upper 4 bits if upper nibble >9 or if C = 1 5
Subtraction of unsigned numbers 0x23 SUBLW 0x3F ; WREG = 3F WREG 3F 23 = 3F + 2 s complement of 23 = 3F + DC + 1 = 1 1C C = 1 N = 0 (D7 is negative flag) 6
Subtract 4C 6E using SUBWF MYREG EQU 0x20 0x4C MOVWF MYREG 0x6E SUBWF MYREG, W ; WERG = MYREG - WREG BNN NEXT NEGF WREG ; take 2 s complement NEXT MOVWF MYREG 4C 6E = 4C + 91 + 1 = DE C = 0 N = 1 (negative, FF-DE+1, -22) 7
Subtract two 16-bit numbers: 2762H - 1296H. Put the difference in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x62 MOVWF L_BYTE 0x27 MOVWF H_BYTE SUBWF SUBWFC 0x96 L_BYTE, F ; F = F W = 62 96 = CCH ; C = borrow = 0, N = 1 0x12 H_BYTE, F ; F = F W borrow ; = 27 12 1 = 14H ; 2762H - 1296H = 14CCH 8
Multiplication of unsigned numbers MULLW 0x25 0x65 25H x 65H = E99H special function registers PRODH = 0EH PRODL = 99H 9
Division of unsigned numbers by repeat subtraction NUM EQU 0x19 ; reminder MYQ EQU 0x20 ; quotient MYNMB EQU D 95 ; numerator MYNDEN EQU D 10 ; denominator CLRF MYQ MYNMB MOVWF NUM MYDEN B1 INCF MYQ, F SUBWF NUM, F BC B1 ; keep subtraction until C = 0 DECF MYQ, F ADDWF NUM, F 10
Signed number arithmetic Positive vs. negative numbers Range for 8-bit number Unsigned numbers: 0 ~ 255 Signed numbers: -128 ~ 127 11
Overflow in signed number operation OV is set if either two conditions occurs 1. A carry from D6 to D7 but no carry out of D7 (C=0) 2. A carry from D7 out (C=1) but no carry from D6 to D7 ADDLW +D 96 +D 70 ; 0110 0000 + 0100 0110 = 1010 0110 (A6H) ; N = 1, OV = 1, sum = -90 ; condition 1 ADDLW -D 128 -D 2 ; 1000 0000 + 1111 1110 = 0111 1110 (7EH) ; N = 0, OV = 1, sum = 126 ; condition 2 12
Overflow in signed number operation (cont.) ADDLW -D 2 -D 5 ; 1111 1110 + 1111 1011 = 1111 1001 (F9H) ; N = 1, OV = 0, sum = -7 ADDLW +D 7 +D 18 ; 0000 0111 + 0001 0010 = 0001 1001 (19H) ; N = 0, OV = 0, sum = 25 ; condition 2 13
Logic operations ANDLW K IORLW K ; Inclusive OR XORLW K ; Exclusive OR 14
RB2 and RB5 of PORTB are used to control an outdoor light and indoor light respectively. BCF TRISB, 2 BCF TRISB, 5 BSF PORTB, 2 BCF PORTB, 5 BCF TRISB, 2 BCF TRISB, 5 B 00000100 IORWF PORTB, F B 11011111 ANDWF PORTB, F 15
Test PortB to see whether it has value 45H CLRF CLRF SETF XORWF BNZ MOVWF EXIT TRISC PORTC TRISB 0x45 PORTB, W EXIT 0x99 PORTC 16
Compare instructions 17
Monitor PORTD for the value 63H SETF BACK CPFSEQ BRA TRISD 0x63 PORTD BACK 18
PORTD is connected to a temperature sensor. Test it for the value 75 if (T > 75) GREG=T; else if (T<75) LREG=T; else WREG=T; LREG EQU 0x20 GREG EQU 0x21 SETF TRISD D 75 CPFSGT PORTD BRA LEQ MOVFF PORTD, GREG BRA OVER LEQ CPFSLT PORTD BRA OVER MOVFF PORTD, LREG OVER 19
Rotate instructions 7 6 5 4 3 2 1 0 C 7 6 5 4 3 2 1 0 Figure 2.17 Operation performed by the rlcf f,d,a instruction Figure 2.18 Operation performed by the rlncf f,d,a instruction C 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 Figure 2.19 Operation performed by the rrcf f,d,a instruction Figure 2.20 Operation performed by the rrncf f,d,a instruction 20
Do multiplication/division of unsigned numbers by rotating left/right through carry MYREG EQU 0x20 0x25 MOVWF MYREG MYREG EQU 0x20 0xD5 MOVWF MYREG BCF STATUS, C BCF STATUS, C RLCF MYREG, F RRCF MYREG, F BCF STATUS, C BCF STATUS, C RLCF MYREG, F RRCF MYREG, F 21
Find number of 1s in a given byte R1 EQU 0x20 COUNT EQU 0x21 VALREG EQU 0x22 CLRF R1 0x8 MOVWF RCNT 0x97 MOVWF VALREG AGAIN RLCF VALREG, F BNC NEXT INCF R1, F NEXT DECF COUNT, F BNZ AGAIN 22
SWAPF filereg, d MYREG EQU MOVWF SWAPF 0x20 0x72 MYREG MYREG, F 23
Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 24