Sei sulla pagina 1di 6
LeLeccttuurree 33 ••SSmmaallll asseassemmbbllyy pprrogogrramam ••IInsnsttrrucucttiionon codecode ••TThehe
LeLeccttuurree 33
••SSmmaallll asseassemmbbllyy pprrogogrramam
••IInsnsttrrucucttiionon codecode
••TThehe dadattaa mmeemmoorryy ooff PPIICC1616FF877A877A
••DDaattaa mmeemmoorryy bbaanksnks PPoorrttss anandd
dadattaa ddiirrececttiionon ooff poporrttss
••AA cocommpplleettee ssmmaallll pprrogogrramam
; name: xplus5.asm date: 2006-10-10 ;author: ; ; register definitions LIST P=16F877A INCLUDE "P16F877a.inc“
; name: xplus5.asm
date: 2006-10-10
;author:
;
;
register definitions
LIST
P=16F877A
INCLUDE "P16F877a.inc“
CONFIG
_CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON
x1
equ
0x20
y1
equ
0x24
;
program
org
0x0000
main
NOP
MOVF
x1,w
ADDLW 0x5
MOVWF y1
SLEEP
end
AA ssmmallall aasssseemmblblyy pprrogogrramam • An 8 bit variable x1 is located in data memory
AA ssmmallall aasssseemmblblyy pprrogogrramam
• An 8 bit variable x1 is located in data
memory location 0x020.
• Write an assembly program which adds x1
with 5 and puts the result in a variable y1.
• y1 is located in data memory location
0x024.
• The program starting address must be
0x0000.
• Everything after a semicolon on a line is interpreted as a comment and will
• Everything after a semicolon on a line is
interpreted as a comment and will be ignored by
the assembler.
• Always start a program with comments with a
program name, a date and an author.
• If something is written starting from the first
position of a line it will be interpreted as a label.
• A label is a name given to a memory address in
the microcontroller.
• x1 and y1 are memory addresses or registers in
the data memory.
• equ tells the assembler that it must exchange x1and y1 with 0x20 and 0x24
• equ tells the assembler that it must exchange
x1and y1 with 0x20 and 0x24 in all the following
lines.
• equ (equals) is not an instruction but a directive
to the assembler
org
0x0000
• is another directive. It tells that all the following
code should be programmed into program
memory locations starting with address 0.
• This is important because as soon as the supply
voltage is connected to the controller it always
starts executing code from address 0.
MOVF x1, W • means move the contents of x1. • The W means move
MOVF x1, W
• means move the contents of x1.
• The W means move it to the working register W
in the CPU.
• ADDLW 0x5 means add 5 to W and put the
result back into W.
• The number after ADDLW must be a number
between 0x00 and 0xFF.
• MOVWF y1
means move the contents of W
into y1.
• main NOP • is the first real instruction. Since this instruction is placed in
• main
NOP
• is the first real instruction. Since this instruction
is placed in the program memory location with
the address 0, the label main means address 0.
• Remember to distinguish directives from
instructions.
• The instructions are given to the microcontroller
and the directives to the assembler.
• Try to write instructions with capital letters.
• NOP means no operation.
• If a program does not run in an infinite loop the last instruction must
• If a program does not run in an infinite
loop the last instruction must be SLEEP.
(or goto $)
• All assembly programs must end with the
directive end.
IInnssttrruuccttioionn ccoodede • In the document instructions.pdf you find the 14 bit code for all
IInnssttrruuccttioionn ccoodede
• In the document instructions.pdf you find the 14 bit code
for all instructions.
• The 7 f:s in 20 of the instructions make up a data
memory address.
• We know from before that we actually need 9 bits to be
able to access all data memory locations.
• Where we get the remaining 2bits in these instructions
will be explained soon.
• The d in 14 instructions means that the result is put in W
if d is 0 and in f if d is 1.
• The 8 k:s in 7 instructions must be a number between
0x00 and 0xFF. 2 instructions have 11 k:s. They indicate
a program memory address.
• The 3 b:s in 4 instructions identify a bit in a register.
• An x means that the value of a bit is ignored.
TThehe dadattaa mmeemmoorryy ooff PPIICC1616FF877A877A • A data memory location is called a register. •
TThehe dadattaa mmeemmoorryy ooff PPIICC1616FF877A877A
• A data memory location is called a register.
• Some registers have a predefined name. Such a
register has a special function and is referred to
as a Special Function Register, SFR.
• All other registers are called General Purpose
Registers.
• In the document registers.pdf you find a table
with all the registers.
• address assembly code • 0x00000 NOP • 0x001 MOVF x1, • 0x0002 ADDLW 0x5
• address
assembly code
• 0x00000
NOP
• 0x001
MOVF x1,
• 0x0002
ADDLW 0x5
• 0x0003
MOVWF y1
machine code
00 0000 0xx0 0000
00 1000 0010 0000
11 111x 0000 0101
00 0000 1010 0100
Op-code
Data/operand
d
DaDattaa mmeemmoorryy banbanksks • There is a difficulty when accessing the data memory in 20
DaDattaa mmeemmoorryy banbanksks
• There is a difficulty when accessing the
data memory in 20 of the instructions.
• We need 9 bits to completely identify a
memory location.
• There is however only room for 7 bits in an
instruction to identify a memory address.
• The possible addresses in an instruction
are from 0x00 to 0x7F.
• To be able to access all the data memory location this memory is divided
• To be able to access all the data memory
location this memory is divided into 4 groups
called banks.
• Bank0 has addresses from 0x000 to 0x07F.
• Bank1 has addresses from 0x080 to 0x0FF.
• Bank2 has addresses from 0x100 to 0x17F.
• Bank3 has addresses from 0x180 to 0x1FF.
• The four registers on each row on the previous
page have a common address in the
instructions.
SSTTAATTUUSS REREGGIISSTERTER • There is a Special Function Register called STATUS which is accessed with
SSTTAATTUUSS REREGGIISSTERTER
• There is a Special Function Register called STATUS which is accessed
with the address B’000 0011’ regardless of register bank.
• Let us look at four such registers and their addresses: • PIR1 0x00C •
• Let us look at four such registers and their
addresses:
• PIR1
0x00C
• PIE1
0x08C
• EEDATA
0x10C
• EECON1
0x18C
B’0 0000 1100’
B’0 1000 1100’
B’1 0000 1100’
B’1 1000 1100’
• Only the 7 least significant bits of these
addresses are used in an instruction.
• This means that there is no point in identifying a
memory location with more that 7 bits.
• The two bits RP1 (bit 6) and RP0 (bit 5) make up the two
The two bits RP1 (bit 6) and RP0 (bit 5) make up the two
most significant bits of the data memory address.
Let us look at some code (note that it is not a complete
program):
STATUS
equ
0x3
TRISD
equ
0x8
PORTD
equ
0x8
RP0
equ
0x5
RP1
equ
0x6
BCF
STATUS,RP1
BSF
STATUS,RP0
;bank1
CLRF
TRISD
BCF
STATUS,RP0
;bank0
MOVLW
0X62
MOVWF
PORTD
• BCF STATUS,RP1 clears bit RP1 in STATUS • BSF STATUS,RP0 sets bit RP0 in
• BCF STATUS,RP1 clears bit RP1 in
STATUS
• BSF STATUS,RP0 sets bit RP0 in
STATUS
• CLRF TRISD clears all bits in TRISD
(all bits of PORTD become outputs)
• MOVLW 0x62 inserts 0x62 into W
• MOVWF PORTD moves the contents of W
to PORTD
• The pins are bi-directional. • Each port register has a data direction register. •
• The pins are bi-directional.
• Each port register has a data direction register.
• TRISB belongs to PORTB.
• If a bit in TRISB is set to 1, the corresponding
PORT bit becomes an input.
• Let us make pins RB0-RB3 inputs and pinsRB4-
RB7 outputs:
• BCF STATUS,RP1
• BSF STATUS,RP0
;bank1
• MOVLW
B’00001111’
• MOVWF
TRISB
PPoorrttss andand dadattaa ddiirrececttiionon ooff poporrtsts • There are 5 ports in PIC16F877A. • PORTA
PPoorrttss andand dadattaa ddiirrececttiionon ooff poporrtsts
• There are 5 ports in
PIC16F877A.
• PORTA has 6 bits, RA0 – RA5.
• PORTB to PORTD have 8 bits.
• PORTE has 3 bits, RE0 – RE2.
• These registers are accessible on
pins on the controller chip.
AA ccoommpleplettee ssmmallall pprrogogrramam • On the PCB we use in the practical exercises three
AA ccoommpleplettee ssmmallall pprrogogrramam
• On the PCB we use in the practical exercises
three pushbuttons are connected to bits 0,1 and
2 of PORTB. Zeros (0 V) are normally connected
to these bits.
• When a pushbutton is pressed a one (5V) is
connected to the corresponding bit of PORTB.
• The three bits 0, 1 and 2 of PORTD are
connected to three light emitting diodes, LEDs.
• PORTD is connected to a display
TThanhankk yyouou • Here is an assembly program which will make a LED illuminate when
TThanhankk yyouou
TThanhankk yyouou
• Here is an assembly program which will make a LED illuminate when the corresponding
• Here is an assembly program which will make a LED illuminate when
the corresponding pushbutton is pressed:
; name: butleds
; program
; date: 2006-10-10
org
0x0000
;author
;register definitions
main
BCF
STATUS,RP1
BSF
STATUS,RP0
TRISB
equ
0x6
MOVLW
0XFF
PORTB
equ
0x6
MOVLW
TRISB
TRISE
equ
0x9
CLRF
TRISE
PORTE
equ
0x9
BCF
STATUS,RP0
STATUS
equ
0x3
Again
MOVF
PORTB,0
;
bit definitions
MOVWF
PORTE
RP0
equ
0x5
GOTO Again
RP1
equ
0x6
END