16.317 Microprocessor Systems Design I Instructor: Dr. Michael Geiger Fall 2013 Lecture 29: PIC instruction set (continued) Lecture outline Announcements/reminders HW 6 due 11/25 Today’s lecture: More PIC instructions 4/13/2015 Microprocessors I: Lecture 29 2 Review: PIC instructions Logical operations: andlw/andwf iorlw/iorwf xorlw/xorwf Rotates: rrf/rlf Jumps/calls/return 4/13/2015 goto call return/retlw/retfie Microprocessors I: Lecture 29 3 Review: Conditional Execution Conditional execution in PIC: skip next instruction if condition true Two general forms btfsc btfss decfsz incfsz Test bit and skip if bit clear/set Increment/decrement register and skip if result is 0 f, b f, b f, F(W) f, F(W) Examples: btfsc TEMP1, 0 btfss STATUS, C decfsz TEMP1, F incfsz TEMP1, W 4/13/2015 STATUS bits: none ;Test bit b of register f, where b=0 to 7, skip if clear ;Test bit b of register f, where b=0 to 7, skip if set ;decrement f, putting result in F or W, skip if zero ;increment f, putting result in F or W, skip if zero ; Skip the next instruction if bit 0 of TEMP1 equals 0 ; Skip the next instruction if C==1 ; Decrement TEMP1, skip if TEMP1==0 ; W <- TEMP1+1 , skip if W==0 (TEMP1==0xFF) ; Leave TEMP1 unchanged Microprocessors I: Lecture 29 4 Example Show the values of all changed registers after each of the following sequences (a) What high-level operation does each perform? movf sublw btfsc goto incf goto a, W 0xA STATUS, Z L1 b, W L2 movf subwf btfss goto movf goto NUM2, W NUM1, W STATUS, C BL NUM1, W Done movf NUM2, W movwf MIN BL L1 decf b, W movwf a L2 4/13/2015 (b) Done Microprocessors I: Lecture 29 5 Example solution (part a) movf sublw btfsc a, W 0xA STATUS, Z goto L1 incf goto b, W L2 decf b, W W=a W = 10 – a Skip goto if result is non-zero Goto L1 if result == 0 Reach this point if result non-zero W=b+1 L1 W=b-1 High-level operation: if (a == 10) a=b–1 else a=b+1 L2 movwf a 4/13/2015 a = W value depends on what’s executed before this Microprocessors I: Lecture 29 6 Example solution (part b) W = NUM2 W = NUM1 – W = NUM1 – NUM2 Carry indicates “above” if set, NUM1 < NUM2 movf subwf NUM2, W NUM1, W btfss STATUS, C goto movf BL NUM1, W goto Done Skip “below” section movf NUM2, W if (NUM1 >= NUM2) W = NUM2 movwf MIN if (NUM1 < NUM2) W = NUM1 BL High-level operation: if (NUM1 < NUM2) MIN = NUM1 else MIN = NUM2 Done 4/13/2015 Microprocessors I: Lecture 29 7 Miscellaneous clrwdt sleep nop ; clear watchdog timer ; go into standby mode ; no operation STATUS bits: clrwwdt, sleep: NOT_TO, NOT_PD nop: none Examples: clrwdt sleep nop 4/13/2015 ; if watchdog timer is enabled, this instruction will reset ; it (before it resets the CPU) ; Stop clock; reduce power; wait for watchdog timer or ; external signal to begin program execution again ; Do nothing; wait one clock cycle Microprocessors I: Lecture 29 8 Working with multiple registers Can’t do simple data transfer or operation on two registers Usually must involve working register Examples: x86 PIC (assume PIC registers defined with same names as x86 registers) MOV AL, BL movf BL, W movwf AL ADD AL, BL movf BL, W addwf AL, F 4/13/2015 Microprocessors I: Lecture 29 9 Conditional jumps Basic ones are combination of bit tests, skips Remember that condition you’re testing is opposite of jump condition Examples: x86 PIC JNC label btfss goto JE label btfsc goto 4/13/2015 STATUS, C label STATUS, Z label Microprocessors I: Lecture 29 10 Conditional jumps (cont.) To evaluate other conditions, may want to use subtraction in place of compare CMP X, Y turns into: movf Y, W subwf X, W Possible results (unsigned comparison only): X > Y Z = 0, C = 1 X == Y Z = 1, C = 1 X < Y Z = 0, C = 0 More complex conditions 4/13/2015 X <= Y Z == C X != Y Z = 0 X >= Y C = 1 Microprocessors I: Lecture 29 11 Shift/rotate operations May need to account for each of the following Carry bit Bit being shifted/rotated out Always shifted in to register for rrf/rlf instructions Basic shift: explicitly set carry to 0 Arithmetic shift right: set carry to sign bit Basic rotate doesn’t rotate through carry Can either pre-test or fix later Multi-bit shift/rotate: loop where # iterations matches shift amount 4/13/2015 Microprocessors I: Lecture 29 12 Shift/rotate operations (cont.) Examples: x86 PIC SHL bcf rlf ROR AL, 1 STATUS, C AL, F AL, 1 bcf rrf btfsc STATUS, C AL, F STATUS, C bsf AL, 7 RCL rlf decfsz goto 4/13/2015 ; Clear carry bit ; Rotate AL one bit to right ; Skip next instruction if C clear ; C = bit shifted out of MSB ; Handle case where C = 1 ; MSB of AL should be 1 AL, 3 movlw movwf Loop: ; Clear carry bit ; Rotate AL one bit to the left 3 ; Initialize working register to 3 (# iterations) COUNT ; Initialize count register ; Assumes you’ve declared variable COUNT AL, F ; Rotate AL one bit to left COUNT, F ; Decrement counter & test for 0 ; Skip goto if result is zero Loop ; Return to start to loop Microprocessors I: Lecture 29 13 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 4/13/2015 AL, BL BL, AL label label AL, 1 AL, 5 Microprocessors I: Lecture 29 14 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) 4/13/2015 Microprocessors I: Lecture 29 15 Example solution (continued) JL label btfsc goto btfss goto End: STATUS, Z End STATUS, C label 4/13/2015 ; If Z == 0, check C ; Otherwise, no jump ; If C == 1, no jump ; Jump to label ; End of jump Microprocessors I: Lecture 29 16 Example solution (continued) SAR AL, 1 bcf STATUS, C btfsc AL, 7 bsf STATUS, C rrf 4/13/2015 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 17 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 4/13/2015 ;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 18 Final notes Next time: Continue with complex operations—multi-byte data Reminders: 4/13/2015 HW 6 to be posted; due date TBD Microprocessors I: Lecture 29 19