;
 ; -------------------------------------------------
 ;      sSubStr = RightS(sStr, lPosR, [lLen])
 ; -------------------------------------------------
 ;
 ; Property Get RightS(sStr As String, _
 ;               ByVal lPosR 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

RightS proc near

   push esi          ; preserve registers
   push ebx

   mov esi,[esp+16]  ; copy source descriptor
   mov ecx,[esp+20]  ; 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 ebx, ecx      ; create copy of start pos
   mov edx,[esi-4]   ; extract string byte len
   shl ebx, 1        ; convert to byte position
   jbe _exitp        ; invalid start pos (jcz)
   sub edx, ebx      ; sub start pos from length
   js _exitp         ; start outside str bounds

   mov ebx,[esp+24]  ; extract length parameter
   add esi, edx      ; calc read start position

   test ebx,ebx      ; test optional len param
   jz _alloc         ; default start pos to end
   js _exitp         ; invalid length so exit
   cmp ebx, ecx      ; test specified length
   cmovb ecx,ebx     ; 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+28]  ; copy return descriptor
   mov [edx], eax    ; assign function result

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

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