190 likes | 298 Views
In this lecture, Dr. Michael Geiger discusses advanced aspects of the PIC instruction set, focusing on logical operations (AND, OR, XOR), rotation instructions, and conditional execution in microprocessor programming. Key topics include manipulating registers, executing jump and call commands, and performing efficient data transfers. Examples illustrate high-level operations through assembly code, with a thorough explanation of how conditions affect program flow, including the use of shift and rotate operations. This is essential for understanding low-level programming in PIC microcontrollers.
E N D
16.317Microprocessor 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 Microprocessors I: Lecture 29
Review: PIC instructions • Logical operations: • andlw/andwf • iorlw/iorwf • xorlw/xorwf • Rotates: rrf/rlf • Jumps/calls/return • goto • call • return/retlw/retfie Microprocessors I: Lecture 29
Review: Conditional Execution • Conditional execution in PIC: skip next instruction if condition true • Two general forms • Test bit and skip if bit clear/set • Increment/decrement register and skip if result is 0 STATUS bits: none Examples: • btfsc TEMP1, 0 ; Skip the next instruction if bit 0 of TEMP1 equals 0 • btfss STATUS, C ; Skip the next instruction if C==1 • decfsz TEMP1, F ; Decrement TEMP1, skip if TEMP1==0 • incfsz TEMP1, W ; W <- TEMP1+1 , skip if W==0 (TEMP1==0xFF) ; Leave TEMP1 unchanged btfsc f, b ;Test bit b of register f, where b=0 to 7, skip if clear btfss f, b ;Test bit b of register f, where b=0 to 7, skip if set decfsz f, F(W) ;decrement f, putting result in F or W, skip if zero incfsz f, F(W) ;increment f, putting result in F or W, skip if zero Microprocessors I: Lecture 29
Example • Show the values of all changed registers after each of the following sequences • What high-level operation does each perform? (a) movf a, W sublw 0xA btfsc STATUS, Z goto L1 incf b, W goto L2 L1 decf b, W L2 movwf a (b)movf NUM2, W subwf NUM1, W btfss STATUS, C gotoBL movf NUM1, W goto Done BL movf NUM2, W Done movwf MIN Microprocessors I: Lecture 29
Example solution (part a) movf a, W W = a sublw0xA W = 10 – a btfsc STATUS, Z Skip goto if result is non-zero gotoL1 Goto L1 if result == 0 Reach this point if result non-zero incf b, W W = b + 1 gotoL2 L1 decf b, W W = b - 1 L2 movwfa a = W value depends on what’s executed before this High-level operation: if (a == 10) a = b – 1 else a = b + 1 Microprocessors I: Lecture 29
Example solution (part b) movf NUM2, W W = NUM2 subwf NUM1, W W = NUM1 – W = NUM1 – NUM2 btfss STATUS, C Carry indicates “above” if set, NUM1 < NUM2 goto BL movf NUM1, W if (NUM1 < NUM2) W = NUM1 gotoDone Skip “below” section BL movf NUM2, W if (NUM1 >= NUM2) W = NUM2 Done movwfMIN High-level operation: if (NUM1 < NUM2) MIN = NUM1 else MIN = NUM2 Microprocessors I: Lecture 29
Miscellaneous clrwdt ; clear watchdog timer sleep ; go into standby mode nop ; no operation Examples: • clrwdt ; if watchdog timer is enabled, this instruction will reset ; it (before it resets the CPU) • sleep ; Stop clock; reduce power; wait for watchdog timer or ; external signal to begin program execution again • nop ; Do nothing; wait one clock cycle STATUS bits: clrwwdt, sleep: NOT_TO, NOT_PD nop: none Microprocessors I: Lecture 29
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 Microprocessors I: Lecture 29
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 STATUS, C goto label • JE label btfsc STATUS, Z goto label Microprocessors I: Lecture 29
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 • X <= Y Z == C • X != Y Z = 0 • X >= Y C = 1 Microprocessors I: Lecture 29
Shift/rotate operations • May need to account for each of the following • Carry bit • Always shifted in to register for rrf/rlf instructions • Basic shift: explicitly set carry to 0 • Arithmetic shift right: set carry to sign bit • Bit being shifted/rotated out • Basic rotate doesn’t rotate through carry • Can either pre-test or fix later • Multi-bit shift/rotate: loop where # iterations matches shift amount Microprocessors I: Lecture 29
Shift/rotate operations (cont.) • Examples: x86 PIC • SHL AL, 1 bcf STATUS, C ; Clear carry bit rlf AL, F ; Rotate AL one bit to the left • ROR AL, 1 bcf STATUS, C ; Clear carry bit rrf AL, F ; Rotate AL one bit to right btfsc STATUS, C ; Skip next instruction if C clear ; C = bit shifted out of MSB bsf AL, 7 ; Handle case where C = 1 ; MSB of AL should be 1 • RCL AL, 3 movlw 3 ; Initialize working register to 3 (# iterations) movwf COUNT ; Initialize count register ; Assumes you’ve declared variable COUNT Loop: rlf AL, F ; Rotate AL one bit to left decfsz COUNT, F ; Decrement counter & test for 0 ; Skip goto if result is zero goto Loop ; Return to start to loop Microprocessors I: Lecture 29
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 AL, BL • SUB BL, AL • JNZ label • JL label • SAR AL, 1 • ROL AL, 5 Microprocessors I: Lecture 29
Example solution • OR AL, BL movf BL, W ; W = BL iorwf AL, F ; AL = AL OR W = AL OR BL • SUB BL, AL movf AL, W ; W = AL subwf BL, F ; BL = BL – W = BL – AL • JNZ label btfss STATUS, Z ; Skip goto if Z == 1 (if goto label ; previous result == 0) Microprocessors I: Lecture 29
Example solution (continued) • JL label btfsc STATUS, Z ; If Z == 0, check C goto End ; Otherwise, no jump btfss STATUS, C ; If C == 1, no jump goto label ; Jump to label End: ; End of jump Microprocessors I: Lecture 29
Example solution (continued) • SAR AL, 1 bcf STATUS, C ; C = 0 btfsc AL, 7 ; Skip if MSB == 0 bsf STATUS, C ; C = 1 if MSB == 1 ; C will hold copy of ; MSB (keeping sign ; intact) rrf AL, F ; Rotate right by 1 Microprocessors I: Lecture 29
Example solution (continued) • ROL AL, 5 movlw 5 ; W = 5 movwf COUNT ; COUNT = W = 5 L:bcf STATUS, C ; C = 0 btfsc AL, 7 ; Skip if MSB == 0 bsf STATUS, C ; C = 1 if MSB == 1 ; C will hold copy of ; MSB (bit rotated into ; LSB) rlf AL, F ; Rotate left by 1 decfsz COUNT ; If COUNT == 0, don’t ; restart loop goto L Microprocessors I: Lecture 29
Final notes • Next time: • Continue with complex operations—multi-byte data • Reminders: • HW 6 to be posted; due date TBD Microprocessors I: Lecture 29