The MASM Forum

Projects => MASM32 => Topic started by: K_F on February 23, 2020, 06:48:56 AM

Title: Register preservation in Lib functions
Post by: K_F on February 23, 2020, 06:48:56 AM
Hutch probably knows about this  :greenclp:

Was doing a conversion loading a buffer with szCatStr.
The proggy was crashing, so after a bit of digging found that szCatStr wasn't preserving some registers.
This might be the case with more Masm32lib functions - Maybe a project idea  :thumbsup:

szCatStr in Masm32Lib

; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

    .486
    .model flat, stdcall
    option casemap :none

    szLen PROTO :DWORD

    .code

; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 16

szCatStr proc lpszSource:DWORD, lpszAdd:DWORD

    push edi

    invoke szLen,[esp+8]            ; get source length
    mov edi, [esp+8]
    mov ecx, [esp+12]
    add edi, eax                    ; set write starting position
    xor edx, edx                    ; zero index

  @@:
    movzx eax, BYTE PTR [ecx+edx]   ; write append string to end of source
    mov [edi+edx], al
    add edx, 1
    test eax, eax                   ; exit when terminator is written
    jne @B

    pop edi

    mov eax, [esp+8]                ; return start address of source

    ret 8

szCatStr endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллллл

end


So I made a quickie macro..

VCatStr MACRO SRC:REQ, DEST:REQ
push edx
push ecx
invoke szCatStr, DEST, SRC
pop ecx
pop edx
endm


Title: Re: Register preservation in Lib functions
Post by: Vortex on February 23, 2020, 07:01:01 AM
Hi K_F,

szCatStr and the other function does not need to preserve the volatile registers eax,ecx and edx.
Title: Re: Register preservation in Lib functions
Post by: K_F on February 23, 2020, 07:12:41 AM
Ah.. old age creeping in  :tongue:
I was thinking in terms of EAX return only.... Freudian slip  :skrewy:

Anyway.. a 'useful' macro if needs be.
Title: Re: Register preservation in Lib functions
Post by: Adamanteus on February 23, 2020, 07:25:04 AM
 That's problem rise when lib used with compilers, as it require to preserve edi, edi, ebx or other as used.
I'm for such cases use text macro "preserve", that's follows clouse USES, after proc decares.
For return values for each type macro FRETxxx Val : REQ
So, if to count to use lib with differ thing programming - really need something to do  :eusa_boohoo:

P. S.
This solution is not optimal for free push/pop's commands, but optimise for differ enviroments lib by unattentevly mistakes too hard, so unused USES registers, possible fall only as task for new option for UASM developrs   :rofl: