Sie sind auf Seite 1von 62

The 68HC11 Microcontroller

Chapter 3: Data Structures and Subroutine Calls

The 68HC11 Microcontroller

Han-Way Huang
Minnesota State University, Mankato

H. Huang Transparency No.3-1

The 68HC11 Microcontroller

Examples of Data Structures


1. Strings. A sequence of characters. 2. Arrays. An ordered set of elements of the same type. 3. Stacks. A data structure with a top and bottom. Elements can be added or removed only at the top of the stack. 4. Queues. A data structure to which elements can be added at only one end (called head) and removed only from the other end (called tail). 5. Dequeues. A data structure in which elements can be added and removed at both ends. 6. Trees. A data structure in which one element is called the root and the remaining elements are partitioned into m disjoint subtrees, each of which is itself a tree. 7. Graphs. A data structure that consists of a set of nodes and a set of arcs (or edges). Each arc in a graph is specified by a pair of nodes. 8. Linked lists. A data structure that consists of linked nodes. Each node consists of two fields, an information field and a next address field. The information field holds the actual element on the list, and the next address field contains the address of the next node in the list.

Operations performed on data structures


adding and deleting elements traversing and searching a data structure etc.
H. Huang Transparency No.3-2

The 68HC11 Microcontroller

The 68HC11 Stack


low address top element . . . high address bottom

SP

A 16-bit stack pointer (SP) points to the location above the top byte of the stack. The 68HC11 CPU registers can be pushed into the stack. The top element (s) of the stack can be pulled into a CPU register. The stack grows from high addresses toward lower addresses.

Push and Pull Instructions PSHA: PSHB: PSHX: PSHY: PULA: PULB: PULX: PULY: Push A onto the stack Push B onto the stack Push X onto the stack (low order byte is pushed first) Push Y onto the stack (low order byte is pushed first) Pull A from the stack Pull B from the stack Pull X from the stack (high order byte is pulled first) Pull Y from the stack (high order byte is pulled first)
H. Huang Transparency No.3-3

The 68HC11 Microcontroller

Example 3.1 Suppose that [A] = $33, [B] = $20, [SP] = $00FF. What will be the contents of the top byte of the stack before and after the execution of PSHA? What will be the contents of the top two bytes of the stack if PSHB is executed? Solution: The contents of the stack before and after the execution of PSHA and PSHB are:
XX XX [SP] = $00FF XX $33 [SP] = $00FE $20 $33 [SP] = $00FD

original stack

after PSHA

after PSHB

H. Huang Transparency No.3-4

The 68HC11 Microcontroller

Instructions Related to the Stack Pointer (SP) [<label>] DES [<comment>] decrements the contents of the stack pointer by 1 [<label>] INS [<comment>] increments the contents of the stack pointer by 1 [<label>] LDS <opr> [<comment>] loads the contents of a memory location or an immediate value into SP [<label>] STS <opr> [<comment>] stores the contents of the stack pointer in a memory location [<label>] TSX [<comment>] places the contents of SP plus one into X [<label>] TSY [<comment>] places the contents of SP plus one into Y [<label>] TXS [<comment>] places the contents of X minus one into SP [<label>] TYS [<comment>] places the contents of Y minus one into SP Example 3.2 Write down an instruction sequence to load the top element of the stack into A and the 9th element from the top of the stack into B. Solution: TSX ; points the index register X to the top element of the stack LDAA 0,X ; places the top element of the stack in A LDAB 9,X ; places the 9th element from the top of the stack in B

H. Huang Transparency No.3-5

The 68HC11 Microcontroller

Indexable Data Structures Vectors and matrices are indexable data structures A vector is one dimensional and a matrix is two dimensional The first element of a vector is associated with index 0 Vectors and matrices can be defined by one or multiple FCB or FDB directives

Example 3.3 Write a program to search an array with N 16-bit elements that are stored starting at $10 using the key stored at $00-$01 and save the address of the first matched element at $02-$03. Save the value $FFFF (-1) if no element matches the key. Solution:
start i0 result $FFFF yes result address of array[i]

i i + 1 no

array[i] = key?

i = N - 1? yes Stop

H. Huang Transparency No.3-6

The 68HC11 Microcontroller


N notfound equ equ 10 -1 ; array count

key result

org $00 FDB xxxx RMB 2

; the key to be used for searching ; memory locations to store the address

array

org $D000 FDB $20,$40,$202,$1,$200,$10,$22,$21,$300,$101 org ldd std ldd ldx ldy cpd beq inx inx iny cpy bne jmp stx end $C000 #notfound result key #array #0 0,X found

loop

found stop

#N-1 loop stop result

; store a -1 to result and result+1 ; ; get the key ; get the starting address of the array ; initialize the loop count ; compare with the key ; found it? ; increment the pointer by 2 ; ; increment the loop count ; check the loop count

; stop the search

H. Huang Transparency No.3-7

The 68HC11 Microcontroller

Matrices can be stored in row major order or column major order a matrix element can be accessed by specifying its row and column numbers

The following matrix


1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25

mat =

can be stored in row major order


mat1 FCB FCB FCB FCB FCB 01, 02, 03, 04, 05 06, 07, 08, 09, 10 11, 12, 13, 14, 15 or 16, 17, 18, 19, 20 21, 22, 23, 24, 25

in column major order


mat2 FCB FCB FCB FCB FCB 01, 06, 11, 16, 21 02, 07, 12, 17, 22 03, 08, 13, 18, 23 04, 09, 14, 19, 24 05, 10, 15, 20, 25

H. Huang Transparency No.3-8

The 68HC11 Microcontroller

Address Calculation for a N M Matrix Elements row major order address of mat1(i, j) = (i M) + j + address of mat1(0,0)

* Suppose memory locations i and j hold the row and column indices * The following instruction sequence computes the address of mat1(i, j)
ldaa ldab mul addb adca addd xgdx i #M j #0 #mat1 ; place row index in A ; place matrix dimension M in B ; compute i M ; compute i M + j ; ; compute i M+ j + mat1(0,0) ; place the address in X

H. Huang Transparency No.3-9

The 68HC11 Microcontroller

column major order address of mat2(i, j) = (j N) + i + address of mat2(0, 0)

* Suppose memory locations i and j hold the row and column indices * The following instruction sequence computes the address of mat2(i, j) ldaa ldab mul addb adca addd xgdx j #N ; place row index in A ; place matrix dimension N in B ; compute j N i ; compute j N + i #0 ; #mat2 ; compute j N + i + mat2(0,0) ; place the address in X

H. Huang Transparency No.3-10

The 68HC11 Microcontroller

Example 3.4 Write a program to compute the sum of two 8 8 matrices. The starting addresses of these two matrices are MA and MB and that the result matrix is MC. These matrices are stored in memory in row major order. Each element is one byte. Solution: N equ org fcb .... fcb .... rmb rmb rmb rmb rmb $8 $00 .... .... 64 1 1 1 2 $C000 #N i i #N j ; dimension of the matrix

MA
MB MC i j buf disp

; matrix MA is here
; matrix MB is here ; matrix MC is here ; row index ; column index ; buffer to hold element ; memory to hold the value of i N + j

org ldaa staa out_lp dec ldab stab

; start from the last row ; initialize the index i ; start from the last column toward the 0th column

H. Huang Transparency No.3-11

The 68HC11 Microcontroller

* The following 10 instructions fetch MA(i, j) and place it at buf in_lp dec j ldaa i ; place row index in A ldab #N ; place N in B mul ; compute N i addb j ; compute N i + j adca #0 ; std disp ; save the value of N i + j addd #MA ; compute MA(0,0) + N i + j xgdx ldaa 0,X staa buf * The following 4 instructions fetch MB(i, j) into A ldd disp addd #MB xgdx ldaa 0,X adda staa buf buf ; compute MA(i, j) + MB(i, j) ; save the sum in buf
H. Huang Transparency No.3-12

The 68HC11 Microcontroller

* The following 3 instructions compute the address of MC(i, j) and leave it in X ldd disp addd #MC xgdx ldaa buf ; get the sum staa 0,X ; save the sum in MC(i,j) dec j ; decrement the column number beq in_lp ; not reach the end of a column yet? dec i ; decrement the row number beq out_lp ; is this the end of all computation end
Matrix Transpose The transpose MT of a matrix M is defined as a matrix obtained by writing the rows of M as the columns of MT. MT can be obtained by swapping the (i, j)th element with the (j, i)th element for each i and j from 0 to N-1. Example 3.4 Write a program to transpose an N N matrix.

H. Huang Transparency No.3-13

The 68HC11 Microcontroller

A breakdown of matrix
a0,0 a0,1 a1,0 a1,1 a0,n-1 upper right

lower left an-1,0

an-2,n-1 an-1,n-2 an-1,n-1

- swap each element in the upper right with an element in the lower left - row index i runs from 0 to n-2 for the upper right elements - for row i column index j runs from i+1 to n-1 - diagonal elements need not be swapped

H. Huang Transparency No.3-14

The 68HC11 Microcontroller

Start i=0

j=i+1

X = address of MAT(i, j)

Y = address of MAT(j,i) j=j+1 A = MAT (i, j) B = MAT (j, i) Store A in the memory location pointed to by Y Store B in the memory location pointed to by X

yes

j < N - 1? no

i = i + 1 yes

i < N - 2? no Stop

Figure 3.6 Flowchart for matrix transposition

H. Huang Transparency No.3-15

The 68HC11 Microcontroller

Matrix Transpose Program

N ilimit jlimit

equ equ equ


org

8 N-2 N-1

; matrix dimension ; upper limit of index i ; upper limit of index j

$00 i rmb 1 ; row index j rmb 1 ; column index ORG $D000 ; starting address of the matrix mat FCB ..... ; matrix to be transposed FCB .... ; . ; . ; ORG $C000 clr i ; initialize i to 0 row_loop ldaa i inca staa j ; initialize j to i + 1 * The following 7 instructions compute the address of mat(i,j) col_loop ldaa i ldab #N mul addb j adca #0
H. Huang Transparency No.3-16

The 68HC11 Microcontroller

addd #mat xgdx * The following 7 instructions compute the address of mat(j,i) and leave address in Y ldaa j ldab #N mul addb i adca #0 addd #mat xgdy * The following 4 instructions swap mat(i,j) with mat(j,i) ldaa 0,X ldab 0,Y staa 0,Y stab 0,X ldab j inc j ; update index j cmpb #jlimit ; j = N - 1? bne col_loop ldaa i inc i ; update index i cmpa #ilimit ; i = N - 2? bne row_loop END
H. Huang Transparency No.3-17

The 68HC11 Microcontroller

Strings Example 3.6 Write a program to append a string to the end of another string. Solution: There are two steps in this program: Step 1 Find the end of the first string. Step 2 Copy string 1. ORG $C000 LDX #string2 again LDAA 0,X BEQ copy INX BRA again copy LDY #string1 copy_loop LDAA 0,Y STAA 0,X BEQ done INX INY BRA copy_loop done NOP SWI
H. Huang Transparency No.3-18

The 68HC11 Microcontroller

string1

string2

ORG FCC FCB FCC FCB END

$D000 . 0 . 0

Example 3.7 Write a program to count the number of characters and words contained in a string. Solution: The first non-white-space character to the right of one or more spaces is the beginning of a new word. Parameters used in the program: char_cnt: wd_cnt: str_ptr: curr_char: character count word count string pointer current character

H. Huang Transparency No.3-19

The 68HC11 Microcontroller


Start

Figure 3.7 Flowchart for

character and word count program

char_cnt 0 word_cnt 0 str_ptr addr. of string_X curr_char [str_ptr] str_ptr str_ptr + 1 curr_char = NULL? no char_cntchar_cnt + 1 yes curr_char = space, tab, LF, or CR? no word_cnt word_cnt + 1 curr_char [str_ptr] str_ptr str_ptr + 1 curr_char = NULL? no char_cntchar_cnt + 1 yes no yes yes

skip white spaces

Stop

reach a new word

skip the remaining characters of the word Stop

curr_char = space, tab, LF, or CR?

Figure 3.7 Flowchart for character count and word count program

H. Huang Transparency No.3-20

The 68HC11 Microcontroller

tab sp CR LF

equ $09 ; ASCII code for horizontal tab character equ $20 ; ASCII code for SPACE equ $0D ; ASCII code for carriage return equ $0A ; ASCII code for line feed org $00 char_cnt rmb 1 wd_cnt rmb 1 ORG $D000 string_X fcc xxxxxxxxxxxxxxxx ; the string to be processed fcb 0 ; the NULL character to terminate the previous string org $C000 ldx #string_X clr char_cnt ; initialize the character count to 0 clr wd_cnt ; initialize the word count to 0 string_lp ldab 0,X ; fetch the current character beq exit ; is this a NULL character? inx ; move to the next character inc char_cnt * The following 8 instructions skip the spaces between words cmpb #sp beq string_lp ; skip the space character cmpb #tab beq string_lp ; skip the tab character
H. Huang Transparency No.3-21

The 68HC11 Microcontroller

cmpb #CR beq string_lp ; skip the carriage return character cmpb #LF beq string_lp ; skip the line feed character * A non-space character is the beginning of a new word inc wd_cnt wd_loop ldab 0,X beq exit ; null character is not included in character count inc char_cnt inx ; move the character pointer * The following 8 instructions check the end of a word cmpb #sp beq string_lp cmpb #tab beq string_lp cmpb #LF beq string_lp cmpb #CR beq string_lp * A non-space character is part of a word bra wd_loop exit swi end
H. Huang Transparency No.3-22

The 68HC11 Microcontroller

Example 3.8 Write a program to search a certain word from a given string. Solution: The flowchart of the word search program is in Figure 3.8.

Start

Skip the white spaces to look for the next word in the string

Is the current word in the string equal to the word to be searched? no no Is this the end of the string? yes Stop

yes

Figure 3.8 Flowchart of the word search program

H. Huang Transparency No.3-23

The 68HC11 Microcontroller

tab sp CR LF NULL

equ $09 ; ASCII code for horizontal tab equ $20 ; ASCII code for SPACE equ $0D ; ASCII code for carriage return equ $0A ; ASCII code for line feed equ $00 ; ASCII code for NULL org $00 search rmb 1 ; flag to indicate if the given word is in the string string_X fcc xxxxxxxxxxxxxxxx ; a string terminated by NULL fcb 0 word_X fcc yyyyyyy ; a word terminated by NULL fcb 0 org $C000 clr search ; initialize the search flag to 0 ldx #string_X ; place the string address in X loop ldab 0,X inx * The following 10 instructions skip the white spaces to search for the next word * in the string tstb beq done ; is this the end of the string? cmpb #sp ; is the current character a space? beq loop ; skip the space cmpb #tab ; is the current character a tab?
H. Huang Transparency No.3-24

The 68HC11 Microcontroller

beq loop ; skip the tab character cmpb #CR ; is this a carriage return? beq loop ; skip it cmpb #LF ; is this a line feed? beq loop ; skip it * The occurrence of the first non-white character indicates the beginning of a word, * and the comparison should be started ldy #word_X ; place the word address in Y ldaa 0,Y ; place the current character of word_X in A iny ; move the word pointer next_ch cba ; compare the character in A and B bne end_of_wd ; check the next word cmpa #NULL ; is this the end of a word? beq matched ; if yes, the word is found in the string ldaa 0,Y ; get the next character in word_X ldab 0,X ; get the next character in string_X inx iny bra next_ch ; check the next pair of characters * The following 10 instructions check to see if the end of the given word is reached end_of_wd cmpa #NULL bne next_wd ; if the not the end of the given word, then not matched cmpb #CR
H. Huang Transparency No.3-25

The 68HC11 Microcontroller

beq matched cmpb #LF beq matched cmpb #tab beq matched cmpb #sp beq matched * The following twelve instructions skip the unmatched word in the string next_wd ldab 0,X ; get the next character in the string beq done ; stop if this is the end of the string inx cmpb #CR beq jmp_loop ; the label loop is too far away to use a conditional branch cmpb #LF beq jmp_loop cmpb #tab beq jmp_loop cmpb #sp beq jmp_loop bra next_wd jmp_loop jmp loop matched ldab #1 ; set the search flag to 1 stab search ; done swi ; return to monitor (buffalo)
H. Huang Transparency No.3-26

The 68HC11 Microcontroller

Subroutines A sequence of instructions that can be called from various places in the program Allows the same operation to be performed with different parameters Simplifies the design of a complex program by using the divide-and-conquer approach

Instructions related subroutine calls


[<label>] [<label>] [<label>] BSR <rel> JSR <opr> RTS [<comment>] [<comment>] [<comment>] ; branch to subroutine ; jump to subroutine ; return from subroutine

where <rel> is the offset to the subroutine <opr> is the address of the subroutine and is specified in the DIR, EXT, or INDexed addressing mode.

H. Huang Transparency No.3-27

The 68HC11 Microcontroller

Program Structure

Subroutine Processing

Main program

Main program <call> sub_x . . .

Subroutine 1

Subroutine 2

Subroutine 3
sub_x: . . . <return>

Subroutine 1.1

Subroutine 2.1

Subroutine 3.1

Subroutine

Figure 3.10 Subroutine processing

Subroutine 2.1.1

Subroutine 2.1.2

Figure 3.9 A structured program

H. Huang Transparency No.3-28

The 68HC11 Microcontroller

Issues in Subroutine Calls 1. Parameter passing - use registers - use the stack - use global memory 2. Returning results - use registers - use the stack (caller created a hole in which the result will be placed) - use global memory 3. Local variables allocation - allocated by the callee - use as many DES instructions when no more than 5 bytes are needed - use the following instruction sequence to allocate more than 5 bytes. TSX XGDX SUBD #N ; allocate N bytes XGDX TXS ; move the stack pointer up by N bytes

H. Huang Transparency No.3-29

The 68HC11 Microcontroller

Issues in Subroutine Calls (continued) 4. Local variables deallocation use as many INS instructions as needed when no more than 5 bytes are to be deallocated use the following instruction sequence when more than 5 bytes are deallocated TSX XGDX ADDD XGDX TXS Stack Frame The region in the stack that holds incoming parameters, the subroutine return address, local variables, and saved registers is referred to as stack frame.

#N

; deallocate N bytes

; move down the stack pointer by N bytes

H. Huang Transparency No.3-30

The 68HC11 Microcontroller

Low address Local variables Saved registers Previous frame pointer Return address Incoming parameters High address Figure 3.12 A stack frame

SP

Frame pointer

SP Y (or X) Local variables Saved registers Return address Incoming parameters Figure 3.13 Example of 68HC11 stack frame

H. Huang Transparency No.3-31

The 68HC11 Microcontroller

Example 3.9 Draw the stack frame for the following program segment when the 9th instruction of the subroutine subx is executed. ldaa psha ldx pshx bsr ... Subx: (1) (2) (3) (4) (5) (6) (7) (8) (9) #N #mat
SP

Solution:

subx
Y_H

6 bytes for local variables upper byte of Y lower byte of Y upper byte of X lower byte of X

pshb psha pshx pshy tsx xgdx subd xgdx txs ...

Y_L X_H X_L A B ret_addr_H

upper byte of return address lower byte of return address upper byte of MAT lower byte of MAT

#6

ret_addr_L MAT _H MAT _L N

Figure 3.14 Stack frame for Example 3.9

H. Huang Transparency No.3-32

The 68HC11 Microcontroller

Using Registers to Pass Parameters Example 3.10 Write a subroutine to compute the average of an array with N 8-bit elements and an instruction sequence to call the subroutine. The array starts at ARRAY. Use registers to pass parameters and return the average in B. Solution: The instruction sequence to call this subroutine is:
SP

LDX #ARRAY LDAA #N BSR average .... The subroutine saves array count A in the stack and also push a 0 into the stack so that 0 and N can be loaded into X when computing the average.

0 N ret_addr_H ret_addr_L

Figure 3.15 Stack frame of example 3.10

H. Huang Transparency No.3-33

The 68HC11 Microcontroller

Start

yes N = 0? no yes N = 1? no i=0 sum = 0


average = array[0]

i=i+1 no

sum = sum + array[i]

i = N - 1? yes average = sum / N Stop

Figure 3.16 Logic flow for computing array average

H. Huang Transparency No.3-34

The 68HC11 Microcontroller

* The following subroutine computes the average of an array and returns it in B average psha ; save the array count clra psha ; clear the top byte of the stack to 0 tsy ; Y points to the top byte of the stack ldab 1,Y ; place the array count in B cmpb #1 ; check the array count blo exit ; is the array empty? bhi do ; does the array have more than one element? ldab 0,X ; get the single element and return bra exit ; do xgdy ; place N in Y and use Y as the loop count clra ; use D as the sum and initialize it to 0 clrb ; again addb 0,X ; add an element to the sum adca #0 ; add the carry to the upper 8 bits inx ; move the array pointer dey ; decrement the loop bne again ; is the end of the loop? tsy ; point Y to the top of the stack ldx 0,Y ; place N in X

H. Huang Transparency No.3-35

The 68HC11 Microcontroller

idiv xgdx * exit pula pula rts

; compute the average of the array ; exchange X and D so that D contains the quotient ; in which A contains 0 and B contains the quotient ; restore registers ;

H. Huang Transparency No.3-36

The 68HC11 Microcontroller

Using the Stack to Pass Parameters Example 3.11 Write a subroutine to find the largest element of an array and an instruction sequence to call this subroutine. The following parameters are passed to this subroutine in the stack: - array: the starting address of the given array - arcnt: the array count - amax: address of the memory location to hold the maximum element of the array Solution: The instruction sequence that calls this subroutine is: ldx pshx ldaa psha ldx pshx bsr tsx ldab abx txs #array #arcnt #armax max #5 ; call the subroutine ; clean up the stack ; ; ;
H. Huang Transparency No.3-37

The 68HC11 Microcontroller

Start yes

arcnt < 1? no arcnt = 1? no armax arr[0] i1

Return

yes

Return array[0]

armax >= arr[i]? ii+1 yes

no armax arr[i]

no

i = arcnt - 1? yes Return armax

Figure 3.17 Logic flow of the array max subroutine

H. Huang Transparency No.3-38

The 68HC11 Microcontroller

SP Y A B Y_H Y_L X_H X_L ret_add_H ret_add_L Y+8 armax_H armax_L Y+10 Y+11 arcnt array_H array_L Each slot of the stack is one byte. T he suffix _H specifies the upper byte of a parameter. T he suffix _L specifies the lower byte of a parameter. Figure 3.18 Stack frame for the array max subroutine

H. Huang Transparency No.3-39

The 68HC11 Microcontroller

array arcnt armax

equ equ equ

11 10 8

; array base address offset from the top of the stack ; array count offset from the top of the stack ; array max address offset from the top of the stack

max

pshx pshy pshb psha tsy ldx ldab ldaa cmpb blo bhi bra start inx decb again cmpa bge ldaa noswap inx

array,Y arcnt,Y 0,X #1 exit start done

0,X noswap 0,X

; save al registers ; ; ; ; point Y to the top of the stack ; load the base address into X ; load the array count into B ; assign the first element as the temporary array max ; check array count ; return if the array is empty ; look for the array max if the array count is larger than 1 ; the array has only one element ; set X to point to the second element of the array ; loop limit is arcnt - 1 ; compare the next element with the array max ; should array max be updated? ; update the array max ; move to the next element

H. Huang Transparency No.3-40

The 68HC11 Microcontroller

done exit

decb bne again ldx armax,Y staa 0,X pula pulb puly pulx rts

; decrement the loop count ; is it done? ; get the address for the array max ; save the array max ; restore registers ; ; ;

H. Huang Transparency No.3-41

The 68HC11 Microcontroller

Example 3.12 Write a program to compute the greatest common divisor (gcd) of two 16-bit integers. The caller pushes these two integers onto the stack and this subroutine returns the gcd in double accumulator D. Solution: Let these two integers be n1 and n2. Assume n1 n2. If not, swap them. Algorithm: Step 1 If n1 > n2 then swap n1 and n2. Step 2 i = 2, gcd = 1. Step 3 If (n1 = 1) or (n2 = 1) return. Step 4 If both n1 and n2 can be divided by i then gcd i. Step 5 If i = n1 then stop. Otherwise, i i + 1. Go to step 4.

H. Huang Transparency No.3-42

The 68HC11 Microcontroller

Stack frame of gcd subroutine

Instruction sequence to call find_gcd LDX <operand n1> PSHX LDX <operand n2> PSHX JSR find_gcd INS INS INS INS

SP Y Y+2 gcd i Y ret_addr Y+8 Y+10 n1 n2

Figure 3.19 Stack frame for gcd program

H. Huang Transparency No.3-43

The 68HC11 Microcontroller

n1_dis n2_dis gcd_dis i_dis

EQU EQU EQU EQU

8 10 0 2

; offset of n1 from the top of the stack ; offset of n2 from the top of the stack ; offset of gcd from the top of the stack ; offset of i from the top of the stack

Find_gcd PSHY TSY DES DES DES DES LDX STX LDD CPD BEQ LDD CPD BEQ CPD BLS LDX

#1 gcd_dis,Y n2_dis,Y #1 done n1_dis,Y #1 done n2_dis,Y start n2_dis,Y

; if n2 = 1 then gcd = 1 ; ; ; if n1 = 1 then gcd = 1 ; ; ; if n1 is smaller then start to compute gcd ; n1 is larger than n2, so swap them
H. Huang Transparency No.3-44

The 68HC11 Microcontroller

start again

next_i

STD STX LDX STX LDD IDIV CPD BNE LDD LDX IDIV CPD BNE LDD STD LDX CPX BEQ INX STX JMP

n2_dis,Y n1_dis,Y #2 ; initialize i to 2 i_dis,Y ; n1_dis,Y #0 ; can i divide n1? next_i ; n2_dis,Y i_dis,Y ; can i divide n2? ; #0 ; next_i i_dis,Y ; use current i as gcd_dis,Y ; the temporary gcd i_dis,Y ; have we done with n1_dis,Y ; all the test yet? done i_dis,Y again

done

INS INS INS INS RTS

H. Huang Transparency No.3-45

The 68HC11 Microcontroller

Example 3.13 Write a subroutine that can divide a 32-bit number into another 32-bit number. Solution: Use repeated subtraction method to implement the division. Assume we have the hardware shown in Figure 3.0.
msb C R 32-bit write R Q 32-bit lsb

shift left

ALU Controller

P 32-bit Figure 3.20 Hardware for 32-bit by 32-bit division

H. Huang Transparency No.3-46

set lsb

The 68HC11 Microcontroller

Algorithm of Division Using Repeated Subtraction Step 1 Shift the register pair (R, Q) one bit to the left. Step 2 Subtract register P from register R, put the result back to R if the difference is nonnegative. Step 3 If the result in step 2 is negative, set the least significant bit of Q to 0. Otherwise, set it to 1. Step 4 Repeat Step 1 to 4 for 31 times. The caller allocates 8 bytes in the stack to hold the remainder and quotient. Both the dividend and the divisor are pushed into the stack. The subroutine allocates 13 bytes for local variables. The calling instruction sequence is TSX XGDX SUBD #8 XGDX TXS LDX divsor ; push divisor PSHX ; LDX didend ; push dividend PSHX ; JSR div32
H. Huang Transparency No.3-47

The 68HC11 Microcontroller

buf R Q divisor dividend i local

EQU EQU EQU EQU EQU EQU EQU

0 5 9 21 25 4 13

; space used as a buffer ; offset of register R from the top of the stack ; offset of register Q from the top of the stack ; offset of divisor from the top of the stack ; offset of dividend from the top of the stack ; offset of variable i from the top of the stack ; number of bytes of local variables

div32

PSHA PSHB PSHX PSHY TSX XGDX SUBD XGDX TXS TSY LDD STD STD LDD STD LDD

; allocate local variables ; #local ; ; ; ; Y points the top byte of the stack #0 ; initialize R to 0 R,Y ; R+2,Y ; dividend,Y ; transfer dividend to Q Q,Y ; dividend+2,Y ;
H. Huang Transparency No.3-48

The 68HC11 Microcontroller

Stack frame for the div32 subroutine

SP Y Y+4 Y+5 Y+9 buf i R Q Y X B A ret_addr Y+21 Y+25 Y+29 Y+33 divisor dividend quo rem

Figure 3.21 Stack frame of 32-bit by 32-bit division subroutine

H. Huang Transparency No.3-49

The 68HC11 Microcontroller

loop

STD LDAA STAA LSL ROL ROL ROL ROL ROL ROL ROL LDD SUBD STD LDAA SBCA STAA LDAA SBCA BCS STAA LDD STD

Q+2,Y #32 i,Y Q+3,Y Q+2,Y Q+1,Y Q,Y R+3,Y R+2,Y R+1,Y R,Y R+2,Y divisor+2,Y buf+2,Y R+1,Y divisor+1,Y buf+1,Y R,Y divisor,Y smaller R,Y buf+2,Y R+2,Y

; transfer dividend to Q ; initialize loop count to 32 ; ; shift register pair (R, Q) to the left one place ; ; ; ; ; ; ; ; subtract the divisor P from R ; ; ; ; ; ; ; ; is [R] divisor < 0? ; store the difference back to R when the difference is positive ; ;
H. Huang Transparency No.3-50

The 68HC11 Microcontroller

LDAA STAA LDAA ORAA STAA BRA smaller LDAA ANDA STAA looptest DEC BNE LDD STD LDD STD LDD STD LDD STD TSX XGDX ADDD

buf+1,Y R+1,Y Q+3,Y #01 Q+3,Y looptest Q+3,Y #$FE Q+3,Y i,Y loop R,Y rem,Y R+2,Y rem+2,Y Q,Y quo,Y Q+2,Y quo+2,Y

; set the lsb of Q to 1 ;

; set the lsb of Q to 0 ; ;

; return the remainder in the hole in the stack ; ; ; ; return the quotient in the hole in the stack ; ; ;

#local

; deallocate local variables


H. Huang Transparency No.3-51

The 68HC11 Microcontroller

XGDX TXS PULY PULX PULB PULA RTS

H. Huang Transparency No.3-52

The 68HC11 Microcontroller

Example 3.14 Write a subroutine to sort an array of 8-bit numbers using the bubble sort method, and also write an instruction sequence to call the subroutine. The starting address of the array and is arr_base, and the array count is N. Use the stack to pass parameters to this routine. Solution: Up to N 1 (labeled as 0th to (N 2)th) iterations of comparisons and swaps are performed: 1. N - i 1 comparisons are performed in iteration i. 2. Set an in-order flag to 1 at the beginning of each iteration. 3. Start from array element with index 0 to index N i 2, compare each element with its adjacent element and swap them if they are not in the right order (ascending or descending). 4. Clear the in-order flag whenever a swap is made. 5. Check the in-order flag at the end of each iteration. If the flag is 1, then stop. Otherwise, continue. Four local variables are used: 1. 2. 3. 4. iteration: keeps track of the numbers of rounds remained to be performed inner: keeps track of the number of comparisons remained to be performed in a round in_order: indicates if the given array is already in sorted order buf: buffer for swapping elements

H. Huang Transparency No.3-53

The 68HC11 Microcontroller


Start iteration N-1

in_order 1 iteration inner i 0


SP

array[i] > array[i+1]? yes swap array[i] & array[i+1] in_order 0

no

Y Y+1 Y+2 Y+3

iteration inner in_order buf X Y

inner i no

inner - 1 i+1

A B

inner = 0? yes in_order = 1? no iteration iteration - 1 yes

ret_addr Y+12 Y+13 N arr_base

Figure 3.23 Stack frame for bubble sort

no

iteration = 0? yes Stop Figure 3.22 Logic flow of bubble sort

H. Huang Transparency No.3-54

The 68HC11 Microcontroller

arr arcnt buf in_order inner iteration true false

EQU EQU EQU EQU EQU EQU EQU EQU

13 12 3 2 1 0 1 0

; offset of array base from the top of the stack ; offset of array count from the top of the stack ; offset of buf from the top of the stack

bubble

PSHB PSHA PSHY PSHX DES DES DES DES TSY LDAA arcnt,Y DECA STAA iteration,Y

; allocate space for local variables ; ; ; ; set Y to point to the top byte of the stack ; compute the number of iterations that need to be performed ;

H. Huang Transparency No.3-55

The 68HC11 Microcontroller

ploop

LDAA #true ; set in-order flag to true STAA in_order,Y ; LDX arr,Y ; place the array base address in X LDAA iteration,Y STAA inner,Y ; initialize inner loop count cloop LDAA 0,X ; get one element CMPA 1,X ; compare it with the next element BLE looptest * the following 5 instructions swap the two adjacent elements STAA buf,Y LDAA 1,X STAA 0,X LDAA buf,Y STAA 1,X LDAA #false ; reset the in_order flag STAA in_order,Y ; looptest INX ; move the array pointer DEC inner,Y BNE cloop TST in_order,Y BNE done DEC iteration,Y BNE ploop
H. Huang Transparency No.3-56

The 68HC11 Microcontroller

* the following four instructions deallocate space allocated to local variables done INS INS INS INS PULX PULY PULA PULB RTS

H. Huang Transparency No.3-57

The 68HC11 Microcontroller

Buffalo Input and Output Routines The Buffalo monitor in EVB and CMD11A8 provides a set of I/O routines. These I/O routines are located in the Buffalo ROM. A jump table is implemented so that the user program can call these routines by jumping to the desired entry.

Buffalo I/O Routines


UPCASE WCHK DECHK INIT INPUT OUTPUT OUTLHLF OUTRHLF OUTA OUT1BYT converts the character in accumulator A to uppercase tests the character in A and returns with Z bit set if character is white space (space, comma, tab) tests the character in A and return with Z bit set if character is delimiter (carriage return or whitespace) initializes I/O device reads I/O device writes I/O device converts left nibble of A to ASCII and outputs to terminal port converts right nibble of A to ASCII and outputs to terminal port outputs the ASCII character in A converts the binary byte at the address in index register X to two ASCII characters and outputs them; returns address in index register X pointing to next byte
H. Huang Transparency No.3-58

The 68HC11 Microcontroller

OUT1BSP

converts the binary byte at the address in index register X to two ASCII characters and outputs them followed by a space; returns address in index register X pointing to next byte OUT2BSP converts two consecutive bytes starting at address in index register X to four ASCII characters and outputs the characters followed by a space; returns address in index register X pointing to next byte OUTCRLF outputs ASCII carriage return followed by a line feed OUTSTRG outputs a string of ASCII bytes pointed to by the address in index register X until character is an end-of-transmission ($04) OUTSTRG0 same as OUTSTRG, except that leading carriage returns and line feeds are skipped INCHAR inputs ASCII character to A and echoes back; this routine loops until character is actually received

H. Huang Transparency No.3-59

The 68HC11 Microcontroller

Table 3.4 The EVB/EVBU I/O routine jump table Address Instruction

$FFA0 $FFA3 $FFA6 $FFA9 $FFAC $FFAF $FFB2 $FFB5 $FFB8 $FFBB $FFBE $FFC1 $FFC4 $FFC7 $FFCA $FFCD

JMP UPCASE JMP WCHEK JMP DCHEK JMP INIT JMP INPUT JMP OUTPUT JMP OUTLHLF JMP OUTRHLF JMP OUTA JMP OUT1BYT JMP OUT1BSP JMP OUT2BSP JMP OUTCRLF JMP OUTSTRG JMP OUTSTRG0 JMP INCHAR

H. Huang Transparency No.3-60

The 68HC11 Microcontroller

Calling the EVB I/O Routines


Execute a JSR instruction to call an EVB I/O routine: To output the character in accumulator A

JSR $FFB8
To output a string pointed by index register X JSR $FFC7

A better approach is to use assembler directives to make the I/O routine call more readable:
outa outstrg equ equ JSR . . . JSR $FFB8 $FFC7 outa

outstrg

H. Huang Transparency No.3-61

The 68HC11 Microcontroller

Example 3.16 Write a subroutine to input a string from the keyboard and save the string in a buffer pointed by X. The input string is terminated by the carriage-return character. Solution:
getchar CR EOT EQU EQU EQU $FFCD $0D $04 getchar #CR done 0,X get_string #EOT 0,X ; use mnemonic to represent hex address

get_string JSR CMPA BEQ STAA INX BRA done LDAA STAA RTS

H. Huang Transparency No.3-62

Das könnte Ihnen auch gefallen