EEE305 Microcontrollers
Week 12: A look at Assembler
Ian McCrum
What I expect you to be able to do
There are 35 simple instructions that a PIC can execute; you need not
all of these, but you should be able to look up what they are.
You should be able to work out what instruction causes a certain
action to occur, e.g how to SET a bit, or how to MOVE a number from
one place to another (actually most “MOVES” are actually “COPIES”)
You should be able to convert a 14 bit number into a assembler
instruction, and convert an assembler instrucition into a 14 bit code.
You should be able to work out what binary pattern to put in a
peripheral control register to configure it (e.g the timer or serial port)
A PIC spends its time reading
instructions from the program
memory, one
after another, and doing
whatever these instructions say.
Each instruction
consists of 14 bits. If you could
see the bits as binary ones and
zeroes, a
program might look like this:
The earliest computers were programmed by
technicians writing binary codes just like this.
As you can see, though, binary codes are very
hard for human beings to read or write
because they’re completely arbitrary; they
look like gibberish.
Another reason binary codes are hard to write
is that many of them refer to locations in
For instance, a “go to” instruction will have
to say what memory address to jump to.
Programming would be much
easier if you could label a location in the
program and have the computer
figure out its address.
Taken from [accessed 1/5/13]
For both of these reasons, assembly language was invented over forty
years ago. Or, to be more precise, many assembly languages have been invented,
one for each type of CPU.What assembly languages have in common
is that the instructions are abbreviated by readable codes (mnemonics)
such as GOTO and locations can be represented by programmer-assigned
For example, in assembly language, the binary
instructions just mentioned would be:
In English:
Put the bit pattern 00000000 into the W register and copy it to
the tri-state control register for port B, thereby setting up port B for output;
then put 00000001 into W and copy it to port B itself; and finally stop the
program by going into an endless loop. This will light an LED wired up to RB0
; assembly language program TURNON.ASM
; Turns on an LED connected to B0.
; Uses RC oscillator, about 100 kHz.
; CPU configuration
__config _RC_OSC & _WDT_OFF & _PWRTE_ON
; Program
org 0
movlw B’00000000’
tris PORTB
movlw B’00000001’
movwf PORTB
; start at address 0
; At startup, all ports are inputs.
; Set Port B to all outputs.
; w := binary 00000000
; copy w to port B control reg
; Put a 1 in the lowest bit of port B.
; w := binary 00000001
; copy w to port B itself
; Stop by going into an endless loop
goto fin
; program ends here
The architecture of the PIC
Program Memory Organization
Data Memory Organization
The data memory is partitioned into multiple banks
which contain the General Purpose Registers and the
Special Function Registers. Bits RP1 (STATUS<6>)
and RP0 (STATUS<5>) are the bank select bits.
Each bank extends up to 7Fh (128 bytes). The lower
locations of each bank are reserved for the Special
Function Registers. Above the Special Function Registers
are General Purpose Registers, implemented as
static RAM. All implemented banks contain Special
Function Registers. Some frequently used Special
Function Registers from one bank may be mirrored in
another bank for code reduction and quicker access.
If the STATUS register is the
destination for an instruction
that affects the Z, DC or C bits,
then the write to these three
bits is disabled.
These bits are set or cleared
according to the device logic.
Furthermore, the TO and PD
bits are not writable, therefore,
the result of an instruction with
the STATUS register as
destination may be different
than intended
Use only BCF, BSF,
SWAPF and MOVWF instructions to
alter the STATUS register.
Each PIC16F87X instruction is a 14-bit word, divided into an OPCODE which specifies the
instruction type and one or more operands which further specify the operation of the
The PIC16F87X instruction set summary in Table 13-2 lists byte-oriented, bit-oriented,
and literal and control operations. Table 13-1 shows the opcode field descriptions.
For byte-oriented instructions, 'f' represents a file register designator and 'd' represents a
destination designator. The file register designator specifies which file register is to be used
by the instruction.
The destination designator specifies where the result of the operation is to be placed. If 'd' is
zero, the result is placed in the W register. If 'd' is one, the result is placed in the file register
specified in the instruction.
For bit-oriented instructions, 'b' represents a bit field designator which selects the number
of the bit affected by the operation, while 'f' represents the address of the file in which the
bit is located.
For literal and control operations, 'k' represents an eight or eleven bit constant or literal
All instructions are executed within one single instruction cycle, unless a conditional test
is true or the program counter is changed as a result of an instruction. In this case, the
execution takes two instruction cycles with the second cycle executed as a NOP.
One instruction cycle consists of four oscillator periods. Thus, for an oscillator frequency
of 4 MHz, the normal instruction execution time is 1 us. If a conditional test is true, or the
program counter is changed as a result of an instruction, the instruction execution time is
2 us.
There exists a couple of instructions called OPTION and TRIS. Microchip are removing
these from their more modern chips, hence the warning in the datasheet;
The key instructions are to move a number into a register and move the contents of
a register to another register. The chip designer had to make compromises, so often
you need to use several instructions to achieve what you want. The W register is an
important register, it is usually involved in data transfers.
Examples from instruction set
Examples from instruction set
And a BTFSC version is available as well, this is
how “IF” statements are written in assembler
Example of “Hand” Assembly
; Given the program snippet below
movlw B’00000001’ ; note that PORTB is at FSR address 0x06 (OHP 09)
movwf PORTB
Relevant parts of the list of instructions
Hence the first instruction becomes 11 0000 0000 0001 (assuming we use 00 for xx )
The second instruction becomes
00 0000 10000110 (since PORTB is 0x06 )
Check OHP 16 to glean understanding of what each instruction does.
As a Tut, try yourself to write down the assembler that does the
Move the contents of file register 0x08 into W
Copy the contents of W into register 6
RESET bit 3 of PORTB
SET bit 0 of PORTB
In one instruction, RESET bits 4 and 5 of W
In one instruction SET bits 0 and 1 of W
Hints for Q5 and Q6, use the boolean AND or OR operations.
Q7-Q12 convert each of the assembler instructions above to 14
bit binary machine code.
Related flashcards
Create flashcards