Chapter 2: PIC ARCHITECTURE AND ASSEMBLY LANGUAGE PROGRAMMING Embedded System Design, Bahria University Islamabad “PIC Microcontroller and Embedded Systems”, Muhammad Ali Mazidi, Rolin D. Mckinlay, Danny Causey Prentice Hall, 2008. Embedded Systems, Bahria University Islamabad 1 • To program in assembly language, we must understand the registers and architecture of a given CPU. Introduction Embedded Systems, Bahria University Islamabad 2 • Performs logical and arithmetic functions • It has following resisters 1. The WREG Register in the PIC WREG – stands for working register (acts as an accumulator) – 8 bit register used to perform arithmetic or logical functions Status register that stores flags – indicates the status of the operation done by ALU Instruction decoder (ID)– when the instruction is fetched, it goes into the ID to be interpreted – tell the processor what to do Embedded Systems, Bahria University Islamabad 3 1. The WREG Register in the PIC To understand use of WREG register, two instructions are used • MOVLW instruction MOVLW K ; move literal value K into WREG K is an 8-bit value L stands for literal which means, literally, a number must be used Examples • MOVLW 25H • MOVLW 87H Embedded Systems, Bahria University Islamabad 4 1. The WREG Register in the PIC • ADDLW instruction ADDLW K ; add literal value K to WREG It add literal value k to register WREG and put the result back in the WREG register MOVLW 25H ADDLW 34H Executing the above lines results in WREG=59H (25H+34H=59H) Embedded Systems, Bahria University Islamabad 5 1. The WREG Register in the PIC • Form above example first instruction move 25H(8-bit literal) in WREG (via ALU). Second instruction add 34H with content of WREG (25H) and store result (59H) in WREG MOVLW 25H ADDLW 34H Embedded Systems, Bahria University Islamabad 6 1. The WREG Register in the PIC Embedded Systems, Bahria University Islamabad 7 SECTION 2.2 THE PIC File Register Embedded Systems, Bahria University Islamabad 8 PIC file register • PIC has many other registers in addition to the WREG • They are called data memory space to distinguish them form program(code)memory • Data memory space is a read/write (static RAM) • It is also called file register • It is used by CPU for data storage, scratch pad and registers for internal use and functions • In PIC it ranges from 32 bytes to several thousand bytes depending on the chip Embedded Systems, Bahria University Islamabad 9 PIC file register • It has maximum of 4096 (4K) bytes (each location of 1 byte data storage) • With 4096 bytes, it has addresses of 000-FFF (12-bit address bus ) • The file register (data RAM) is divided into two section a) General-Purpose Registers(GPR) or GeneralPurpose RAM (GP RAM) b) Special function Register (SFR) • Different models have different number of SFRs and location is GPR Embedded Systems, Bahria University Islamabad 10 PIC File register Embedded Systems, Bahria University Islamabad 11 PIC File register & Access Bank • Special Function Register (SFRs) of PIC18 we will study it in late chapters PIC File register (SFR) Embedded Systems, Bahria University Islamabad 13 SECTION 2.3 Assembly Language Instructions Embedded Systems, Bahria University Islamabad 14 • MOVWF instruction MOVWF FileReg Assembly Language instructions (MOVWF) We saw MOVLW instruction previous that move literal (8-bits) to WREG In MOVWF value of WREG is move to ( F ) means file register The location in the file register can be one of SFRs or a location in the general-purpose register region After execution of instruction location in the file register will have the same value as WREG Embedded Systems, Bahria University Islamabad 15 • Location with SFR Assembly Language instructions (MOVWF) • Location with General Purpose register Embedded Systems, Bahria University Islamabad 16 Assembly Language instructions (MOVWF) Embedded Systems, Bahria University Islamabad 17 Assembly Language instructions (ADDWF) • ADDWF instruction It add the content of WREG and file register. File register can be one of SFRs or a Generalpurpose register. The destination can be WREG or the File register Format for this instruction is fileReg is the file register location D indicate destination bit (D=0 for WREG, D=1 for file register) Embedded Systems, Bahria University Islamabad 18 Assembly Language instructions (ADDWF) Embedded Systems, Bahria University Islamabad 19 Assembly Language instructions (ADDWF) Embedded Systems, Bahria University Islamabad 20 • We can also write w or f instead of 0 or 1 Assembly Language instructions (ADDWF) • Rewrite of the last program Embedded Systems, Bahria University Islamabad 21 Assembly Language instructions (ADDWF) Embedded Systems, Bahria University Islamabad 22 Assembly Language instructions (ADDWF) Embedded Systems, Bahria University Islamabad 23 v Assembly Language instructions Embedded Systems, Bahria University Islamabad 24 • COMF instruction Assembly Language instructions (COMF) This instruction complement (inverts) the content of fileReg and place result in WREG or fileReg. Embedded Systems, Bahria University Islamabad 25 Assembly Language instructions (COMF) Embedded Systems, Bahria University Islamabad 26 • DECF instruction Assembly Language instructions (DECF) This instruction Decrements (subtracts one from) the contents of fileReg and place the result in WREG or fileReg Embedded Systems, Bahria University Islamabad 27 • MOVF instruction Assembly Language instructions (MOVF) It intended to perform MOVFW If D=0, it copies content of fileReg to WREG If D=1, the content of fileReg is copied to itself It is used to bring data into WREG from I/O ports Sometimes it is used to copy fileReg to itself for the purpose of testing fileReg contents. Embedded Systems, Bahria University Islamabad 28 Assembly Language instructions (MOVF) Embedded Systems, Bahria University Islamabad 29 Assembly Language instructions (MOVF) Embedded Systems, Bahria University Islamabad 30 • MOVFF instruction Assembly Language instructions (MOVFF) It copies data from one location of fileReg to another location in fileReg. fileReg location for source and destination can be any of 4096 location of RAM MOVFF allows us to move data within RAM without going through the WREG register Embedded Systems, Bahria University Islamabad 31 Assembly Language instructions (MOVFF) Embedded Systems, Bahria University Islamabad 32 Assembly Language instructions (MOVFF) Embedded Systems, Bahria University Islamabad 33 Assembly Language instructions • So far, we have done following instructions MOVLW value ADDLW value MOVWF fileReg ADDWF fileReg, D COMF fileReg, D DECF fileReg, D MOVF fileReg, D MOVFF fileReg, fileReg Embedded Systems, Bahria University Islamabad 34 Assembly Language instructions Embedded Systems, Bahria University Islamabad 35 Assembly Language instructions Embedded Systems, Bahria University Islamabad 36