;
 ; ------------------------------------------------------
 ;       sSubStr = Mid$(sStr, lPos, [lLen])
 ;       sSubStr = MidS(sStr, lPos, [lLen])
 ; ------------------------------------------------------
 ;
 ; Property Get MidS(sStr As String, _
 ;                   ByVal lPos As Long, _
 ;          Optional ByVal lLen As Long) As String
 ; End Property
 ;
 ; ------------------------------------------------------
;
 .686          ; 32-bit instruction set
 .model flat   ; Protected memory, 4GB limit
 .code         ; Start of code

MidS proc near

   push esi           ; preserve register
   mov esi,[esp+12]   ; copy source descriptor
   mov edx,[esp+16]   ; copy position parameter
   mov esi,[esi]      ; extract string pointer
   xor eax, eax       ; zero default return
   test esi,esi       ; validate string pointer
   jz _exitp          ; exit null string passed
   mov ecx,[esi-4]    ; extract string byte length

   dec edx            ; convert pos to zero base
  ;js _exitp          ; exit if invalid start pos
   shr ecx, 1         ; convert to unicode chr cnt
  ;jz _exitp          ; exit empty string passed
   sub ecx, edx       ; sub start pos from length
   jbe _exitp         ; start outside str bounds

   lea esi,[esi+edx*2]; calc string start address

   mov edx,[esp+20]   ; copy length parameter
   test edx,edx       ; validate optional param
   jz _alloc          ; default start pos to end
   js _exitp          ; invalid length so exit
   cmp edx, ecx       ; test specified length
   cmovb ecx,edx      ; use specified length

  _alloc:
   mov edx, 8876543h  ; api address patched on init
   push ecx           ; SysAllocStringLen len param
   push esi           ; string to copy pointer+start
   call edx           ; call SysAllocStringLen api

  _exitp:
   mov edx,[esp+24]   ; copy return descriptor
   mov [edx], eax     ; assign function result

   pop esi       ; restore pointer register
   xor eax, eax  ; clear eax tell VB all okay
   ret 20        ; cleanup (lpObj, p1, p2, p3, ret)

MidS endp   ; end of procedure
end MidS    ; end of code, indicate entry point
