Transcript Lec5
EE 319K
Introduction to Microcontrollers
Lecture 5: Conditionals, Loops,
Modular Programming, Subroutines, Parameter passing
5-1
Conditionals
> < ≥ ≤
conditional
branch
instructions
must follow a
subtract
compare or test
instruction,
such as
suba subb
sbca sbcb
subd cba cmpa
cmpb cpd cpx
cpy tsta tstb
tst
Ramesh Yerraballi
C code
if(G2 == G1){
isEqual();
}
if(G2 != G1){
isNotEqual();
}
if(H2 == H1){
isEqual();
}
if(H2 != H1){
isNotEqual();
}
assembly code
ldaa G2
cmpa G1
bne next
jsr isEqual
next
ldaa G2
cmpa G1
beq next
jsr isNotEqual
next
ldd H2
cpd H1
bne next
jsr isEqual
next
ldd H2
cpd H1
beq next
jsr isNotEqual
next
Equality/Inequality check
5-2
Signed Conditional
bge target
;Branch if signed greater than or equal to (),
;if (N^V)=0, or (~N.V+N.~V)=0
bgt target
;Branch if signed greater than (>),
;if (Z+N^V)=0, or (Z+~N.V+N.~V)=0
ble target
;Branch if signed less than or equal to (),
;if (Z+N^V)=1, or (Z+~N.V+N.~V)=1
blt target
;Branch if signed less than (<),
;if (N^V)=1, or (~N.V+N.~V)=1
Ramesh Yerraballi
5-3
… Signed Conditional
C code
if(G2 > G1){
isGreater();
}
if(G2 >= G1){
isGreaterEq();
}
if(G2 < G1){
isLess();
}
if(G2 <= G1){
isLessEq();
}
assembly code
ldaa G2
cmpa G1
ble next
jsr isGreater
next
ldaa G2
cmpa G1
blt next
jsr isGreaterEq
next
ldaa G2
cmpa G1
bge next
jsr isLess
next
ldaa G2
cmpa G1
bgt next
jsr isLessEq
next
,<, , > checks
Ramesh Yerraballi
5-4
Unsigned Conditional
bhs target
;Branch if unsigned greater than or equal to (),
;if C=0, same as bcc
bhi target
;Branch if unsigned greater than (>),
;if C+Z=0
bls target
;Branch if unsigned less than or equal to (),
;if C+Z=1
blo target
;Branch if unsigned less than (<),
;if C=1,
same as bcs
Ramesh Yerraballi
5-5
… Unsigned Conditional
C code
if(G2 > G1){
isGreater();
}
if(G2 >= G1){
isGreaterEq();
}
if(G2 < G1){
isLess();
}
if(G2 <= G1){
isLessEq();
}
assembly code
ldaa G2
cmpa G1
bls next
jsr isGreater
next
ldaa G2
cmpa G1
blo next
jsr isGreaterEq
next
ldaa G2
cmpa G1
bhs next
jsr isLess
next
ldaa G2
cmpa G1
bhi next
jsr isLessEq
next
,<, , > checks
Ramesh Yerraballi
5-6
Problem Solving
When we solve problems on the computer, we
need to answer these questions:
What
does
being
in
a
state
mean?
List state parameters
What
is
the
starting
state
of
the
system?
Define the initial state
What
information
do
we
need
to
collect?
List the input data
What
information
do
we
need
to
generate?
List the output data
How do we move from one state to another?
Actions we could do
What is the desired ending
state?
Define the ultimate goal
Ramesh Yerraballi
5-7
Successive Refinement
Start with a task and decompose the
task into a set of simpler subtasks
Subtasks are decomposed into even
simpler sub-subtasks.
Each subtask is simpler than the task
itself.
Make design decisions
Subtask is so simple, it can be converted
to software code.
Ramesh Yerraballi
5-8
Decomposition
Task
True
Subtask 1
Subtask 2
Condition
False
Condition
Subtask 1
Subtask 2
True
Subtask
Subtask
False
Sequential
Conditional
“do A then do B”
“do A and B in either order”
“if A, then do B”
“for each A, do B”
“do A until B”
“repeat A over & over forever”
“on external event do B”
“every t msec do B”
Ramesh Yerraballi
Iterative
Interrupt
→ sequential
→ sequential
→ conditional
→ iterative
→ iterative
→ iterative (condition always true)
→ interrupt
→ interrupt
5-9
Successive Refinement: Example
Cnt = 0
Initialize
Initialize
Create
next
digit
Create
digits
done?
Output
digits
Ramesh Yerraballi
yes
no
N = N/10
R = remainder
Pull R
ch = R+$30
Cnt--
Push R
Cnt++
OutChar(ch)
!=0
N
!=0
Cnt
=0
=0
Output
digits
5-10
if-then-else
G1<=G2
isLessEq
low
high
next
ldaa
cmpa
bhi
jsr
bra
jsr
G1>G2
isGreater
G1
if(G1>G2){
G2
high ; branch if G1>G2
isLessEq
; G1<=G2
isGreater();
next
}
else{
isGreater ; G1>G2
isLessEq();
}
Two alternative implementations
high
low
next
Ramesh Yerraballi
ldaa
cmpa
bls
jsr
bra
G1
G2
low ; branch if G1≤G2
isGreater ; G1>G2
next
jsr
isLessEq
; G1<=G2
if(G1>G2){
isGreater();
}
else{
isLessEq();
}
5-11
while loop
G2>G1
G2<=G1
loop ldaa
cmpa
bls
jsr
bra
next
Ramesh Yerraballi
G2
G1
next
Body
loop
;stop if G2≤G1
;body of loop
Body
while(G2 > G1){
Body();
}
5-12
for loop
for(i=0; i<100; i++){
Process();
}
i=0
i
i >= 100
for(i=100; i!=0; i--){
Process();
}
i= 100
i < 100
Process
i
i!= 0
i== 0
i= i-1
i = i+1
loop
ldab #0
cmpb #100
bhs
done
jsr
Process
incb
bra
loop
; i=0
Process
for(i=0; i<100; i++){
Process();
}
; i=i+1
done
L1
ldab #100
jsr
Process
dbne B,L1
; i=100 for(i=100; i!=0; i--){
Process();
;i=i-1 }
done
The dbne instruction optimizes this for-loop implementation.
Ramesh Yerraballi
5-13
Modular Design
Goal
Clarity
Create a complex system from simple parts
Definition of modularity
Maximize number of modules
Minimize bandwidth between them
Entry point (where to start)
The label of the first instruction of the subroutine
Exit point (where to end)
The rts instruction
Good practice, one rts as the last line
Ramesh Yerraballi
5-14
Modular Design
Public (shared, called by other modules)
Add underline in the name, module name before
Private (not shared, called only within this
module)
No underline in the name
Helper functions
Coupling (amount of interaction between
modules)
Data passed from one to another (bandwidth)
Synchronization between modules
Ramesh Yerraballi
5-15
Subroutines and the Stack
classical definition of the
stack
push saves data on the top
of the stack,
pull removes data from the
top of the stack
stack implements last in first
out (LIFO) behavior
stack pointer (SP) points to
top element
many uses of the stack
temporary calculations
subroutine (function) return
addresses
subroutine (function)
parameters
local variables
Ramesh Yerraballi
psha push Register A on
the stack
pshb push Register B on
the stack
pshx push Register X on
the stack
pshy push Register Y on
the stack
des S=S-1(reserve space)
pula
pull
from
stack
into A
pulb
pull
from
stack
into B
pulx
pull
from
stack
into X
puly
pull
from
stack
into Y
ins S=S+1 (discard top
of stack)
5-16
Registers to pass parameters
High level program
1) Sets Registers to
contain inputs
2) Calls subroutine
6) Registers contain
outputs
Ramesh Yerraballi
Subroutine
3) Sees the inputs in
registers
4) Performs the
action of the
subroutine
5) Places the
outputs in registers
5-17
Introduction to Pointers
Not pointing to anything
Pt
Object1
Object2
Pointing to Object1
Pt
Object1
Object2
Pointing to Object2
Pt
Object1
Object2
Pointers are addresses pointing to objects.
The objects may be data, functions,
or other pointers:
• If register X or Y contains an address we say
it points into memory
Ramesh Yerraballi
5-18
Use of Index registers
ldaa 0,x
pointer
X
data
A
ldd 0,y
pointer
Y
Ramesh Yerraballi
data
A
B
5-19
Functional Debugging
Instrumentation: dump into array without filtering
Assume happy is strategic 8-bit variable.
SIZE
Buf
Pt
equ 20
rmb SIZE
rmb 2
#define SIZE 20
unsigned char Buf[SIZE];
unsigned char *Pt;
Pt will point into the buffer.
Pt must be initialized to point to the beginning, before the debugging begins.
ldx #Buf
stx Pt
Ramesh Yerraballi
Pt = Buf;
5-20
… Functional Debugging
The debugging instrument saves the strategic variable into the Buffer.
Save
pshb
pshx
ldx
cpx
bhs
ldab
stab
inx
stx
done
pulx
pulb
rts
;save
Pt ;X=>Buf
#Buf+SIZE
done ;skip if full
happy
0,X ;save happy
;next address
Pt
void Save(void){
if(Pt < &Buf[SIZE]){
(*Pt) = happy;
Pt++;
}
}
Next, you add jsr Save statements at strategic places within the system.
Use the debugger to display the results after program is done
Ramesh Yerraballi
5-21