Arithmetic and Logic Instructions

Arithmetic and Logic Instructions
Hsiao-Lung Chan
Dept Electrical Engineering
Chang Gung University, Taiwan
[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_2
N_3
N_4
EQU
EQU
MOVLW
MOVWF
MOVLW
ADDLW
BNC
INCF
ADDLW
…
ADDLW
BNC
INCF
MOVWF
0x6
0x7
0x0
H_BYTE
0x40
0x41
N_2
H_byte, F
0x42, W
0x43,W
N_4
H_byte, F
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
0x3B
H_BYTE
MOVLW
ADDWF
MOVLW
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
MOVLW
ADDLW
DAW
0x47
; WREG = 47H
0x25
; WREG = 6CH, DC = 0, C = 0
; Adjust for BCD addition by adding 6 (WREG=72H) C =0
MOVLW
ADDLW
DAW
0x09
; WREG = 09H
0x08
; WREG = 11H, DC = 1, C = 0
; Adjust for BCD addition by adding 6 (WREG=17H) C =0
MOVLW
ADDLW
DAW
0x52
; WREG = 52H
0x87
; WREG = D9H, DC = 0, C =0
; Adjust for BCD addition by adding 60H (WREG=39H) C =1
MOVLW
ADDLW
DAW
0x57
; WREG = 57H
0x77
; WREG = CEH, DC = 0, C =0
; 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
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 SUBWF
MYREG
NEXT
EQU
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, 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
0x27
H_BYTE
MOVLW
SUBWF
0x96
L_BYTE, F
MOVLW
SUBWFC
0x12
H_BYTE, F
; F = F – W = 62 – 96 = CCH
; C = borrow = 0, N = 1
; F = F – W – borrow
; = 27 – 12 – 1 = 14H
; 2762H - 1296H = 14CCH
8
Multiplication of unsigned 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’
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
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


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’
MOVLW
ADDLW
-D’128’
-D’2’
; 1000 0000 + 1111 1110 = 0111 1110 (7EH)
; N = 0, OV = 1, sum = 126
; condition 2
; 0110 0000 + 0100 0110 = 1010 0110 (A6H)
; N = 1, OV = 1, sum = -90
; condition 1
12
Overflow in signed number operation (cont.)
MOVLW
ADDLW
-D’2’
-D’5’
MOVLW
ADDLW
+D’7’
+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
13
Logic operations



ANDLW K
IORLW K
XORLW K
; Inclusive OR
; Exclusive OR
14
RB2 and RB5 of PORTB are used to control an
outdoor light and indoor light 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’
PORTB, F
B’11011111’
PORTB, F
15
Test PortB to see whether it has value 45H
CLRF
CLRF
SETF
MOVLW
XORWF
BNZ
MOVLW
MOVWF
EXIT
…
TRISC
PORTC
TRISB
0x45
PORTB, W
EXIT
0x99
PORTC
16
Compare instructions
17
Monitor PORTD for the value 63H
SETF
MOVLW
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
GREG
LEQ
OVER
EQU
EQU
SETF
MOVLW
CPFSGT
BRA
MOVFF
BRA
CPFSLT
BRA
MOVFF
…
0x20
0x21
TRISD
D’75’
PORTD
LEQ
PORTD, GREG
OVER
PORTD
OVER
PORTD, LREG
19
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
20
Do multiplication/division of unsigned numbers by
rotating left/right through carry
MYREG EQU
0x20
MOVLW 0x25
MOVWF MYREG
MYREG EQU
0x20
MOVLW 0xD5
MOVWF MYREG
BCF
RLCF
STATUS, C
MYREG, F
BCF
RRCF
STATUS, C
MYREG, F
BCF
RLCF
STATUS, C
MYREG, F
BCF
RRCF
STATUS, C
MYREG, F
21
Find number of 1s in a given byte
R1
EQU
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
AGAIN
22
SWAPF fileReg, d
MYREG EQU
MOVLW
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