Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Introduction to Microcontrollers. Architecture, Programming, and Interfacing of the Motorola 68HC12 (G.J. Lipovski, 1999).pdf
Скачиваний:
190
Добавлен:
12.08.2013
Размер:
29.57 Mб
Скачать

4.6 Summary

107

*Get hexadecimal value

*entry: X->first character of hex number

*

exit:

A:value, X->nextcharacter after hex number

*

saved:

B,Y

 

*

 

 

 

GETHEX:

BSR

GH1

; convert ascii character to a nibble

 

LSLA

 

; move to high nibble

 

LSLA

 

 

 

LSLA

 

 

 

LSLA

 

 

 

PSHA

 

; save on stack

 

BSR

GH1

; convert ascii character to a nibble

 

ORAA

1, sp+ ; pop and combine

*

RTS

 

 

 

 

 

GH1:

LDAA

1, x+ ; get next symbol

 

CMPA

#'9 '

 

 

BLS

GH2

 

 

SUBA

#7

 

GH2 :

SUBA

#' 0 '

; subtract ascii 0

 

RTS

 

 

Figure 421.Convert ASCII Hex String to a Binary Number

The reader should observe that this subroutine, PASS2, is broken into subroutines GETOPCD, GETHEX, and FINLBL. Each of these subroutines is more easily understood and debugged than a long program PASS2 that doesn't use subroutines. Each subroutine corresponds to an easily understood operation, which is described in the subroutine's header. This renders the subroutine PASS2 much easier to comprehend.

The contents of the vector OBJECT will be downloaded into the target machine and executed there. The assembler permits the programmer the ability to think and code at a higher level, not worrying about the low-level encoding of the machine code.

The reader should observe the following points from the above example. First, the two-pass assembler will determine where the labels are in the first pass. Thus, labels that are lower in the source code than the instructions that use these labels will be known in the second pass when the instruction machine code is generated. Second, these subroutines further provide many examples of techniques used to convert ASCII to hexadecimal, used to search for matching characters, and used to insert data into a vector.

4.6 Summary

In this chapter, we learned that an assembler can help you write much larger programs than you would be able to write by hand coding in machine code. Not only are the mnemonics for the instructions converted into instruction opcode bytes, but also symbolic addresses are converted into memory addresses. However, every new powerful

PROBLEMS

111

13. Write a shortest assembly-language (source code) subroutine that concatenates one null-terminated string onto the end of another null-terminated string, storing a null at the end of the expanded string. Assume that on entry, X points to the first string, Y points to the second string, and there is enough space after the second string to fit the first string into this space. This program is essentially the C procedure strcat().

14. Write a shortest assembly-language (source code) subroutine to compare at most n characters of one null-terminated string to those of another null-terminated string, similar to Figure 4.10. Assume X points to the first string, and Y points to the second string, and A contains the number n. Return carry set if and only if the strings match.

15.Write a shortest assembly-language (source code) subroutine that builds a symbol table as in Figure 4.12a but stores six-letter symbolic names and a two-byte value in each symbol table row. Upon entry to the subroutine, X points to the first of the six letters (the other letters follow in consecutive locations), and accumulator D contains the two-byte value associated with this symbol. The symbol table is stored starting at label LABELS, and the number of symbols (rows) is stored in one-byte variable SIZE.

16.Write a shortest assembly-language (source code) subroutine that searches a symbol

table as in Figure 4.12b but searches six-letter symbolic names having a two-byte value in each symbol table row. Upon entry to the subroutine, X points to the first of the six letters (the other letters follow in consecutive locations). The symbol table is stored starting at label LABELS, and the number of symbols (rows) is stored in one-byte variable SIZE. The subroutine returns with carry bit set if and only if a matching symbol is found; then Y points to the beginning of the row where the symbol is found.

17 . Write a shortest assembly-language (source code) program that finds themaximum MAX of N 4-byte signed numbers contained in array Z where N < 100. Your program should have in it the assembler directives

N

DS

1

MAX

DS

4

Z

DS.L

100

and be position independent. How would your program change if the numbers were unsigned?

18. Write an assembly-language program that finds the sum SUM of two 4-byte signed magnitude numbers NUM1 and NUM2. The result should also be in signed-magnitude form. Your program should include the assembler directives

 

ORG

$800

N

DS

1

NUM1

DS

4

NUM2

DS

4

SUM

DS

4

PROBLEMS

 

!15

 

origin 800

; put at the beginning of SRAM

*

entry SOURCE

; provide entry address toHiwave

 

 

 

LCNTR:

ds.w

1

; 8-bit location counter

SIZE:

ds.b

 

; 8-bit number of symbols

SOURCE:

dc.b "ALF DC 7A"f $d,"LBl LD LB2",$d,"LB2 AD LBl,$d

LABELS

dc.b

32

; allocate 32-byte symbol table

*

 

 

 

 

Idx

SOURCE

; get source code string address

 

bsr

FINDLABEL ; look for label

 

bvs

FOUND

; if none found, skip. Note: y -> next symbol entry

 

movb

1, x+, y+1

; if found, copy first char from source to symbol table

 

movd

2, x+,2,y+ ; then copy 2nd and3rd characters to symbol table

 

movb

#LCNTR, 1, y + ; and copy location counter into symbol table

 

add

SIZE, 1

; increase label count

FOUND:

bra

*

; wait for debugger to stop program

*

 

 

 

FINDLBL: pshx

 

; save caller's pointer on the stack

 

leax

#LABELS

; x-> first symbol table row

 

Idab

1, X+

; get a character from source file

 

Idy

+1, X

; get next two characters from source file

 

Ida

SIZE

; A = number of symbols

*

branch F2

; go to end at F2 (in case there are zero items)

 

 

 

F1:

cpb

4, x+

; compare first character in B and move to nextrow

 

bne

F2

; if mismatch, try next by going to F2

 

cpy

3, x

; compare second, third character inY

 

clra

 

; clear A andcarry indicating success

 

Idx - 4 ,x

; make x->beginning of row found

 

bra

F4

; exit after balancing stack, putting pointer in Y

F2 :

dec

a

; count down accumulatorA

 

bpl

Fl

; go to Fl to do search if more to come

 

setc

 

; set carry to indicate failure

F3:

leay

SOURCE

; make y-> beginning of row found

 

pula

 

; restore caller's pointer from the stack

 

rts

 

; return to caller

Figure 4.27. FINDLBL Subroutine with Errors

27. Correct the assembly-language program in Figure 4.28. Do not change any lines that are already correct. This shortest subroutine LIST for "SA2" prints a line of the listing at the end of PASS2, after two bytes have been put into the object code. It prints the decimal line number ( < 10),the hexadecimal object code, and the source code. OUTHEX prints a binary number n in accumulator A as two digits (the hexadecimal representation of n). OUTCH places the ASCII character in accumulator A into a line of the listing, which is pointed to by the X register.