4 Starting to Program - An Introducing to Assember
Download
Report
Transcript 4 Starting to Program - An Introducing to Assember
Starting to Program – An
Introduction to Assembler
Chapter Four
Dr. Gheith Abandah
1
Outline
•
•
•
•
•
•
•
•
•
Introduction
The PIC 16 Series Instruction Set
Byte-oriented file register operations
Bit-oriented file register operations
Literal and control operations
Instruction Formats
Operation Types
MPASM Assembler
Example
Dr. Gheith Abandah
2
Introduction
Micro-controller Programming Options:
1. Machine Code
00 0111 0001 0101
2. Assembly Language: needs an assembler
addwf NUM, w
3. High-Level Language: needs a compiler
for (i=0; i<10; i++) sum += a[i];
Dr. Gheith Abandah
3
Development Process
Dr. Gheith Abandah
4
ALU Data Flow
Dr. Gheith Abandah
5
The PIC 16 Series Instruction Set
• 35 instructions
• Three Groups:
1. Byte-oriented file register operations
2. Bit-oriented file register operations
3. Literal and control operations
• Operation Types:
– Arithmetic
– Data movement
– Misc
– Logic
– Control
Dr. Gheith Abandah
6
1. Byte-oriented file register
operations
• Format: op f, d
– op: operation
– f: number of file or register
– d: destination (0: working register, 1: file register)
• Example:
addwf PORTA, 0
Adds the contents of the working register and
register PORTA, puts the result in the working
register.
Dr. Gheith Abandah
7
2. Bit-oriented file register
operations
• Format: op f, b
– op: operation
– f: number of file or register
– b: bit number, 0 through 7
• Example:
bsf
STATUS, 5
Sets to 1 Bit 5 of register STATUS.
Dr. Gheith Abandah
8
3. Literal and control operations
• Format: op k
– op: operation
– k: literal, an 8-bit if data or 11-bit if address
• Examples:
addlw 5
Adds to the working register the value 5.
call 9
Calls the subroutine at address 9.
Dr. Gheith Abandah
9
Instruction
Formats
Dr. Gheith Abandah
10
Arithmetic Operations
Mnemonic Operands
Description
Cycles
Status
Affected
ADDWF
f, d
Add W and f
1
C,DC,Z
COMF
f, d
Complement f
1
Z
DECF
f, d
Decrement f
1
Z
INCF
f, d
Increment f
1
Z
SUBWF
f, d
Subtract W from f
1
C,DC,Z
ADDLW
k
Add literal and W
1
C,DC,Z
SUBLW
k
Subtract W from literal
1
C,DC,Z
Dr. Gheith Abandah
11
Logic Operations
Mnemonic Operands
Description
Cycles
Status
Affected
ANDWF
f, d
AND W with f
1
Z
IORWF
f, d
Inclusive OR W with f
1
Z
XORWF
f, d
Exclusive OR W with f
1
Z
ANDLW
k
AND literal with W
1
Z
IORLW
k
Inclusive OR literal with W
1
Z
XORLW
k
Exclusive OR literal with W
1
Z
Dr. Gheith Abandah
12
Data Movement Operations
Mnemonic Operands
Description
Cycles
Status
Affected
Z
MOVF
f, d
Move f
1
MOVWF
f
Move W to f
1
SWAPF
f, d
Swap nibbles in f
1
MOVLW
k
Move literal to W
1
Dr. Gheith Abandah
13
Control Operations
Mnemonic Operands
DECFSZ
INCFSZ
BTFSC
BTFSS
CALL
GOTO
RETFIE
RETLW
RETURN
f, d
f, d
f, b
f, b
k
k
k
-
Description
Cycles
Decrement f, Skip if 0
Increment f, Skip if 0
Bit Test f, Skip if Clear
Bit Test f, Skip if Set
Call subroutine
Go to address
Return from interrupt
Return with literal in W
Return from Subroutine
1 (2)
1 (2)
1 (2)
1 (2)
2
2
2
2
2
Dr. Gheith Abandah
Status
Affected
14
Miscellaneous Operations
Mnemonic Operands
CLRF
CLRW
NOP
RLF
f
f, d
RRF
f, d
BCF
BSF
CLRWDT
SLEEP
f, b
f, b
-
Description
Cycles
Clear f
Clear W
No Operation
Rotate Left f through Carry
Rotate Right f through
Carry
Bit Clear f
Bit Set f
Clear Watchdog Timer
Go into standby mode
1
1
1
1
Dr. Gheith Abandah
Status
Affected
Z
Z
C
1
C
1
1
1
1
TO',PD'
TO',PD'
15
Assembler Format
Dr. Gheith Abandah
16
Assembler Directives
Assembler
directive
Summary of action
list
Implement a listing option
#include
Include additional source file
org
Set program origin
equ
Define an assembly constant; this
allows us to assign a value to a label
end
End program block
Dr. Gheith Abandah
17
Number Representation in MPASM
Radix
Example
Decimal
D’255’
Hexadecimal
H’8d’ or 0x8d
Octal
O’574’
Binary
B’01011100’
ASCII
‘G’ or A’G’
Dr. Gheith Abandah
18
Example: move push button to LED
Dr. Gheith Abandah
19
Example – Page 1
;***************************************************
;ELECTRONIC PING-PONG DATA MOVE
;This program moves push button switch values from
;Port A to the leds on Port B
;TJW 21.2.05 Tested 22.2.05
;***************************************************
;
;Configuration Word: WDT off, power-up timer on,
; code protect off, RC oscillator
;
list p=16F84A
Dr. Gheith Abandah
20
Example – Page 2
;
;specify SFRs
status
equ
porta
equ
trisa
equ
portb
equ
trisb
equ
;
03
05
05
06
06
Dr. Gheith Abandah
21
Example – Page 3
;
org 00
;Initialise
start
bsf status,5 ;select memory bank 1
movlw B’00011000’
movwf trisa
;set port A direction
movlw 00
movwf trisb
;all port B bits output
bcf status,5 ;select bank 0
Dr. Gheith Abandah
22
Example – Page 4
;
;The "main" program starts here
clrf porta
;clear all bits in ports A
loop
movf porta,0 ;move port A to W register
movwf portb
;move W register to port B
goto loop
end
Dr. Gheith Abandah
23
Summary
• Assembler is a programming language that is part of the toolset
used in embedded systems programming. It comes with its own
distinct set of rules and techniques.
• It is essential to adopt and learn an IDE when developing
programs. The MPLAB IDE is an excellent tool for PIC
microcontrollers, both for learners and professionals. And it
can’t be beaten on price!
• While some people are eager to get programs into the
hardware immediately, it is extremely useful to learn the
features of a simulator. The simulator in MPLAB allows the user
to test program features with great speed, and is an invaluable
learning tool.
Dr. Gheith Abandah
24