3D1-Microprocessor Systems 1
Lecture 29: Review of 68k directives, executable statements and a Subroutine for ASCII to Binary Conversion
In this lecture, we look at the implementation of a search algorithm and a program that converts ascii numbers to binary.
Learning Outcomes:
On completion of this lecture, you will be able
-
Explain the use of labels in programs;
-
Search defined patterns from a nul-terminated ascii string;
-
Convert a decimal number from its ascii representation to binary.
-
Explain the idea and the use of labels in Assembly Language Programs.
A
label is a symbolic name that identifies a particular line in an assembly language program. In 68k assembly language programs, a label is any valid identifier that begins in the first column. A label permits a programmer to refer to a line without having to know its actual address.
-
Write a complete program to find the index of the first character of the first decimal number represented in a NUL-terminated character string.
All appropriate directives such as ORG, DC, DS and END must be included. The number may be preceded by a plus or a minus sign. For example, if the character string was: The number of newspapers sold was 69411.
Then the program should calculate the index to be 35, as the first character of the decimal number is the thirty fifth character in the string.
ORG $2000 input location
string DC.B 'The number of newspapers sold was+69411.',$0
ORG $3000 returns the index of 1st decimal number in string
MOVEQ #0,D0 return 0 if no decimal number is found
MOVEQ #0,D1 init. Index counter to zero
LEA string,A1 A1 points at the sting effective address
loop ADDQ #1,D1 increment index counter
MOVE.B (A1)+,D2 copy char locally in D1 and increment pointer
BEQ exit exit if no decimal number found before end of string
CMP.B #$30,D2 is char ASCII code less than $30 ?
BMI loop if yes, then look at next char
CMP.B #$3A,D2 else, is char ASCII code strictly greater than $39?
BPL loop if yes, look at next char
MOVE.L D1,D0 save current char index to D0
return SUBA #2,A1 move pointer to char right before the 1st digit
CMP.B #'+',(A1) is the number preceded by a plus sign?
BEQ reverse if yes, then decrement char index
CMP.B #'-',(A1) else, is the number preceded by a plus sign?
BEQ reverse if yes, then decrement char index
exit MOVE.B D0,RSLT save [D0] in RSLT
TRAP #0 return control to OS
reverse SUBQ #1,D0 decrement char index
BRA exit
ORG $4000 result location
RSLT DS.L 1 reserve space for result
END
29.3 Write a subroutine to read a decimal number from a NUL-terminated ASCII string to produce a signed binary longword in D0. The address the string should be passed to the subroutine in A0.
ORG $1FFC
RSLT DS.L 1
ORG $2000
num DC.B '-124',$0
ORG $4000
LEA num,A0 load number’s effective address
CLR D0 clear accumulator
MOVEQ #1,D3 D3 indicates number’s sign
BSR convert convert to decimal
TRAP #0 return control to Monitor
ORG $3000
convert CMP.B #'-',(A0) is the number negative ?
BNE loop if not goto loop
MOVEQ #-1,D3 else D3 <- -1
ADDA.L #1,A0 move to next char
loop MOVE.B (A0)+,D1 copy ascii char in D0
BEQ exit if char is 0 then exit
SUB.B #$30,D1 else subtract $30 from ascii char code
MOVE.L D0,D2 copy D0 in D2
LSL.L #3,D0 multiply contents of D0 by 8
LSL.L #1,D2 multiply contents of D2 by 2
ADD.L D2,D0 add [D0] and [D2] ( product of by 10 mult)
ADD.L D1,D0 add [D1] and [D0]
BRA loop repeat with next character
exit CMP.L #-1,D3 is the sign negative?
BNE return if not goto return
CLR D2 else clear D2
SUB.L D0,D2 take away [D0] from D2: D2 <- -[D0]
MOVE.L D2,D0 move D2 in D0
return MOVE.L D0,RSLT else save [D1] to RSLT
RTS
END
-
Explain why it is not a good idea to try to use the 68000’s MULU and MULS in the previous subroutine
MULU and MULS operate on 16-bit operands. The second operand is a data register that also used to save the product; hence only the lower word is multiplied with 16 bits from the source operand to produce a 32-bit product. Logical shift operations are preferably used to perform multiplication on longwords.
REFERENCES
-
Dr. Mike Brady, Microprocessor Systems 1, dept of Computer Science, Trinity College Dublin: http://www.tcd.ie/Engineering/Courses/BAI/JS_Subjects/3D1/.
-
Look on the Web at http://www.mee.tcd.ie/~assambc/3D1.
29-
|