flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Personal Developments

Author
Thread Post new topic Reply to topic
FoXx



Joined: 25 Feb 2025
Posts: 24
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: 1748
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: 24
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: 24
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
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.