Assembly code snippets

Back to the snippets overview

Details

Titlewsprintf_sci
Authorscientica
Submitted by:scientica
Date added:2003-06-01 14:10:19
Date modified:2003-06-01 14:11:12

Comments

While realising that the wsprintf function has a limit on the string length I wrote my own function for insterting strings in an other string, this function uses 0FFh instead of '%s' as string insert indicator, please not that this functon has only been submited to limited testing (i.e. they're used in my latest project, and has so far worked for me).
This funciton uses the C calling convention, which means you are responsible for balancing the stack.
example of usage (code from my project, replace jcall with call, jcall is a macro)
push ADSLr.szNip ; string to instert at first 0FFh
push ADSLr.szADSL ; source/input buffer, containing the formatter string ("0FFh-string")
push [ADSLr.hHTMLpage] ; destination/output buffer
jcall _wsprintf_sci
add esp, 4*3 ; <!> N.B! Balance the Stack!

Feel free to send your comments to me via my e-mail. [EDIT]N.B. the code is in FASM syntax.[/EDIT]

Snippet

;[ebp+...] args
;[ebp+C] lpformat
;[ebp+8] lpDst
wsprintf_sci:
        push    ebp
        mov     ebp,esp
lpFormat_len equ ebp-4  ;.lpFormat_len
        sub     esp, 4
        pushad

        xor     edx,edx

        mov     edi, [ebp+8]
        mov     esi, [ebp+0Ch]

        ; scan for null terminator and calculate length of input string
        xor     ecx,ecx
@@:     mov     al, [esi+ecx]
        or      al,al
        jz      .lok
        inc     ecx
        jmp     @B
.lok:   ; ecx = length of [lpDst] (incl null terminator)

        mov     [lpFormat_len],ecx
        xor     ebx,ebx

@@:     mov     al, [esi]
        cmp     al, 0FFh        ; test for string insert indicator (0FFh)
        jz      .insertString
        mov     [edi+ebx],al
        inc     ebx
        inc     esi
.k:     cmp     al,0
        jnz     @B

        mov     byte [edi+ebx],0

        popad
        add     esp,4 ; remove arg
        pop     ebp
        ret

.insertString:
        inc     esi
        push    esi
        push    ecx
        push    ebp
        mov     esi, [ebp+edx*4+10h]    ; get ptr to arg

        ; get len of arg
        xor     ecx,ecx
@@:     mov     al, [esi+ecx]
        or      al,al
        jz      .lok2
        inc     ecx
        jmp     @B
.lok2:   ; ecx = length of [lpDst] (incl null terminator)
        dec    ecx
        push   ecx

        ; copy arg to [plDst], last char first
.l:     mov     al, [esi+ecx]
        lea     ebp, [edi+ebx]     ;|since below is illegal
        mov     [ds:ebp+ecx],al    ;|mov     [edi+ebx+ecx],al
        sub     ecx,1
        jns     .l      ; if ecx=-1 then  we've copied all chars

        pop     ecx
        add     ebx,ecx ; adjust the dst offset.

        inc     edx  ; increase argument counter
        pop     ebp
        pop     ecx
        pop     esi
        jmp     .k