Lecture 4 Program Loops and Arrays
Lecture 4 Program Loops and Arrays
Arrays
Outline
Indexed addressing
Program structure
Data arrays
Delay loops
Table Lookup
External references
4-1
IND,X
IND,Y
; [X+IND] A
; [Y+IND] A
4-2
4-3
oprx0_xysp: This word breaks down into one of the following alternative forms that
assemble to an 8-bit indexed addressing postbyte code, which is designated as xb
oprx5,xysp
oprx3,xys
oprx3,+xys
oprx3,xys
oprx3,xys+
abd,xysp
oprx3: A label or expression that evaluates to an offset in the range +1 to +8
oprx5: A label or expression that evaluates to a 5-bit offset in the range 16 to +15
oprx9: A label or expression that evaluates to a 9-bit offset (range 256 to +255)
oprx16: A label or expression that evaluates to a signed or unsigned 16-bit offset
opr8a A label or expression that evaluates to an 8-bit address.
opr16a A label or expression that evaluates to a 16-bit address.
opr8i A label or expression that evaluates to an 8-bit immediate value
opr16i A label or expression that evaluates to a 16-bit immediate value
ECE 362 Microprocessor Systems and
Interfacing
4-4
4-5
X $800
ECE 362 Microprocessor Systems and
Interfacing
$800 $12
$801 $34
$802 $56
$803 4-6$78
4-7
Whats Subroutine?
When is it used?
a section of code is used multiple times
ECE 365
Whats Subroutine?
ECE 365
How?
4-10
.
JSR Sub1
1000 Sub1 first instruction
next instruction
..
.
..
RTS
When calling
PC
1000
When returning
203
Stack
memory
11
Subroutine
203
ECE 365
Performed by
the processor
automatically
Add2ToA: ADDA #2
RTS
RTS causes the program to return from the subroutine to the caller.
Program execution returns to the next instruction after the JSR
instruction.
RTS should be the last instruction in subroutines.
The stack pointer must be initialized before making a subroutine call,
e.g., using LDS#$c00 ; load into SP
ECE 362 Microprocessor Systems and
Interfacing
4-12
In subroutines
4-13
PULA
PULB
PULD
PULX
PULY
4-14
for
while
do-while
if-else
4-15
4-16
}
4-17
SendChar(*charPtr);
charPtr++;
}
ECE 362 Microprocessor Systems and
Interfacing
4-18
4-19
4-20
4-21
4-22
4-23
Execution time is
What is the total execution time required for the following loop?
DELAY:
LDX#14
; 2 cycles
DEX
; 1 cycle
BNE
DELAY
; 3 (back) / 1(down) cycle(s)
4-24
Answer _________
4-25
Quiz
What is the total number of clock cycles and the delay time of the
following program? Assume a crystal frequency of 1 MHz.
wt_lp:
4-26
Programming
Where to start?
What would be main coding?
What addressing mode should you use for the main
coding?
What program structure should you use?
4-27
adda
#10
staa array,x
staa**
inx
dex
------- (remove)
cpx #cnt
cpx #0
cpx ***
bne again
bne again
ldaa switches
adda
#10
staa array,x
ECE 362 Microprocessor Systems and
Interfacing
4-28
4-29
Whats a null?
Where to start?
What would be main coding?
What addressing mode should you use for the main
coding?
What program structure should you use?
4-30
4-31
4-32
beq
4-33
4-34
4-35
Where to start?
What would be main coding?
What addressing mode should you use for the main
coding?
What program structure should you use?
Do you have to initialize any?
flags?
variables?
4-36
4-37
3-38
4-39
Where to start?
What would be main coding?
What addressing mode should you use for the main coding?
What program structure should you use?
What are the input and output?
VAR: DS.B
DAT: DC.B
DC.B
START:
#DAT
LDX
4-40
START:
LDX
#DAT
; set base address
NEXT: LDAA
0,X ; load a value from list
CMPA
#0 ; check NULL
BEQ
TERM
CMPA
VAR ; keep scanning
BEQ
MATCH
INX
BRA
NEXT
MATCH: NOP
; do when match is found
TERM: NOP
; did not find match
ECE 362 Microprocessor Systems and
Interfacing
4-41
Example
Where to start?
What would be main coding?
What addressing mode should you use for the main coding?
What program structure should you use?
Do you have to initialize any?
What are the input and output?
4-42
; Fahrenheit
; Fahrenheit
3-43
4-44
3-45
4-46
4-47
4-48
External references.
4-49
4-50