flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > String processing routines

Author
Thread Post new topic Reply to topic
FoXx



Joined: 25 Feb 2025
Posts: 26
FoXx 15 Dec 2025, 13:15
In the early 2000s, I developed several string processing routines. They've stood the test of time. I'd like to share that code.

Code:
continue        equ @r
skip            equ @f

;------------------------------------------------
;       * * *  String To DWORD  * * *
;------------------------------------------------
proc StrToInt

;       mov ESI, lpBuffer

        xor EDX, EDX
        or  ESI, ESI
        jz  skip

                mov ECX, EDX
                mov EBX, EDX
                mov  BL, 10
.scan:
                lodsb
                cmp AL, '0'
                jb  skip

                cmp AL, '9' 
                ja  skip

                        sub AL, '0'
                        mov CL, AL

                        mov EAX, EDX
                        mul EBX 
                        add EAX, ECX
                        mov EDX, EAX
        
                jmp .scan
@@:
        mov EAX, EDX    ;       return Value
        ret
endp

;------------------------------------------------
;       * * *  INT To String  * * *
;------------------------------------------------
proc IntToStr

;       mov EDI, lpBuffer
;       mov ECX, Value

        xor EBX, EBX
        mov  BL, 10
        mov EAX, ECX

        @@:
                inc EDI
                xor EDX, EDX
                div EBX

                or EAX, EAX
                jnz continue

        mov ESI, EDI
        mov EAX, ECX

        @@:
                dec EDI
                xor EDX, EDX
                div EBX

                add DL, '0'
                mov [EDI], DL

                or EAX, EAX
                jnz continue

        mov EDI, ESI    ;       set the end position
        ret
endp

;------------------------------------------------
;       * * *  HEX To String  * * *
;------------------------------------------------
sHexScaleChar   DB '0123456789ABCDEF'   ;        it's in the data section

proc HexToStr

;   mov EDI, lpBuffer
;   mov EDX, Value

        mov ESI, sHexScaleChar
        xor ECX, ECX
        mov EBX, ECX
        mov  CL, 8

        @@:
                rol EDX, 4
                mov BL,  DL
                and BL,  01111b         ;       get 4 bits

                mov AL, [ESI+EBX]
                stosb

                loop continue
        ret
endp    


Last edited by FoXx on 15 Dec 2025, 14:20; edited 2 times in total
Post 15 Dec 2025, 13:15
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1749
Location: Toronto, Canada
AsmGuru62 15 Dec 2025, 13:51
Very nice looking code!
Especially, I like how you use blank lines.
I do that too.
Post 15 Dec 2025, 13:51
View user's profile Send private message Send e-mail Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 26
FoXx 15 Dec 2025, 14:06
AsmGuru62 wrote:
Very nice looking code!
Especially, I like how you use blank lines.
I do that too.

Thank you Smile
Blank lines separate code into blocks.

I follow certain rules in my procedures. My principle is to use registers as often as possible. This speeds up my procedures.
Post 15 Dec 2025, 14:06
View user's profile Send private message Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 26
FoXx 15 Dec 2025, 14:32
Useful command line splitting procedure.
Applied after the call [GetCommandLine]
Code:
;------------------------------------------------
;       * * *   Get paramameters of string
;------------------------------------------------
proc GetParamComnandLine

;       mov EDI, TableParamameters
;       mov ESI, szComnandLine

        mov EBX, EDI

.scan:
                lodsb
                cmp AL, '"'
                je skip

                cmp AL, "'"
                je skip

                        dec ESI
                        mov AL, ' '
                @@:

                mov  DL, AL
                mov EAX, ESI
                stosd

                @@:
                        lodsb
                        cmp AL, ' '
                        jb .end

                        cmp AL, DL
                        jne continue

                mov EDX, ESI
                dec ESI

                xor EAX, EAX
                mov [ESI], AL

                mov ESI, EDX

                @@:
                        lodsb
                        cmp AL, ' '
                        jb .end
                        je continue

                dec ESI
                jmp .scan

.end:
        mov ECX, EDI
        xor EAX, EAX
        stosd

        dec ESI
        mov [ESI], AL

        mov EAX, ECX
        sub EAX, EBX
        shr EAX, 2              ;       count of paramameters
        ret
endp    
Post 15 Dec 2025, 14:32
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1749
Location: Toronto, Canada
AsmGuru62 16 Dec 2025, 01:29
I found out the strange thing about the command line parameters.
In C language, using the double quotes preserves blanks inside of the quotes.
Everyone knows that, but what is interesting --- if there is no blank between, say, parameter #2 and parameter #3 --- the parameter includes all text.
Here is an example:
Code:
PROGRAM.EXE -param1 -param2"C:\My Files Are Here\Source.Asm"more
    

When PROGRAM.EXE is a C program, you get the following 2 parameters (not 3 or 4):
Code:
-param1
-param2C:\My Files Are Here\Source.Asmmore
    


You see?
Parameters are stuck together if you have no blanks.
Very interesting.
And I think, it is not a bug in C.
Probably done to put the flag, like "-mydir" together with actual directory name:
Code:
PROGRAM.EXE -mydir:"C:\My Files Are Here"
    
Post 16 Dec 2025, 01:29
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20793
Location: In your JS exploiting you and your system
revolution 16 Dec 2025, 01:42
Quote characters are not generally considered as separators, that's what spaces are for. Quotes are used to "hide" the spaces from the tokeniser.
Post 16 Dec 2025, 01:42
View user's profile Send private message Visit poster's website Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 26
FoXx 16 Dec 2025, 07:59
revolution wrote:
Quote characters are not generally considered as separators, that's what spaces are for. Quotes are used to "hide" the spaces from the tokeniser.
This began with the advent of long file names. The system still uses the shortened form of the name. You can launch an application using the shortened name. The shortened name can be retrieved using the GetShortPathNameA function.
And in old-school C programming, the convention was to not use long file names.
AsmGuru62 wrote:
I found out the strange thing about the command line parameters.
In C language, using the double quotes preserves blanks inside of the quotes.

You see?
Parameters are stuck together if you have no blanks.
AsmGuru62, you're right. I've never had command line parameters interpreted this way. My function won't work that way.


Last edited by FoXx on 16 Dec 2025, 08:23; edited 1 time in total
Post 16 Dec 2025, 07:59
View user's profile Send private message Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 26
FoXx 16 Dec 2025, 08:22
Finding a variable index by keyword.
Code:
proc FindKeyWordIndex

;       mov ESI, TableKeyWords
;       mov EDX, length
;       mov EAX, String


        push EBP

        mov EBP, EAX
        xor EAX, EAX
        mov EBX, EAX
        mov ECX, EAX
        @@:
                inc EBX         ;       index counter
                add ESI, ECX
                lodsb

                mov ECX, EAX    ;       end of table
                jECXz skip

                cmp EAX, EDX
                jne continue

                        mov EDI, EBP
                        repe cmpsb
                        jne continue

                                mov EAX, EBX
        @@:
        pop EBP
        ret
endp
    
We use it like this.
Code:
;       data section
TableKeyWords DB 5,'Apple',4,'Pear',6,'Orange',6,'Banana',7,'Coconut',9,'Pineapple'

TableParamIndex ANY_STRUCTURE ?    
Code:
struct ANY_STRUCTURE
 Apple          dd ?
 Pear           dd ?
 Orange         dd ?
 Banana         dd ?
 Coconut        dd ?
 Pineapple      dd ?
ends

;       define a constant

ERROR_INDEX     = 0
APPLE_INDEX     = 1
PEAR_INDEX      = 2
ORANGE_INDEX    = 3
BANANA_INDEX    = 4
COCONUT_INDEX   = 5
PINEAPPLE_INDEX = 6


        mov ESI, TableKeyWords 
        mov EDX, length
        mov EAX, String
        call FindKeyWordIndex

        or EAX, EAX
        jz .no_found

                mov EAX, [value]
                mov [TableParamIndex+EBX*4], EAX    
Post 16 Dec 2025, 08:22
View user's profile Send private message 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.