News:

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

A "varargs" routine for concatenating stuff

Started by Larry Hammick, November 12, 2009, 02:58:54 AM

Previous topic - Next topic

Larry Hammick

Cleans its own stack and also allows 1- and 2-byte text items to be passed by value rather than by reference, which is convenient when inserting a space or a colon or similar between strings.

MyConcat:     ;(destination, component, component, ..., 0)
    pop edx     ;return address
    pop ecx     ;destination
MyConcat_:
    pop eax
    test eax,eax
    jz short jmpedx
    test eax,0FFFF0000h    ;in Win32 an address is always >= 64K
    jz short @F
    push edx
    push eax         ;src for sz_copy
    push ecx         ;dest
    call sz_copy@8   ;returns eax=terminator in destination
    pop edx
    xchg eax,ecx     ;or mov ecx,eax. xchg eax,reg is a one-byte opcode.
    jmp MyConcat_
@@: mov [ecx],al
    inc ecx
    shr eax,8
    jnz @B
    jmp MyConcat_
jmpedx: jmp edx


sz_copy@8:      ;(lpDest,lpSrc)  returns eax=terminator in destination
    pop eax
    pop edx      ;dest
    pop ecx      ;src
    push eax
    sub edx,ecx
@@: mov al,[ecx]
    mov [ecx+edx],al
    inc ecx
    test al,al
    jnz @B
    lea eax,[ecx+edx-1]
    ret      ;stack is clean

hutch--

Larry,

The masm32 library has a routine originally written By Alex Yukubtchik called szMultiCat that is a reasonably fast and certainly reliable procedure.


; #########################################################################

;       -------------------------------------------------------------
;       This original algorithm was designed by Alexander Yackubtchik
;                     It was re-written in August 2006
;       -------------------------------------------------------------

    .486
    .model flat, stdcall  ; 32 bit memory model
    option casemap :none  ; case sensitive

    .code

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

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 4

szMultiCat proc C pcount:DWORD,lpBuffer:DWORD,args:VARARG

    push ebx
    push esi
    push edi
    push ebp

    mov ebx, 1

    mov ebp, [esp+20]

    mov edi, [esp+24]           ; lpBuffer
    xor ecx, ecx
    lea edx, [esp+28]           ; args
    sub edi, ebx
  align 4
  @@:
    add edi, ebx
    movzx eax, BYTE PTR [edi]   ; unroll by 2
    test eax, eax
    je nxtstr

    add edi, ebx
    movzx eax, BYTE PTR [edi]
    test eax, eax
    jne @B
  nxtstr:
    sub edi, ebx
    mov esi, [edx+ecx*4]
  @@:
    add edi, ebx
    movzx eax, BYTE PTR [esi]   ; unroll by 2
    mov [edi], al
    add esi, ebx
    test eax, eax
    jz @F

    add edi, ebx
    movzx eax, BYTE PTR [esi]
    mov [edi], al
    add esi, ebx
    test eax, eax
    jne @B

  @@:
    add ecx, ebx
    cmp ecx, ebp                ; pcount
    jne nxtstr

    mov eax, [esp+24]           ; lpBuffer

    pop ebp
    pop edi
    pop esi
    pop ebx

    ret

szMultiCat endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

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