News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

MASM for FUN - #0.2 [string reversing]

Started by frktons, May 20, 2010, 08:24:50 PM

Previous topic - Next topic

hutch--

JJ,

This is why I don't care all that much in this context. The following example runs at about 2.2 gigabyte a second on my Core2 Quad. Yes there are far faster algos to get string lengths but a tiny one does the job in almost every usable instance.

Timing

1048576000
1 gigabyte in 469 milliseconds
Press any key to continue ...


Example

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    slen PROTO :DWORD

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc


    LOCAL pMem  :DWORD

    mov pMem, alloc(1024*1024*1001)          ; 1001 meg

    invoke memfill,pMem,1024*1024*1000,"xxxx"

    invoke GetTickCount
    push eax

    print str$(rv(slen,pMem)),13,10,"1 gigabyte in "

    invoke GetTickCount
    pop ecx
    sub eax, ecx

    print str$(eax)," milliseconds",13,10

    free pMem

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

slen proc pstr:DWORD

    mov ecx, [esp+4]
    mov eax, ecx
    sub eax, 1

  @@:
    add eax, 1
    cmp BYTE PTR [eax], 0
    jne @B

    sub eax, ecx

    ret 4

slen endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

lingo

#46
Old stuff again.. :wink
64      cycles for RevLingo
..sogla ym gnitset sa hcus ,sesoprup fo yteirav a sevres taht ,gnol sretcarahc 0
01 ,gnirts a si sihT
161     cycles for RevString
This is a string, 100 characters long, that serves a variety of purposes, such a
s testing my algos..
174     cycles for RevString2
..sogla ym gnitset sa hcus ,sesoprup fo yteirav a sevres taht ,gnol sretcarahc 0
01 ,gnirts a si sihT
162     cycles for RevStr
This is a string, 100 characters long, that serves a variety of purposes, such a
s testing my algos..
216     cycles for szRev
.sogla ym gnitset sa hcus ,sesoprup fo yteirav a sevres taht ,gnol sretcarahc 00
1 ,gnirts a si sihT.

Sizes:
64      RevString
64      RevString2
65      RevStr
62      szRev
121     RevLingo

--- ok ---

frktons

#47
 :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol

I have to confess that...
I'd like some Algos explanation.  Just reading the CODE could take
half of my holydays to figure it out, and not sure about that.
After all I'm just an old slow assembly beginner student  :P

What is the logic behind the routines? Why should they be faster or slower?
What kind of Assembly gems are these? What are their strong points?

:lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol  :lol

I'll try to understand them anyway, but a little help is always welcome  :U

Frank
Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

I have addresses two very simple algo designs, the reverse string algo scans a string to get its length first, then it swaps opposite end pairs until it gets to the middle.


123456789    ; original
923456781    ; 1st swap
983456721    ; 2nd swap
987456321    ; 3rd pair
987654321    ; reversed string
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: hutch-- on May 24, 2010, 12:12:09 AM
Frank,

I have addresses two very simple algo designs, the reverse string algo scans a string to get its length first, then it swaps opposite end pairs until it gets to the middle.


123456789    ; original
923456781    ; 1st swap
983456721    ; 2nd swap
987456321    ; 3rd pair
987654321    ; reversed string


Thanks Hutch, this is the kind of help I need to direct my eyes
into the routines  :U
Mind is like a parachute. You know what to do in order to use it :-)