'===========================================================================
' Subject: GET ONE CHARACTER FROM STRING      Date: 01-28-97 (19:59)       
'  Author: Brian Mclaughlin                   Code: ASM                    
'  Origin: comp.lang.basic.misc             Packet: ASMCODE.ABC
'===========================================================================
; MIDASC.ASM  - assemble with MASM 5.1 or compatible assembler
;             - use with QB4.x only
;             - written by Brian McLaughlin. Released to public domain.
;
; This FUNCTION accepts a string, and an integer describing which
; character in the string to examine. It returns the ASCII code of that 
; character. If the string is a null, or an illegal value is passed in CharPos% 
; (such as a zero, or a value beyond the end of the string) it returns a -1.
;
; It's best use is when stepping through a string one character at a time,
; looking for some particular character. It's much faster than ASC(MID$()).
;
; Declare it this way:   DECLARE FUNCTION MidAsc%(Test$, CharPos%)
; Use it this way:       AsciiValue% = MidAsc%(Test$, CharPos%)


.MODEL MEDIUM, BASIC
.CODE

MidAsc PROC, String:WORD, CharPos:WORD

        Mov BX, CharPos ; address of CharPos
        Mov DX, [BX]    ; DX = value of CharPos
        Or  DX, DX      ; if DX = 0 then CharPos is illegal value
        Jz  WasError    ; (an OR sets zero flag if results are zero)
        Mov BX, String  ; address of String's descriptor
        Mov CX, [BX]    ; CX = length of String
        Jcxz WasError   ; if CX = 0 then String is a null
        Cmp DX, CX      ; if DX > CX then CharPos is illegal value
        Ja  WasError    ;
        Mov BX, [BX+2]  ; BX = address of first byte of string
        Add BX, DX      ; add CharPos to address of first byte
        Dec BX          ; move back one place to correct byte
        Mov AL, [BX]    ; put the ASCII value into AL
        Xor AH, AH      ; zeroes AH, so AX = value in AL
Exit:
        Ret             ; return the value in AX
WasError:               ; come here when an error condition
        Mov AX, -1      ; put -1 in AX to flag an error
        Jmp SHORT Exit
MidAsc ENDP
       END
