Professional Documents
Culture Documents
Microcontroller Architecture 2C
Microcontroller Architecture 2C
For writing assembly language program Microchip Technology has suggested the following guidelines.
Instruction Set:
The instruction set for PIC16C74A consists of only 35 instructions. Some of these instructions are byte oriented
instructions and some are bit oriented instructions.
The byte oriented instructions that require two parameters (For example, movf f, F(W)) expect the f to be
replaced by the name of a special purpose register (e.g., PORTA) or the name of a RAM variable (e.g., NUM1),
which serves as the source of the operand. 'f' stands for file register. The F(W) parameter is the destination of the
result of the operation. It should be replaced by:
F, if the destination is to be the source register. W, if the destination is to be the working register (i.e.,
Accumulator or W register).
The bit oriented instructions also expect parameters (e.g., btfsc f, b). Here 'f' is to be replaced by the name of a
special purpose register or the name of a RAM variable. The 'b' parameter is to be replaced by a bit number
ranging from 0 to 7.
For example:
Z equ 2
btfsc STATUS, Z
Z has been equated to 2. Here, the instruction will test the Z bit of the STATUS register and will skip the next
instruction if Z bit is clear.
The literal instructions require an operand having a known value (e.g., 0AH) or a label that represents a known
value.
For example:
NUM equ 0AH ; Assigns 0AH to the label NUM ( a constant )
movlw NUM ; will move 0AH to the W register.
INSTRUCTION SET:
STATUAS
Mnemonics Description Cycles
bits affected
bcf f, b Clear bit b of register f 1
bsf f, b Set bit b of register f 1
clrw Clear W 1 Z
clrf f Clear f 1 Z
addlw k Add the literal value to W and store the result in W 1 C,DC,Z
sublw k Subtract the literal value from W and store the result in W 1 C,DC,Z
rlf f, F(W) Copy f into F or W; rotate F or W left through the carry bit 1 C
rrf f, F(W) Copy f into F or W; rotate F or W right through the carry bit 1 C
btfsc f, b Test 'b' bit of the register f and skip the next instruction if bit is clear 1 (2)
btfss f, b Test 'b' bit of the register f and skip the next instruction if bit is set 1 (2)
Decrement f and copy the result to F or W; skip the next instruction if
decfsz f, F(W) 1 (2)
the result is zero
Increment f and copy the result to F or W; skip the next instruction if
incfcz f, F(W) 1 (2)
the result is zero
goto label Go to the instruction with the label "label" 2
call label Go to the subroutine "label", push the Program Counter in the stack 2
retrun Return from the subroutine, POP the Program Counter from the stack 2
Return from the subroutine, POP the Program Counter from the stack;
retlw k 2
put k in W
retie Return from Interrupt Service Routine and re-enable interrupt 2
clrwdt Clear Watch Dog Timer 1 NOT_TO, NOT_PD
nop No operation 1
Example-1:
bcf f, b Clear 'b' bit of register 'f'
Operands: 0 ≤ f ≤ 127
0≤b≤7
Encoding:
0 1 0 0 b b b f f f f f f f
Op-Code Bit Address Register Address
The instruction is executed in one instruction cycle, i.e., 4 clock cycles. The activities in various clock cycles are
as follows.
Q1 Q2 Q3 Q4
Example-2:
Encoding:
1 0 1 k k k k k k k k k k k
Op-Code 11-bit-address
Since this instruction requires modification of program Counter, it takes two instruction cycles for execution.
Q1 Q2 Q3 Q4
1ST Instruction Read literal Process
Cycle Decode Write to PC
‘k’ Data
Clear/ Move
clrw ; Clear the Working register, W
clrf TEMP1 ; Clear Temporary variable TEMP 1
movlw 5 ; Load 5 into W
movlw 10 ; Load D’10’ or H’10’ or B’10’ into W
; depending upon the default representation
movwf TEMP1 ; move W into TEMP1
movwf TEMP1, F ; Incorrect syntax
movf TEMP1,W ; move TEMP1 into W
swapf TEMP1, F ; Swap 4-bit nibbles of TEMP1
swapf TEMP1, W ; Move TEMP1 to W, swapping nibbles
; and leave TEMP1 unchanged
Multiple-bit Manipulation
andlw B’00000111’ ; Force uppar 5 bits of W to zero
andwf TEMP1, F ; TEMP1 < ̶ TEMP1 AND W
andwf TEMP1, W ; W< ̶ TEMP1 AND W
iorlw B’00000111’ ; Force lower 3 bits of W to one
iorwf TEMP1, F ; TEMP1 < ̶ TEMP1 OR W
xorlw B’00000111’ ; Complement lower 3 bits of W
xorwf TEMP1, W ; W< ̶ TEMP1 XOR W
Addition/ Subtraction
addlw 5 ; Add 5 to W
addwf TEMP1, F ; TEMP1< ̶ TEMP1+W
sublw 5 ; W< ̶ 5-W (not W < ̶ W-5)
subwf TEMP1, F ; TEMP1 < ̶ TEMP1 – W
Rotate
rlf TEMP1, F ;Nine-bit left rotate through C
; (C < ̶ TEMP1, 7; TEMP1, i+1 < ̶ TEMP1, i
; TEMP1, 0 < ̶ C)
rrf TEMP1, W ; Leave TEMP1 unchanged
; Copy to W and rotate W right through C
Conditional branch
btfsc TEMP1, 0 ; Skip the next instruction if bit 0 of
; TEMP1 equals zero
btfss STATUS, C ; Skip if C=1
decfsz TEMP1, F ; Decrement TEMP1; skip if zero
incfsz TEMP1, W ; Leave TEMP1 unchanged; skip if
; TEMP1 = H’FF’; W < ̶ TEMP1 + 1
Miscellaneous
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
; signal to begin program execution again
Nop ; Do nothing; wait one clock cycle
Simple Operation
Either-Or sequence
Assume that an instruction that affects the Z bit has just been executed. Then depending on the result, one
instruction sequence or another is to be executed, continuing on after either case.
Note how the movf instruction is first used to test COUNTL for zero without changing it, and even without
having to move it into W.
Note that the same scheme will work for a variable of any size.
These examples illustrate some of the tediousness of working with this “bare-bones” instruction set. However, it
is important to notice three things:
1. Because of pipelining and because the chip can execute five cycles every microsecond (when used with a 20-
MHz crystal), these sequences are executed quickly. For example, the 16-bit decrement sequence requires four
cycles and therefore takes only 0.8 micro-second to execute (whether or not the branch is taken).
2. Because the instruction set can operate directly on RAM variables, many operations avoid the “overhead”
associated with other microcontrollers wherein operands in memory must be first loaded into an accumulator,
then operated on, and then restored to memory.
3. Microchip Technology’s (free) assembler is a macro assembler. Consequently, sequences such as these can
be prepared once and for all as macros. Therefore any such code sequence looks like a newly defined
instruction. Furthermore, the assembler accepts include files. This means any number of macro definitions can
be put into one or more files and then have the assembler access these files as it assembles the file containing
our application code. Any macros that are used will generate object code, the code that is loaded into PIC for
execution. Unused macros generate no object code. Even macro files of complex operations can be included,
such as signed and unsigned multiply and divide routines for numbers of various lengths up to 32 bits long,
available over the internet from Microchip Technology. In fact, Microchip supports user of its various
microcontrollers with free books, application examples, assembler and simulator software, as well as macro files
over the internet.