Arithmetic and Logic g Instructions Hsiao-Lung H i L Chan Ch Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail cgu edu tw [email protected] 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 H_Byte N_1 N_2 N_3 N 4 N_4 EQU EQU MOVLW MOVWF ADDLW BNC INCF ADDLW BNC INCF ADDWF … ADDLW BNC INCF MOVWF 0x6 0x7 0x0 H_BYTE 0x40, W N_1 H_byte, F 0x41 N_2 H_byte, y ,F 0x42, W 0x43,W , N_4 H_byte, F L Byte 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 H_Byte EQU EQU MOVLW MOVWF MOVLW MOVWF 0x6 0x7 0x8D L BYTE L_BYTE 0x3B H_BYTE MOVLW ADDWF MOVLW ADDWFC 0xE7 L_BYTE,, F 0x3C H_BYTE, F 3 BCD ((binaryy coded decimal)) number system y Unpacked BCD 59 is represented by “0000 0101” and “0000 1001” Packed BCD 59 is represented p byy 59H “0101 1001” Efficient in storing data 4 DAW instruction MOVLW ADDLW DAW 0x47 ; WREG = 47H 0x25 ; WREG = 6CH, DC = 0, C = 0 ; Adjust for BCD addition by adding 6 (WREG=72H) MOVLW ADDLW DAW 0x09 ; WREG = 09H 0x08 ; WREG = 11H, DC = 1, C = 0 ; Adjust for BCD addition by adding 6 (WREG=17H) MOVLW ADDLW DAW 0x52 ; WREG = 52H 0x87 ; WREG = D9H,, DC = 0,, C =1 ; Adjust for BCD addition by adding 60H (WREG=39H) MOVLW ADDLW DAW 0x57 ; WREG = 57H 0x77 ; WREG = CEH, DC = 0, C =1 ; Adjust for BCD addition by adding 66H (WREG=34H) 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 g numbers MOVLW SUBLW 0x23 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 g SUBWF MYREG NEXT EQU Q MOVLW MOVWF MOVLW SUBWF BNN NEGF MOVWF 0x20 0x4C MYREG 0x6E MYREG, W NEXT WREG MYREG ; WERG = MYREG - WREG ; take 2’s complement 4C – 6E = 4C + 91 + 1 = DE C = 0 N = 1 (negative, ( i FF DE 1 -22) 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 H_Byte EQU EQU MOVLW MOVWF MOVLW MOVWF 0x6 0x7 0x62 L BYTE L_BYTE 0x27 H_BYTE MOVLW SUBWF 0x96 L_BYTE,, F MOVLW SUBWFC 0x12 H BYTE F H_BYTE, ; F = F – W = 62 – 96 = CCH ; C = borrow = 0, N = 1 ; F = F – W – borrow ; = 27 – 12 – 1 = 14H ; 2762H - 1296H = 14CCH 8 Multiplication p of unsigned g numbers MOVLW MULLW 0x25 0x65 25H x 65H = E99H special function registers PRODH = 0EH PRODL = 99H 9 Division of unsigned numbers by repeat subtraction NUM MYQ MYNMB MYNDEN EQU EQU EQU EQU 0x19 0x20 D’95’ D’10’ D 10 B1 CLRF MOVLW MOVWF MOVLW INCF SUBWF BC DECF ADDWF MYQ MYNMB NUM MYDEN MYQ, F NUM, F B1 MYQ, F NUM, F ; reminder ; quotient ; numerator ; denominator ; keep subtraction until C = 0 10 Convert an 8-bit hexadecimal to decimal. Save the digits in location 0x22 0x22, 0x23 0x23, and 0x24, 0x24 NUME QU RMND_L RMND M RMND_M RMND_H MYNUM MYNDEN EQU EQU EQU EQU EQU EQU EQU MOVLW MOVWF MOVLW CLRF D_1 INCF SUBWF BC 0x15 0x20 0x22 0x23 0x24 0xFD D’10’ ; reminder ; quotient MYNUM NUME MYDEN QU QU, F NUME, F D_1 ADDWF DECF MOVFF NUME, F QU, F NUME, RMND_L MOVFF CLRF D_2 INCF SUBWF BC ADDWF DECF MOVFF QU, NUME QU, F QU, F NUME, F D_2 NUME, F QU, F NUME RMND_M NUME, RMND M MOVFF QU, RMND_H 11 Signed g number arithmetic Positive vs. negative numbers Range for 8-bit number Unsigned numbers: 0 ~ 255 Signed numbers: -128 ~ 127 12 Overflow in signed g number operation p OV is set if either two conditions occurs 1. 2. A carry from D6 to D7 but no carry out of D7 (C=0) A carry from D7 out (C=1) but no carry from D6 to D7 MOVLW ADDLW +D’96’ +D’70’ 0 MOVLW ADDLW -D’128’ -D D’2’ 2 ; 1000 0000 + 1111 1110 = 0111 1110 (7EH) ; N = 0, OV = 1, sum = 126 ; condition 2 ; 0110 0 0 0000 + 0100 0 00 0110 0 0 = 1010 0 0 0110 0 0 (A6H) ( 6 ) ; N = 1, OV = 1, sum = -90 ; condition 1 13 Overflow in signed g number operation p (cont.) ( ) MOVLW ADDLW -D’2’ -D’5’ MOVLW ADDLW +D’7’ +D’18’ +D 18 ; 1111 1110 + 1111 1011 = 1111 1001 (F9H) ; N = 1, OV = 0, sum = -7 ; 0000 0111 + 0001 0010 = 0001 1001 (19H) ; N = 0, OV = 0, sum = 25 ; condition 2 14 Logic g operations p ANDLW K IORLW K XORLW K ; Inclusive OR ; Exclusive OR 15 RB2 and RB5 of PORTB are used to control an outdoor light and indoor light respectively. respectively BCF BCF BSF BCF TRISB, 2 TRISB, 5 PORTB, 2 PORTB, 5 BCF BCF MOVLW IORWF MOVLW ANDWF TRISB, 2 TRISB, 5 B’00000100’ B 00000100 PORTB, F B’1101111’ B 1101111 PORTB, F 16 Test PortB to see whether it has value 45H CLRF CLRF SETF MOVLW XORWF BNZ MOVLW MOVWF EXIT … TRISC PORTC TRISB 0x45 PORTB W PORTB, EXIT 0x99 PORTC 17 Compare p instructions 18 Monitor PORTD for the value 63H SETF MOVLW BACK CPFSEQ BRA TRISD 0x63 PORTD BACK 19 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 GREG LEQ OVER EQU Q EQU SETF MOVLW CPFSGT BRA MOVFF BRA CPFSLT BRA MOVFF … 0x20 0x21 TRISD D’75’ PORTD LEQ PORTD, GREG OVER PORTD OVER PORTD LREG PORTD, 20 Rotate instructions 7 6 5 4 3 2 1 0 C Figure 2.17 Operation performed by the rlcf f,d,a instruction C 7 6 5 4 3 2 1 0 Figure 2.19 Operation performed by the rrcf f,d,a instruction 7 6 5 4 3 2 1 0 Figure 2.18 Operation performed by the rlncf f,d,a instruction 7 6 5 4 3 2 1 0 Figure 2.20 Operation performed by the rrncf f,d,a instruction 21 Do multiplication/division of unsigned numbers by rotating left/right through carry MYREG EQU Q 0x20 MOVLW 0x25 MOVWF MYREG MYREG EQU Q 0x20 MOVLW 0xD5 MOVWF MYREG BCF RLCF STATUS, C MYREG F MYREG, BCF RRCF STATUS, C MYREG F MYREG, BCF RLCF STATUS, C MYREG, F BCF RRCF STATUS, C MYREG, F 22 Do multiplication/division of signed numbers by rotating left/right through carry MYREG EQU Q 0x20 MOVLW 0xD5 MOVWF MYREG BCF RLCF STATUS, C MYREG F MYREG, BCF RLCF STATUS, C MYREG, F MYREG EQU Q 0x20 MOVLW 0xD5 MOVWF MYREG BCF BTFSC BSF RRCF STATUS, C MYREG 7 MYREG, STATUS, C MYREG, F BCF BTFSC BSF RRCF STATUS, C MYREG 7 MYREG, STATUS, C MYREG F MYREG, 23 Series a byte of data Transfer value 41H seriallyy via pin p RB1. Put one high g at the start and end of the data. Send the LSB first. RCNT EQU Q MYREG EQU BCF MOVLW MOVWF MOVLW MOVWF 0x20 0x21 TRISB, 1 0x41 MYREG 0x8 RCNT BSF AGAIN RRCF BNC BSF BRA OVER BCF NEXT DECF BNZ BSF PORTB,, 1 MYREG, F OVER PORTB, 1 NEXT PORTB 1 PORTB, RCNT, F AGAIN PORTB, 1 24 Input series data: Bring g in a byte y of data seriallyy via RC7 and save it in file register location 0x21. The byte comes in with the LSB first. RCNT EQU Q MYREG EQU 0x20 0x21 BSF MOVLW MOVWF AGAIN BTFSC BSF BTFSS BCF RRCF DECF BNZ TRISC, 7 0x8 RCNT PORTC, 7 STATUS, C PORTC, 7 STATUS, C MYREG F MYREG, RCNT, F AGAIN 25 Find number of 1s in a g given byte y R1 EQU Q COUNT EQU VALREG EQU CLRF MOVLW MOVWF MOVLW MOVWF AGAIN RLCF BNC INCF NEXT DECF BNZ 0x20 0x21 0x22 R1 0x8 RCNT 0x97 VALREG VALREG, F NEXT R1, F COUNT F COUNT, AGAIN 26 SWAPF fileReg, g d MYREG EQU MOVLW MOVWF SWAPF 0x20 0x72 MYREG MYREG, F 27 Packed BCD to ASCII conversion BCD_VAL EQU Q L_ASC EQU H_ASC EQU 0x29 0x06 0x07 MOVLW BCD_VAL ANDLW 0x0F IORLW 0x30 MOVWF L_ASC MOVLW BCD_VAL ANDLW 0xF0 SWAP WREG W WREG, IORLW 0x30 MOVWF H_ASC H ASC 28 ASCII to p packed BCD conversion MYBCD EQU Q MOVLW ANDLW MOVWF SWAP MOVLW ANDLW IORWF 0x20 A’4’ 0x0F MYBCD MYBCD, F A A’7’ 7 0x0F MYBCD, F 29 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 L Learning, i 2005. 2005 30
© Copyright 2026 Paperzz