Lecture 5
Lecture 5
Program.asm
Program.hex
assembler/ (machine language)
translator 11 00xx 0010 0000 programmer
MOVLW 0x20
Representation Numbers in
Assembler
Hexadecimal:
MOVLW 99H, MOVLW 0x99, MOVLW 99, MOVLW h’99’
ASCII:
MOVLW A’2’
;WREG = 00110010 or 32 in hex
Decimal:
MOVLW D’12’, MOVLW .12
;WREG = 00001100 = 0CH = 12
Binary:
MOVLW B’10011001’
;WREG = 1001101 or 99 in hex
Representation Numbers in
Assembler cont…
Structure:
[label] mnemonic [operands] [;comment]
Labels
Allows the program to refer to a line of code or section of
program by name
Marco, branching, goto
Instructions
The way we write an instruction (syntax)
Operands
Instruction element for the instruction that is being executed
Registers / variables / constants
Comments
Begin with semicolon (;)
Series of words that a programmer writes to make
itself
Control Directives
#DEFINE Exchange one part of text for another
Syntax:
#define<text> [<another text>]
Example:
#define turned_on 1
#define turned_off 0
Control Directives cont…
Syntax:
#include <file_name>
#include "file_name"
Example:
#include <regs.h>
#include "subprog.asm"
Control Directives cont…
Syntax:
<name_constant> equ <value>
Example:
five equ 5
six equ 6
seven equ 7
Control Directives cont…
;in binary
DATA6 EQU b’00110101’ ;binary (35 in hex)
DATA7 EQU B’00110101’ ;binary (35 in hex)
Control Directives cont…
;in decimal
DATA8 EQU D’28’ ;decimal numbers (1C in hex)
DATA9 EQU d’28’ ;second way for decimal
;in ASCII
DATA10 EQU A’2’ ;ASCII characters
DATA11 EQU a’2’ ;another way for ASCII char
DATA12 EQU ‘2’ ;another way for ASCII char
Control Directives cont…
Syntax:
<label>org<value>
Example:
Start org 0×00
movlw 0xFF
movwf PORTB
Control Directives cont…
Syntax:
End
Example:
.
.
movlw 0xFF
movwf PORTB
end
Command Operand
; Start main loop
;...............................................................
Comment
Review
1. _______ are translated by the assembler into machine code,
whereas _______ are not.
2. True or false. Assembly language is a high-level language.
3. Which of the following instructions produces opcode? List all
that do.
(a) MOVLW 25H (b) ADDLW 12
(b) ORG 2000H (d) GOTO HERE
4. True or false. Assembler directives are not used by the CPU
itself. They are simply a guide to the assembler.
5. In Question 3, which one is an assembler directive?
Assembling & Linking a PIC
Program
Editor
Program
myfile.asm
Editor
Assembler
Program
myfile.err myfile.o
.o additional
.lib additional
object files
library files Linker
Program .lkr linker
script files
Download to
PIC’s ROM
List File
00000000 00001 allout EQU 00 ;Define Data Direction Code
00000005 00002 porta EQU 05 ;Port A data register
00000006 00003 portb EQU 06 ;Port B data register
00004
0000 3000 00005 MOVLW allout ;Load W with Direction Code
0001 0066 00006 TRIS portb ;Send code to direction register
00007
0002 0186 00008 reset CLRF portb ;Start output at 00000000
0003 1C05 00009 start BTFSS porta,0 ;Test R0 input button
0004 2802 00010 GOTO reset ;and reset Port B if pressed
0005 1885 00011 BTFSC porta,1 ;Test R1 input button
0006 2803 00012 GOTO start ;and run count if pressed
0007 0A86 00013 INCF portb ;Increase output by 1
0008 2803 00014 GOTO start ;Repeat main loop
00015
00016 END ;Terminate Program
Flowchart
PIC Program
A
Convert specification into
algorithm/flowchart
Logical
Error?
Yes
Edit/write source code No
Done
A
Subroutine
Subprogram that represents a set of instructions
begin with a label & end with the instruction
return or retlw.
Executed when call subprogram_name is
encountered in program.
Can be located anywhere in the program,
Yes
Reset?
No
No
Run?
Yes
Increment output
Delay
Example
Example
Example
Write a program to count up from 00 to FFH, save the
count value at location 10H (GPR RAM address), then
send the count value to SFR of Port B. Use one CALL
subroutine for sending the data to Port B and another
one for time delay. Put a time delay in between each
issuing of data to Port B.
Example
Example
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
Subroutine:
Use stack space when it is called
Total 767
If Clock frequency = 4MHz
then Instruction Frequency = 1MHz
and Instruction Period = 1s
and Total Delay Time = 767 s
Execution Time cont…
Find the size of the delay of the code snippet below if the crystal
frequency is 4MHz:
Instruction cycle
MYREG EQU 0x08 ;use loc 08 as counter
DELAY MOVLM 0xFF 1
MOVWF MYREG 1
AGAIN NOP 1
NOP 1
DECFSZ MYREG, F 1
GOTO AGAIN 2
RETURN 2
;************************************************************
ORG 000
GOTO start ;Jump to start of main program
;Main loop...................................................
newbar CLRF point ;Reset pointer to start of table
Ex.
LFSR 0, 0x30 ; Load FSR0 with address 0x30 (pointing to memory location)
MOVF INDF0, W ; Move data from the location pointed by FSR0 into WREG
PIC18 Instruction Examples
Add Instructions
addwf 0x20,F,A ; add data register and WREG and place sum in WREG
Subtract Instructions
RISC CISC
Separated data and program memory Combined data and program memory
Most operations are register to register Most operations can be register to memory
Take shorter time to design and debug Take longer time to design and debug
Provide large number of CPU registers Provide smaller number of CPU registers