Documente Academic
Documente Profesional
Documente Cultură
Spring 2008
Control Directives:
ORG tells the code where to start in memory
NAM should be the first line in a source program;
it gives a name to the program
Data Directives:
FCB Form Constant Byte
FDB Form Double Byte
FCC Form Constant Character
EQU EQUal
RMB Reserve Memory Byte
Flowchart Symbols
Example
Write a program to add the decimal numbers:
1, 5, 9, 13, 17 and 21.
Assume that the numbers are stored in memory locations
C100 C105
Flow Chart
Algorithm
Initialize COUNT
Initialize Pointer
LOAD M(Pointer)
Again: ADD M(Pointer+1)
Dec COUNT
Test if COUNT=Zero
Not? Inc Memory Pointer
Branch to Again
Yes? Finish
Program Documentation
It is very important that every program that is written in
Assembly language be very well documented in order to
make the program understandable to other people an to
the programmer himself.
Written description of how the program works
Flowcharts
Comments
Assembly Code
LDAB #$6
LDAX #C100
LDAA 0,X
Next INX
ADDA 0,X
DECB
BNE
Next
SWI
; initialize counter
; initialize pointer
; load first data
; increment pointer
; add next data
; decrement counter
; check if finished
Branching Instructions
A branch or jump is a break in the normal sequence of
execution of a program.
Typically the instructions of a program are executed
sequentially, the next instruction in the program is executed
after the previous one has been executed. When a program
is been executed, the PC is incremented so that it points to
location in memory of the next instruction in the program to
be executed. However, a branch instruction makes the PC
to be loaded with a new memory address that is not
contiguous to the instruction that was previously executed in
the program.
Unconditional Branch:
When the Microcontroller arrives to the branch
instruction, it jumps to the new branch instruction, it
jumps to the new location indicated by the
instruction and continues normal execution of the
program from the new location.
Conditional Branch:
Example
Write a program to generate a long chain of numbers that
follow the sequence:
1, 5, 9, 13, . , 20,001
Notice that the next number on the sequence is given by:
New n= Old n+ 4
Variables
N = New number of the sequence to be added
L = counter
The values of N and L should follow the sequences:
N: 1 5 9 13 17 20,001
L : 1 2 3 4 5 5,001
Flow Chart
Variables in Memory
Assume that memory locations $1000 and $1001 are
used to store N (16 bit number), and that memory
locations $1002 and $1003 are used to store L (16 bit
number)
The program code to implement the flow chart is as
follows:
; Initialization Process
Zero
EQU
Four
EQU
ORG
$1000
FDB
FDB
5001
ORG
$2000
LDX
; read counter
LDD
ADDD
#Four
;N=N+4
STD
LOOP
DEX
BNE
SWI