flat assembler
Message board for the users of flat assembler.

Index > Windows > String Manipulation Functions

Author
Thread Post new topic Reply to topic
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
up until this moment, im still doing most of the work using windows api

i managed to code some of those API in pure asm, and i was hoping that someone can recheck them for me, and tell me if there are other instructions or ways to make those functions faster/smaller or "me" better, they are all simple string functions, all those functions are based on stosb and lodsb
* BTW, All Functions are Working

Code:
; All Inputs must be Null-Terminared Strings

proc _strcpy, lpDest, lpSource
  pusha
  mov        esi, [lpSource]
  mov        edi, [lpDest]

  .clear_dest:
     lodsb
     cmp        al, byte 0
     je         .store_data
     mov        al, 0
     stosb
     jmp        .clear_dest

  .store_data:
     lodsb
     cmp        al, byte 0
     je         .done
     stosb
     jmp        .store_data

  .done:
     mov        [edi],byte 0
     popa
     ret

endp


proc _strcat, lpDest, lpSource
  pusha
  mov        esi, [lpDest]
  mov        edx, -1

  .get_data_length:
    lodsb
    inc        edx
    cmp        al, 0
    jne        .get_data_length
    mov        esi, [lpSource]
    mov        edi, [lpDest]
    add        edi, edx

  .store_data:
    lodsb
    cmp        al, byte 0
    je         .done
    stosb
    jmp        .store_data

  .done:
    mov        [edi],byte 0
    popa
    ret

endp

proc _strlen, lpBuffer
  push edx
  push esi
  mov   esi, [lpBuffer]
  mov   edx, -1

  .start_count:
    inc     edx
    lodsb
    cmp     al, byte 0
    jne     .start_count
    mov     eax, edx
    pop     esi
    pop     edx
    ret

endp

proc _clear_str, lpBuffer
  pusha
  stdcall       _strlen, [lpBuffer]
  mov           edx, eax
  mov           edi, [lpBuffer]
  mov           al, byte 0

 .clear:
   dec  edx
   stosb
   cmp  edx, -1
   jne  .clear

 .done:
   popa
   ret
endp

proc _split, lpBuffer, Str1, Str2
  pusha
  stdcall _clear_str, [Str1]
  stdcall _clear_str, [Str2]
  mov esi,[lpBuffer]
  mov edi,[Str1]
  mov dh, byte '|'

  .store_str:
    lodsb
    cmp             al, dh
    je              .check_str
    cmp             al, byte 0
    je              .done
    stosb
    jmp             .store_str

  .check_str:
    cmp             dh, byte 0
    je              .done
    mov             edi, [Str2]
    mov             dh, byte 0
    jmp             .store_str

  .done:
    popa
    ret
endp

proc _chrpos, lpBuffer, lpChr
 push esi
 push edx
 mov  esi, [lpBuffer]
 mov  edx, 0

 .start_searching:
  inc   edx
  lodsb
  cmp   al, byte 0
  je    .done
  cmp   al, byte '|'
  jne   .start_searching

 .done:
  mov   eax, edx
  pop   edx
  pop   esi
  ret
endp
    
Post 09 Sep 2010, 00:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17248
Location: In your JS exploiting you and your system
revolution
Nameless wrote:
* BTW, All Functions are Working
Erm. _strcpy is broken.

Do you have a test program to check all the functions? I would suggest that you hold off trying to make the functions faster or smaller until you get them all working first. And I also suggest that you write a set of companion test functions to automatically check they still function properly after each change.
Post 09 Sep 2010, 00:51
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2891
Location: [RSP+8*5]
bitRAKE
Here are some small ones conforming to Win32 ABI:
Code:
strZERO:
        pop edx         ; return address!
        pop ecx         ; count
        mov al,0
        xchg edi,[esp]  ; address
        rep stosb
        pop edi
        jmp edx

strLEN:
        pop edx
        mov al,0
        or ecx,-1
        xchg edi,[esp]
        repnz scasb
        xchg eax,ecx
        not eax
        dec eax
        pop edi
        jmp edx

strCMP:
        pop edx         ; return address
        pop ecx         ; count
        pop eax         ; string one
        xchg esi,[esp]  ; string two
        xchg eax,edi
        repe cmpsb
        xchg eax,edi
        xchg eax,ecx    ; return position and flags
        pop esi
        jmp edx

strCOPY:
        pop edx         ; return address!
        pop ecx         ; count
        pop eax         ; string one
        xchg esi,[esp]  ; string two
        xchg eax,edi
        rep movsb
        xchg eax,edi
        pop esi
        jmp edx    
Just for fun. Very Happy
(Best to inline instead.)
Post 09 Sep 2010, 04:15
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17248
Location: In your JS exploiting you and your system
revolution
bitRAKE: Win32 ABI uses null terminated strings pretty much everywhere. Your code seems to need a count value.

Nameless: Are you aware that the Win32 API already provides string manipulation functions? You are aware that you are reinventing the wheel, right?
Post 09 Sep 2010, 04:22
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
revolution wrote:
Nameless: Are you aware that the Win32 API already provides string manipulation functions? You are aware that you are reinventing the wheel, right?


Where is the problem ? More free, more open source, more FASM'ous Smile
Post 09 Sep 2010, 06:03
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
@revolution:
sorry, fixed Very Happy
Code:
proc _strcpy, lpDest, lpSource
  pusha
  stdcall    _clear_str, [lpDest]
  mov        esi, [lpSource]
  mov        edi, [lpDest]

  .store_data: 
     lodsb 
     cmp        al, byte 0 
     je         .done 
     stosb 
     jmp        .store_data 

  .done: 
     mov        [edi],byte 0
     popa 
     ret
endp
    

also if u read the first lines of my topic, im already using those APIs (thats how i got the names), but the thing is, i wanna write my owen in pure ASM

@bitRAKE: y is it best to use inline? *i see alot of new instructions in ur code (to me), time to google Very Happy*
Post 09 Sep 2010, 06:20
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2891
Location: [RSP+8*5]
bitRAKE
Nameless wrote:
@bitRAKE: y is it best to use inline?
These routines are very few bytes. The minimal overhead of passing parameters on the stack: 1 byte for each value in register and 5 bytes for the call. The string instructions are only a single byte and the REP prefix is one byte. Wise register choices can prepare for the REP string operation almost eliminating the need for additional shuffling of values.

Looking at the downside: faster algorithms can be developed with a bias towards strings of a particular average size; different processors have very different performance characteristics for the string instructions.

_________________
¯\(°_o)/¯ unlicense.org
Post 09 Sep 2010, 07:23
View user's profile Send private message Visit poster's website Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Here's two fasm links,

vid's fasmlib has a basic set of safer string functions. See inside fasmlib-0.8.0/src/str.

aaro's dynamic library, old stuff which probably require modifications today. Check inside StrLib.asm

plus a tiny one Razz
Code:
; strlen()
; in:esi address, out:ecx length

strlen:     push esi
            or ecx, -1
@@:         inc ecx
            lodsb
            test al, al
            jnz @B
            pop esi
            ret
    
Post 09 Sep 2010, 11:39
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Code:
  .store_data: 
     lodsb 
     stosb 
     test        al, al 
     jnz         .store_data

     popa 
     ret     
(Not a bug fix, just simplification)
Post 09 Sep 2010, 14:25
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
@Picnic: thanks alot for this code, i got some ideas out of it. ur missing my point here, im trying to make my owen, i wont get better by using other's code Very Happy (at least thats what i think)

@LocoDelAssembly: kewl Very Happy, time to learn about test instruction Very Happy, thanks alot Very Happy
Post 09 Sep 2010, 16:57
View user's profile Send private message Reply with quote
mindcooler



Joined: 01 Dec 2009
Posts: 423
Location: Västerås, Sweden
mindcooler
Code:
strlen: ;>esi,<eax
        xor     eax,eax
 .loop: cmp     byte [esi+eax],0
        je      .out
        inc     eax
        jmp     .loop
 .out:  retn    

_________________
This is a block of text that can be added to posts you make.
Post 10 Sep 2010, 06:17
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
awsome
Code:
cmp     byte [esi+eax],0
    

this keeps getting better and better with each post
YAYYYYYYYYYYYYYYYYYYYYY!
Post 10 Sep 2010, 11:18
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1287
Location: behind the arc
Picnic
Nameless wrote:
i wont get better by using other's code Very Happy (at least thats what i think)


Well, i think that often studying other people's code/techniques has benefits.
But maybe is not the right way for all.
Post 13 Sep 2010, 14:45
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless
ofc stying other people's code and techniques is very good, but using it blindly isn't a right way at all
Post 13 Sep 2010, 17:02
View user's profile Send private message Reply with quote
semiono



Joined: 31 Aug 2007
Posts: 176
Location: section '.code' executable
semiono
post 1,2 This all procedures depend on each other? Is it available make it not depend and separatly?

I've grab this code so as lesson for me Smile Thanks!
Post 15 Sep 2010, 13:16
View user's profile Send private message ICQ Number 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-2020, Tomasz Grysztar.

Powered by rwasa.