***************************************************
* Address of Str1 and Str2 in stack
*
*return value in D0 (whole 32 bits):
* 0 if two strings equal
* 1 if Str1 > Str2
* -1 if Str2 > Str1
***************************************************
ORG $1000
StrCmp:
*
* move.l (a7)+,a2
move.l (a1),-(a7)
move.l (a2),-(a7)
CheckZero:
clr.l d2
cmp.b (a1),d2
bne OneZero
*
cmp.b (a2),d2
tst.b (a2,d2.w)
bne CheckChar
OneZero:
cmpm.b (a1)+,(a2)+
beq Equal
blt Small
bgt Greater
*
CheckChar:
cmpm.b (a1)+,(a2)+
beq CheckZero ;they are the same, so check the next pair
blt Small
bgt Greater
*
Equal:
move.l #0,d0
Small:
move.l #-1,d0
Greater:
move.l #1,d0
*
rts
*###########################################################################
PrintStr:
* For Demo only, changes D0,D1
move.w #-1,d1
NextChar:
add.w #1,d1
tst.b (a1,d1.w)
bne NextChar
move.b #1,d0 ;Print Str (Addr=A1, Len=D1)
trap #15
rts
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
MainProg:
* Sample main program, shows whether
* Str1 is larger than Str2
move.l #Str1,a1
bsr PrintStr ;Print first string
move.l #Str1,-(a7)
move.l #Str2,-(a7)
jsr StrCmp ;Call String Comparison function
adda #8,a7
move.l #EQ,a1
tst.l d0
beq PrintCMP
bgt isLT
move.l #ST,a1
bra PrintCMP
isLT:
move.l #LT,a1
PrintCMP:
bsr PrintStr ;Print comparison result
move.l #Str2,a1
bsr PrintStr ;Print second string
move.b #9,d0
trap #15 ;Terminate program
*#######################################################################
Str1 dc.b 'Hello',0
Str2 dc.b 'World',0
LT dc.b ' is larger than ',0
EQ dc.b ' is equal to ',0
ST dc.b ' is smaller than ',0
end MainProg
               (
geocities.com/hk/tings_garden)                   (
geocities.com/hk)