PIC assembly programming

16.317
Microprocessor Systems Design I
Instructor: Dr. Michael Geiger
Fall 2013
Lecture 30:
PIC programming
Lecture outline

Announcements/reminders



Review: complex operations




HW 6 due 11/25
No lecture 11/27
Working with multiple registers
Conditional jumps
Shift/rotate
Today’s lecture: multi-byte data
1/11/2017
Microprocessors I: Lecture 29
2
Review: complex operations

Multiple registers


Data must be transferred through working register
Conditional jumps



Usually btfsc/btfss instruction + goto
Equality/inequality—use subtract in place of CMP
If you subtract X – Y:







X > Y  Z = 0, C = 1
X == Y  Z = 1, C = 1
X < Y  Z = 0, C = 0
X <= Y  Z == C
X != Y  Z = 0
X >= Y  C = 1
Shift/rotate


1/11/2017
Manipulate carry before operation
Use loop for multi-bit shift/rotate
Microprocessors I: Lecture 10
3
Examples








Translate these x86 operations to PIC code
Assume that there are registers defined for
each x86 register (e.g. AL, AH, BL, BH, etc.)
OR
SUB
JNZ
JL
SAR
ROL
1/11/2017
AL, BL
BL, AL
label
label
AL, 1
AL, 5
Microprocessors I: Lecture 29
4
Example solution

OR
AL, BL
movf BL, W
iorwf AL, F

SUB
BL, AL
movf AL, W
subwf BL, F

JNZ
; W = BL
; AL = AL OR W = AL OR BL
; W = AL
; BL = BL – W = BL – AL
label
btfss STATUS, Z ; Skip goto if Z == 1 (if
goto label
; previous result == 0)
1/11/2017
Microprocessors I: Lecture 29
5
Example solution (continued)

JL
label
btfsc
goto
btfss
goto
End:
STATUS, Z
End
STATUS, C
label
1/11/2017
; If Z == 0, check C
; Otherwise, no jump
; If C == 1, no jump
; Jump to label
; End of jump
Microprocessors I: Lecture 29
6
Example solution (continued)

SAR
AL, 1
bcf STATUS, C
btfsc AL, 7
bsf STATUS, C
rrf
1/11/2017
AL, F
;C=0
; Skip if MSB == 0
; C = 1 if MSB == 1
; C will hold copy of
; MSB (keeping sign
; intact)
; Rotate right by 1
Microprocessors I: Lecture 29
7
Example solution (continued)

ROL
L:
AL, 5
movlw 5
movwf COUNT
bcf
STATUS, C
btfsc AL, 7
bsf
STATUS, C
rlf
AL, F
decfsz COUNT
goto
1/11/2017
;W=5
; COUNT = W = 5
;C=0
; Skip if MSB == 0
; C = 1 if MSB == 1
; C will hold copy of
; MSB (bit rotated into
; LSB)
; Rotate left by 1
; If COUNT == 0, don’t
; restart loop
L
Microprocessors I: Lecture 29
8
Multi-byte data


Logical operations can be done byte-by-byte
Arithmetic and shift/rotate operations require
you to account for data flow between bytes



Carry/borrow in arithmetic
Bit shifted between bytes in shift/rotate
Order of these operations is important


1/11/2017
Arithmetic: must do least significant bytes first
Shift/rotate: move through bytes in same order as
shift  bits being shifted will move through carry
Microprocessors I: Lecture 9
9
Working with 16-bit data
Assume a 16-bit counter, the upper byte of the counter is called COUNTH and the lower
byte is called COUNTL.
Decrement a 16-bit counter
movf
COUNTL, F
btfsc
STATUS, Z
decf
COUNTH, F
decf
COUNTL, F
Test a 16-bit variable for zero
movf
COUNTL, F
btfsc
STATUS, Z
movf
COUNTH, F
btfsc
STATUS, Z
goto
BothZero
CarryOn
1/11/2017
; Set Z if lower byte == 0
; if so, decrement COUNTH
; in either case decrement COUNTL
; Set Z if lower byte == 0
; If not, then done testing
; Set Z if upper byte == 0
; if not, then done
; branch if 16-bit variable == 0
Microprocessors I: Lecture 9
10
Examples


Translate these x86 operations to PIC code
Assume that there are registers defined for each
x86 register (e.g. AL, AH, BL, BH, etc.)






16-bit values (e.g., AX) must be dealt with as
individual bytes
MOVZX
MOVSX
INC
SUB
RCL
1/11/2017
AX, BL
AX, BL
AX
BX, AX
AX, 5
Microprocessors I: Lecture 9
11
Example solutions


MOVZX AX, BL
movf
BL, W
movwf
AL
clrf
AH
MOVSX AX, BL
movf
BL, W
movwf
AL
clrf
AH
btfsc
AL, 7
decf
AH, F
1/11/2017
; Copy BL to W
; Copy W to AL
; Clear upper byte
; Copy BL to W
; Copy W to AL
; Clear upper byte
; Test sign bit
; If sign bit = 1, set
; AH = 00 - 1 = 0xFF
Microprocessors I: Lecture 9
12
Example solutions


INC AX
incf
btfsc
incf
AL, F
STATUS, Z
AH, F
SUB
movf
subwf
btfss
decf
BX, AX
AL, W
BL, F
STATUS, C
BH, F
movf AH, W
subwf BH, F
1/11/2017
; Increment low byte
; Check zero bit
; If Z == 1, increment
; high byte
; Copy AL to W
; BL = BL – AL
; Check carry
; If C == 0 (borrow is 1),
; decrement BH before sub.
; Copy AH to W
; BH = BH - AH
Microprocessors I: Lecture 9
13
Example solutions

RCL
AX, 5
movlw 5
movwf COUNT
L:
rlf
AL, F
rlf
AH, F
decfsz COUNT, F
goto L
1/11/2017
;W=5
; COUNT = W = 5
; Assumes register
; COUNT is defined
; Rotate low byte
; Bit transferred from
; low to high byte is
; now in carry
; Rotate high byte
; Decrement & test COUNT
; Return to start of loop if
; COUNT != 0
Microprocessors I: Lecture 9
14
Final notes

Next time:


Continue with complex operations—multi-byte
data
Reminders:

1/11/2017
HW 6 to be posted; due date TBD
Microprocessors I: Lecture 29
15