You are on page 1of 50

Lecture 4 Program Loops and

Arrays
Outline

Indexed addressing

Program structure

Basic Subroutine and Stack

Data arrays

Delay loops

Table Lookup

External references

ECE 362 Microprocessor Systems and


Interfacing

4-1

Do you remember Indexed Addressing mode?

When need to access data arrays, use


LDAA
LDAA

IND,X
IND,Y

; [X+IND] A
; [Y+IND] A

Registers X and Y are index registers (i.e., they contain


address or pointer)
IND can be a 3-bit auto offset, 5-bit, 9-bit or 16-bit signed
offset/label.
LDAA +10,X
; [X+10] A
LDAA -100,Y
; [Y-100] A
ECE 362 Microprocessor Systems and
Interfacing

4-2

ECE 362 Microprocessor Systems and


Interfacing

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

Indexed Addressing: Autoincrement


and Autodecrement, Pre- or Post

The index register can be automatically incremented or


decremented to change the location being accessed.
It can be modified either before or after it is referenced.

contents of index registers are changing

It can be adjusted in the amount of -8 ~ -1, 1 ~ 8.


ldaa 2,X+ ; reference first, then add 2; suffixafter ref
ldaa 4,-X ; subtract 4, then reference; prefixbefore
ref
ldaa 4,+X ; add 4 (i.e., X+4 X), and then reference
ECE 362 Microprocessor Systems and
Interfacing

4-5

Indexed Addressing Modes (X is pointer)

LDAA 1,X ; where does it access?


; what is X value after execution?
LDAA 1,+X ; where does it access?
; what is X value after execution?
LDAA 1,X+ ; where does it access?
; what is X value after execution?
LDD
2,+X ; what value is loaded into D?
; what is X value after execution?

X $800
ECE 362 Microprocessor Systems and
Interfacing

$800 $12
$801 $34
$802 $56
$803 4-6$78

Indexed Addressing Exercise


My_constant: section
dataBytes: dc.b
$10,$20,$30,$40
My_code: section
ldx
#dataBytes
ldaa
3,x

What is the value loaded into X when the program


executes? _______
What value is loaded into A? _______

ECE 362 Microprocessor Systems and


Interfacing

4-7

Whats Subroutine?

When is it used?
a section of code is used multiple times

ECE 365

like C function, e.g., printf();


We separate it and make a standalone code
for easy handling, debugging, and maintenance

Whats Subroutine?

A subroutine has entry and exit

The entry is associated with a label (used as subroutine name)


The exit has a return-from-subroutine instruction (RTS)
A subroutine is invoked by using JSR instruction

When a JSR instruction is executed, the address of subroutine


name (label) is entered into PC and thereby jump occurs
After executing the subroutine, CPU needs to come back to the
next instruction after the JSR instruction

ECE 365

JSR means jump to subroutine


JSR subroutine_name
; JSR printf

How?

Since a subroutine is executed and returned, CALL instead of JSR


is more commonly used

How to return from Subroutine?

Right before jump, the address of next-to-JSR instruction


(contents of the PC, called return address) must be saved
to enable correct return
JSR is a special branch instruction that performs the
following operations

store the contents of the PC onto stack


jump to the target address specified by the JSR instruction

The return-from-subroutine (RTS) instruction moves the


return address (saved on the stack) to PC and thereby
return jump occurs
What is stack?

a special memory space used for subroutine calls


ECE 362 Microprocessor Systems and
Interfacing

4-10

Steps of Subroutine call & return using Stack


Memory
location
200
203

Calling program Memory


location

.
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

Behavior of 68HC12 Subroutine Call & Return

JSR SubroutineName calls a subroutine

subroutine has a label (used by callers)


JSR Add2ToA
STAA PORTS

; side stepped for repeated tasks

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

What about Registers in subroutines?

In subroutines

Subroutines should not touch registers of the calling program


But we have limited number of registers, so how can we not
touch those used in the caller?
How do we know which registers the caller uses?

Instead, a subroutine must save (and restore before return)


the contents of callers registers that it is going to use

where? on the stack


how? using stack manipulation instructions

ECE 362 Microprocessor Systems and


Interfacing

4-13

What Stack Manipulation Instructions exist?


Saving register contents
Retrieving register contents
onto the stack
from the stack
PSHA
PSHB
PSHD
PSHX
PSHY

PULA
PULB
PULD
PULX
PULY

ECE 362 Microprocessor Systems and


Interfacing

4-14

Whats Programming Structure?

A few basic structures provide most of the program


building blocks needed.
Common program structures allow direct implementation
of flow charts, pseudo-code, or high-level language
statements.
Implementation of C language blocks:

for
while
do-while
if-else

ECE 362 Microprocessor Systems and


Interfacing

4-15

What kinds of Loop Types?

for (variable initialization; continue condition; variable


update) { Code to execute while the condition is true }

completion check is done at the beginning

ascending iterator, e.g., for (i=0; i<N; i++)


descending iterator, e.g., for (i=N; i>0; i--)

while (continue condition) { Code to execute while the


condition is true }

Code block may never be executed

Code block may never be executed

do {Code to execute } while (continue condition);

Code block is executed at least once


ECE 362 Microprocessor Systems and
Interfacing

4-16

How to assembly program a While loop?

// count the number of tens in a variable value


// register b is the counter
ldaa #value
Alternative
ldab #0
TensLP:cmpa #9
cmpa #10
ble done
blt done
incb
suba #10
index = 0;
bra TensLP
while (value > 9)
Done:
{
index++;
value = value - 10;
ECE 362 Microprocessor Systems and
Interfacing

}
4-17

Another example of a While loop

// send characters until NULL character


// register X points to a character string
DisplayLP: ldaa 1,x+ ; reference and then add 1 to x
beq done
psha
; char sent on stack
jsr DisplayChar; subroutine
pula
bra DisplayLP
while(*charPtr != 0)
{
done:

SendChar(*charPtr);
charPtr++;

}
ECE 362 Microprocessor Systems and
Interfacing

4-18

How to program a Do-while loop?


// register X points to a character string
ldab #10 ; the number of characters
SendLP: ldaa 1,x+
psha
; char sent on stack
jsr SendChar ; send one character
pula
dbne b,SendLP ; decrement b, and branch if
charCnt = 10
not zero
do
{
SendChar(*charPtr++);
charCnt--;
} while(charCnt > 0);
ECE 362 Microprocessor Systems and
Interfacing

4-19

How to program a For-loop?


// register b contains the count (iterator)
// register X points to the first character of a character string
// numChars contains the total character count
ldab #0 ; B is the count
SendLP: cmpb
numChars
bcc done
; if carry flag =0
ldaa 1,x+ ; passing parameter
psha
; char sent on stack
jsr SendChar
pula
for(count=0; count<numChars; count++)
incb
{
bra SendLp
SendChar(*charPtr);
done:
charPtr++;
}
ECE 362 Microprocessor Systems and
Interfacing

4-20

How to program If-then-else?


; assume A has a signed number
; output sign character according to value in register A
cmpa
#0
bge SendPlus ; jump if a #0
ldaa #-
; passing parameter bra SendIt
SendPlus:
ldaa #+ ; passing parameter +
SendIt: psha
jsr SendChar
leas 1,s
; sp = sp + 1, just modify SP
if (a 0)
; dont need to pull A back
SendChar(+);
else
SendChar(-);
ECE 362 Microprocessor Systems and
Interfacing

4-21

Instruction Timing and E-clock

CPU is a complex FSM (finite state machine)


An FSM consists of Flip-Flops (FFs) and logic gates
Flip-Flops need clocks
CPU is a sophisticated FSM operating clock by clock
Clock is generated from a crystal oscillator
Our CPUs clock frequency, referred to as E-clock, is one-half
the crystal oscillators frequency.
One memory access cycle per E-clock

When accessing internal memory, either ROM or RAM, the


processor can read up to two bytes at every memory access.

16-bit data bus internally


ECE 362 Microprocessor Systems and
Interfacing

4-22

Whats Clock Cycle?

Lab boards crystal oscillator frequency is 8 MHz.


What is the period of the E-clock on the Lab board? Answer in
microseconds. __________
One E-clock period is called a clock cycle
How to measure the elapsed time to execute a code section?

The time is called execution time

How to measure the execution time of a code section?

the number of clock cycles X clock period

ECE 362 Microprocessor Systems and


Interfacing

4-23

How to measure Execution Time?

Execution time is

the # clock cycles X clock period

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)

First, we need to count the total # clock cycles: _________


If clock period is .25 us
Then, the execution time is ___________
ECE 362 Microprocessor Systems and
Interfacing

4-24

Delay for a specific time.

Using the previous delay loop, what count is required to


delay 0.14 milliseconds? Assume an 8-MHz crystal.
First determine how many clock cycles are necessary.

Answer _________

ECE 362 Microprocessor Systems and


Interfacing

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:

;instruction ; # machine cycles


ldaa #5 ; 1
deca
;1
bne wt_lp
; 3 (loop back) / 1 (fall through)

Clock period = _________


Number of machine cycles = ______
Total Execution Time = _________seconds

ECE 362 Microprocessor Systems and


Interfacing

4-26

Programming

Reading switches 50 times into an Array

Where to start?
What would be main coding?
What addressing mode should you use for the main
coding?
What program structure should you use?

ECE 362 Microprocessor Systems and


Interfacing

4-27

Reading switches 50 times into an Array


data:
section
cnt: equ 50
Alternative 1 Alternative 2
array:
ds.b cnt
switches: equ $ae
text: section
ldx #0
ldx #cnt-1
ldx #array
again: ldaa switches

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

Reading switches 50 times into an Array

How does the previous program need to change if LDX #0


is replaced with LDX #array? Assume the program will
store the results in the same locations as in the example.

ECE 362 Microprocessor Systems and


Interfacing

4-29

Output a sequence of bytes (e.g., string)


to display (PortS) till a null is read

Whats a null?

0, which is also often called a terminator

Where to start?
What would be main coding?
What addressing mode should you use for the main
coding?
What program structure should you use?

ECE 362 Microprocessor Systems and


Interfacing

4-30

Output a sequence of bytes (e.g., string)


to display (PortS) till a null is read
PORTS: equ $d6
data:
dc.b 25,36,14,27,36,124
dc.b 99,45, 0
; 0 is the terminator
ldx #data
; set base address into X
again: ldaa 1,x+
; reference and inc pointer
cmpa #0
; check if the loaded
beq
exit
; value is the terminator
staa PORTS
bra again
exit:
ECE 362 Microprocessor Systems and
Interfacing

4-31

Another way of outputting a sequence


of bytes to PortS till a null is read
PORTS: equ $d6
data:
dc.b 25,36,14,27,36,124
dc.b 99,45, 0
; 0 is the terminator
ldx
#0
; clear index
again: ldaa #data,x
; set base address
cmpa #0
; check if the loaded
exit
; data is the terminator
staa PORTS
; display
inx
; move to next
bra again
exit:
ECE 362 Microprocessor Systems and
Interfacing

4-32

beq

Debugger Break Points

Breakpoints allow you to execute all statements up until


the selected one rather than stepping through every
statement.
If no breakpoint is placed in the program, execution
continues indefinitely (or until an end or halt instruction is
reached).
Explain how to set breakpoint in lab

ECE 362 Microprocessor Systems and


Interfacing

4-33

Debugger Break Points

ECE 362 Microprocessor Systems and


Interfacing

4-34

Laboratory 4.1 : Arrays


1. Write a program that will output the sequence shown below to the LEDs.

The LEDs are connected to Port S (address $248).


The data direction register for Port S (address $24A) will need to be
programmed to make Port S an output port.
The program should continuously loop outputting the data to the LEDs.
When the end of the data list is reached, the program should start
over at the beginning of the list outputting the data.
Use a pointer to access the list. You may consider a terminator at the
end of the list or alternatively use a counter to count to the end of the
list.
To debug, step through the program. When running the program at
speed, all the lights will appear to be on.
Use either a for style loop with a counter or while style with a
terminator.
Sequence: $81,$42,$24,$18,$00,$24,$42
ECE 362 Microprocessor Systems and
Interfacing

4-35

Unsigned Multibyte Addition using a loop

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?

ECE 362 Microprocessor Systems and


Interfacing

4-36

Unsigned Multibyte Addition using a loop


cnt: equ 4
data1: dc.b $32,$84,$4c,$9a
data2: dc.b $5a,$a2,$2a,$8f
data3: ds.b 4 ; space for result
ldab #cnt
ldx #3 ; set index to the last byte
clc
; clear carry
again: ldaa data1,x
adca data2,x
staa data3,x
dex
; does not affect Carry
dbne b, again; no flags are affected
ECE 362 Microprocessor Systems and
Interfacing

4-37

Combined Branch Instructions

IBEQ Increment the first operand and branch (to second


operand) if the first operand become zero.
IBNE Increment and branch if not zero.
DBEQ Decrement and branch if zero.
DBNE Decrement and branch if not zero.

E.g., DBNE B, Loop

*note: they dont affect flags

ECE 362 Microprocessor Systems and


Interfacing

3-38

ECE 362 Microprocessor Systems and


Interfacing

4-39

Searching a List (Finish if Terminator)

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

1 ; find VAR in list DAT


20,30,40,50
60,70,80,90,0

START:

#DAT

LDX

; set base address

ECE 362 Microprocessor Systems and


Interfacing

4-40

Searching a List Using a Terminator


VAR: DS.B
DAT: DC.B
DC.B

1 ; find VAR in list DAT


20,30,40,50
60,70,80,90,0

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

How to search a Lookup Table


- finding nth element in an array

Example

Translate a Celsius value to Fahrenheit

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?

ECE 362 Microprocessor Systems and


Interfacing

4-42

Table Lookup - find nth element in an array


;Translate a Celsius value to Fahrenheit (Celsius input is used as the index)
; Instead of using equation and calculation, Fahrenheit = Celsius * 9 / 5 + 32
TempTran: dc.b 32,34,36,37,39,41,43
dc.b 45,46,48,50,52,54,55
TempVal: equ 5

; Fahrenheit
; Fahrenheit

; Celsius (used as an index to the array)

ldx #TempTran ; get the starting address of array


ldab #TempVal ; get index
abx
; find address of data
ldaa 0,x
; get data
An alternative way (indexed addressing mode) to access data
ldx #TempVal
ldaa #TempTran, x
ECE 362 Microprocessor Systems and
Interfacing

3-43

ECE 362 Microprocessor Systems and


Interfacing

4-44

Laboratory 4.2 : Table lookup

Write a program that repeatedly reads the dipswitches and


writes the following values to the LEDs for the switch
inputs (using a lookup table). The left value is the switch
input and the right value is sent to the LEDs.
$0->$12
$4->$3a
$8->$61
$c->$b6

$1->$18 $2->$24 $3->$36


$5->$43 $6->$4b $7->$51
$9->$6f $a->$7a $b->$92
$d->$c3 $e->$d6 $f ->$f1

ECE 362 Microprocessor Systems and


Interfacing

3-45

Laboratory 4.3 Search in a table

Start a new project.


Write a flowchart and then write program to determine the
index of a number in a look-up table. Use the following
table.
table:
dc.b $eb,$77,$7b,$7d,$b7,$bb,$bd,$d7
dc.b $db,$dd,$e7,$ed,$7e,$be,$de,$ee
val: ds.b 1

Your program should end at the same memory location


(use a NOP instruction) whether a match is found or not.
ECE 362 Microprocessor Systems and
Interfacing

4-46

Laboratory 4.4 : Stepper Motor


Write an assembly language program to turn the stepper
motor. Start a new project.
Set bits 1 to 4 of port P DDR ($25A) to 1 (output direction)
Do not set bits 0, 5, 6 and 7; setting these bits may
produce unexpected results.
Output the following sequence to turn the motor clockwise
to port P ($258):
Bit # 7 6 5 4 3 2 1 0 |= value
x x d 0 1 0 1 d |= 0x0A
x x d 1 0 0 1 d |= 0x12
x x d 1 0 1 0 d |= 0x14
x x d 0 1 1 0 d |= 0x0C

ECE 362 Microprocessor Systems and


Interfacing

4-47

Laboratory 4.4 : Stepper Motor


Note:
1. Be sure the cable to the stepper motor is connected
before performing this exercise.
2. To control the stepper motor the jumper on J2 (lower
board) must be moved to the left side position.
3. Other bits of PORT P are dont care (either 0 or 1)

ECE 362 Microprocessor Systems and


Interfacing

4-48

Assembler Directives: xdef & xref

External references.

Use xdef to make a defined symbol visible outside the


current module.
xdef printf

Use xref to indicate that a referenced symbol is defined


outside the current module.
xref printf

ECE 362 Microprocessor Systems and


Interfacing

4-49

Laboratory 4.5 : Loop Delay, External


References

Modify the stepper motor program to place the delay loop in


a separate file.
Create a new assembly file with the .asm extension and
save it to the sources directory in your project.
Add the assembly file created in the previous step to the
sources in your project by right clicking in the project finder.
Create a variable DelayCount to hold the value for the
number of iterations of the delay loop to execute(this will
allow you to change the length of your delay loop by
adjusting the DelayCount variable)
Load the delay count in a variable (DelayCount) in the main
program. (continue in the note page)
ECE 362 Microprocessor Systems and
Interfacing

4-50

You might also like