flat assembler
Message board for the users of flat assembler.

Index > Windows > Mid String

Author
Thread Post new topic Reply to topic
spacebuddy



Joined: 26 Aug 2009
Posts: 5
spacebuddy 26 Aug 2009, 04:27
I am attempting to write a mid string proc but it is not working 100% and I
can't find the bug. Can anyone help. Here is the code.

proc MidStr,source,aStart,aCount
sub [aStart],1
mov esi,[source]
add esi,[aStart]
xor ecx,ecx
myloop:
mov al,[esi]
mov [edx+ecx],al
inc esi
inc ecx
cmp ecx,[aCount]
jne myloop
ret
endp



ty Very Happy
Post 26 Aug 2009, 04:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20459
Location: In your JS exploiting you and your system
revolution 26 Aug 2009, 04:35
You are not specifying a destination. The value in edx has not been initialised.
Post 26 Aug 2009, 04:35
View user's profile Send private message Visit poster's website Reply with quote
Pirata Derek



Joined: 31 Oct 2008
Posts: 259
Location: Italy
Pirata Derek 26 Aug 2009, 08:06
I don't know how you want to do, but try this:
Code:
; Copy a piece of a string to another place

; INPUT Parameters:
;        PUSHD source string (offset)
;        PUSHD the bytes to skip from start (bytes)
;        PUSHD the bytes to copy (bytes)
;        PUSHD destination string (offset)

proc MidString source,start,count,destination
     mov ecx,[count]
     jecxz .exit ; if zero then exit
     mov esi,[source]
     add esi,[start]
     mov edi,[destination]
     cld
     rep movsb
     xor al,al ; to terminate the second string
     stosb
     .exit: ret
endp    

This procedure is more simple, smaller and faster Cool


Last edited by Pirata Derek on 27 Aug 2009, 14:45; edited 1 time in total
Post 26 Aug 2009, 08:06
View user's profile Send private message Send e-mail Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 26 Aug 2009, 10:52
from FASMLIB:

str.sub
Code:
;; @name str.sub
;; @desc
;;    Extracts substring from another string.
;; @arg dest
;;    Pointer to destination string buffer.
;; @arg destlen
;;    Size of destination string buffer.
;; @arg src
;;    Pointer to source string.
;; @arg srclen
;;    Size of source string buffer.
;;    You can set this to -1 (FFFFFFFFh) if string is quaranteed to be properly zero-terminated.
;; @arg index
;;    Position in source string from where we start extracting.
;;    Position 0 corresponds to first character, included.
;; @arg len
;;    Length of string we want to extract.
;; @ret
;;    CF set on error, otherwise
;;    EAX = dest, and
;;    OF set if string overlapped and less than len characters were copied
;; @err ERR_OUT_OF_RANGE
;;    Position is outside of destination string, pos >= strlen(dest).
;;    You are trying to extract string from behind end of source string.
;; @err ERR_BUFFER_OVERFLOW
;;    Destination buffer is not big enough.
;;    Only first destlen bytes of extracted string were written.
;;    String in destination buffer is NOT zero terminated.
;; @err ERR_ZERO_SIZE
;;    destlen=0 or srclen=0.
;; @note If source string doesn't contain enough characters from given index,
;;       string shorter than len is copied and procedure returns with OF set.
;; @note If len=0, destination buffer is filled with empty string.
proc str.sub dest, destlen, src, srclen, index, len
 push    ecx esi
     pushf
       btc     dword [esp], 11 ;clear OF
   cld

     ;error if destlen=0
 cmp     [destlen], 0
        je      .error_destlen_zero

     ;error if srclen=0
  cmp     [srclen], 0
 je      .error_srclen_zero

      ;if len is zero, we just fill dest with empty string
        cmp     [len], 0
    je      .len_is_zero

    ;ECX = length of source string
      libcall str.len, [src], [srclen]
    jc      .rc
 mov     ecx, eax

        ;ERR_OUT_OF_RANGE if index >= length of source string
    cmp     [index], ecx
        jae     .error_index_out_of_range

       ;ECX = required length for the new string = min(index+len, str.len(string)) - index
 mov     eax, [index]
        add     eax, [len]
  cmp     eax, ecx
    jbe     @f
  mov     eax, ecx
    bts     dword [esp], 11 ;set OF
@@:
  sub     eax, [index]
        mov     ecx, eax

        ;ECX = length in dest buffer = min(ECX, destlen)
    mov     eax, [destlen]
      cmp     ecx, eax
    cmova   ecx, eax

        ;get position in source string from where to copy
   mov     esi, [src]
  add     esi, [index]

    ;copy actual part of string that should be copied
   libcall mem.copy, [dest], esi, ecx
  jc      .rc

     ;error if there is no place for ending zero, eg. if ECX = destlen
   cmp     ecx, [destlen]
      je      .error_dest_buffer_full

 ;otherwise add ending zero
  add     ecx, [dest]
 mov     byte [ecx], 0

   ;return dest
.done:      mov     eax, [dest]

.rnc:    btc     dword [esp], 1 ;clear CF
.r:     popf
        pop     esi ecx
     ret
.rc: bts     dword [esp], 1 ;set CF
      jmp     .r

      ;length of desired string is zero, just fill dest with empty string
.len_is_zero:
    mov     eax, [dest]
 mov     byte [eax], 0
       jmp     .done

.error_dest_buffer_full:
   mov     eax, ERR_BUFFER_FULL
        jmp     .rc

.error_destlen_zero:
 mov     eax, ERR_ZERO_SIZE
  jmp     .rc

.error_srclen_zero:
  mov     eax, ERR_ZERO_SIZE
  jmp     .rc

.error_index_out_of_range:
   mov     eax, ERR_OUT_OF_RANGE
       jmp     .rc

endp    
Post 26 Aug 2009, 10:52
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
spacebuddy



Joined: 26 Aug 2009
Posts: 5
spacebuddy 26 Aug 2009, 17:03
Thanks all, got it working Laughing

one more question, how do I use the fasmlib in my program?
Post 26 Aug 2009, 17:03
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 26 Aug 2009, 17:06
Have you seen the website for it?
http://fasmlib.x86asm.net/

fasmlib website wrote:

This package contains:
- complete sources
- full documentation (CHM, HTML)
- tutorial (FASM)
- binaries (LIB, ELF, MS-COFF, DLL)
- includes for FASM, MASM, NASM/YASM
- examples for FASM, MASM, NASM/YASM

I'm sure there should be enough there to help you learn how to use it.

_________________
----> * <---- My star, won HERE
Post 26 Aug 2009, 17:06
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 26 Aug 2009, 17:17
Should be a MAIN thread not WINDOWS.

If your trying to emulate the VB Mid than the extra destination parameter kind of throws it off. You could allocate memory for the new string in your procedure and return the address.

Or you could just return the pointer in your src string to the new mid'd location.
Code:
;;Equivalent to MyString = Mid(MyString, Start, Count)
proc Mid source,start,count
   MOV eax,[source]
   ADD eax,[start]
   MOV ecx,[count]
   MOV byte[eax+ecx],0
endp
    
Post 26 Aug 2009, 17:17
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.