News:

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

Two routines sharing setup code

Started by Larry Hammick, December 03, 2009, 06:14:44 PM

Previous topic - Next topic

Larry Hammick

There is a trick involving the carry flag that allows two routines to share the same initial few instructions. Here's an illustration.
stringmatch_A:      ;(lpsz,lpsz,count)
    db 0Ch      ;opcode for OR AL,immed8. This will clear the carry flag.
stringmatch_W:      ;(lpUnicodeString,lpUnicodeString,count)
    stc
    push ebp
    mov ebp,esp
    push esi
    push edi
    mov esi,[ebp+8]
    mov edi,[ebp+12]
    mov ecx,[ebp+16]
    jc short unicode
@@: cmpsb
    loope @B
    jmp short stringmatch_done
unicode:
@@: cmpsw
    loope @B
stringmatch_done:
    mov eax,0
    je short @F     ;return eax = 0, 1, or -1 according to the comparison
    sbb eax,eax
    sbb eax,-1
@@: pop edi
    pop esi
    pop ebp
    ret 12