;===============================================================================
;
; FindStringInTable
;
; Find if a string in a table
;
; in a0 Beginning of the table
; a1 End of the table
; a2 String to find
; d0 Size of an element
; fp frame pointer
;
; out a0 Point to the entry if it exists, else NULL
;
; destroy std
;
;===============================================================================
FindStringInTable:
movem.l a2-a3,-(sp)
\Loop: cmpa.l a0,a1
beq.s \NotFound ; No entry found in the table
movea.l a1,a3
suba.l a0,a3 ; Size of the table
move.l a3,d1
divu.w d0,d1 ; Number of elements
lsr.w #1,d1 ; /2
mulu.w d0,d1 ; Offset of the middle
lea 0(a0,d1.w),a3 ; Entry in the middle
bsr \CheckEntry
bne.s \Retry
lea 0(a0,d1.w),a0 ; Success
\End: movem.l (sp)+,a2-a3
rts
\Retry: bcs.s \Greater ; Else strip interval
lea -Instruction.sizeof(a0,d1.w),a1 ; Middle - 1
bra.s \Loop
\Greater: lea Instruction.sizeof(a0,d1.w),a0 ; Middle + 1
bra.s \Loop
;---------------------------------------------------------------
; No match found in the table
;---------------------------------------------------------------
\NotFound:
suba.l a0,a0
bra.s \End
;---------------------------------------------------------------
; Check if strings match. Set the flags according to the comparison
;---------------------------------------------------------------
\CheckEntry:
movea.l 4(sp),a2 ; Restore org a2
\CheckLoop:
move.b (a3)+,d2
bne.s \Not0 ; At the end of the string, we must check that the next char is valid
tst.b (a2) ; All these cmp will set Z-flag
beq.s \Rts ; EOF
cmpi.b #EOL,(a2)
beq.s \Rts
cmpi.b #ASM_COMMENT_CHAR,(a2)
beq.s \Rts
cmpi.b #SPACE,(a2)
beq.s \Rts
cmpi.b #HTAB,(a2)
beq.s \Rts
cmpi.b #'.',(a2) ; Size
beq.s \Rts ; Else the next comparison will set the flags
\Not0: cmp.b (a2)+,d2
beq.s \CheckLoop ; Chars match, check next
\Rts: rts